All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.flow.data.renderer.NativeButtonRenderer Maven / Gradle / Ivy

There is a newer version: 24.5.5
Show newest version
/*
 * Copyright 2000-2024 Vaadin Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.vaadin.flow.data.renderer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.ElementFactory;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.shared.Registration;

/**
 *
 * A template renderer to create a clickable button.
 * 

* {@link ItemClickListener}s are notified when the rendered buttons are either * clicked or tapped (in touch devices). * * @author Vaadin Ltd * * @param * the type of the item to be received in the click listeners */ public class NativeButtonRenderer extends BasicRenderer implements ClickableRenderer { private List> listeners = new ArrayList<>(1); /** * Creates a new button renderer with the specified label. The label is the * same for all the items. *

* Item click listeners can be added via * {@link #addItemClickListener(ItemClickListener)}. * * @param label * the label of the rendered button, not null */ public NativeButtonRenderer(String label) { this(value -> label); } /** * Creates a new button renderer with the specified label and registers a * {@link ItemClickListener} to receive events. The label is the same for * all the items. *

* More click listeners can be added via * {@link #addItemClickListener(ItemClickListener)}. * * @param label * the label for the rendered button, not null * @param clickListener * a listener to receive click events */ public NativeButtonRenderer(String label, ItemClickListener clickListener) { this(label); addItemClickListener(clickListener); } /** * Creates a new button renderer with a dynamic label. *

* Item click listeners can be added via * {@link #addItemClickListener(ItemClickListener)}. * * @param labelProvider * the provider for the labels of the rendered buttons, not * null */ public NativeButtonRenderer(ValueProvider labelProvider) { super(labelProvider); } /** * Creates a new button renderer with a dynamic label and registers a * {@link ItemClickListener} to receive events. *

* More click listeners can be added via * {@link #addItemClickListener(ItemClickListener)}. * * @param labelProvider * the provider for the labels of the rendered buttons, not * null * @param clickListener * a listener to receive click events */ public NativeButtonRenderer(ValueProvider labelProvider, ItemClickListener clickListener) { this(labelProvider); addItemClickListener(clickListener); } @Override public Registration addItemClickListener( ItemClickListener listener) { return Registration.addAndRemove(listeners, listener); } @Override public List> getItemClickListeners() { return Collections.unmodifiableList(listeners); } @Override protected String getTemplateForProperty(String property, Rendering context) { String templatePropertyName = getTemplatePropertyName(context); String eventName = templatePropertyName + "_event"; String disabledName = templatePropertyName + "_disabled"; setEventHandler(eventName, this::onClick); return String.format( "", eventName, disabledName, property); } @Override public Component createComponent(SOURCE item) { Element button = ElementFactory .createButton(getValueProvider().apply(item)); button.addEventListener("click", event -> getItemClickListeners() .forEach(listeners -> listeners.onItemClicked(item))); return ComponentUtil.componentFromElement(button, NativeButton.class, true); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy