Search in sources :

Example 61 with Checkbox

use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.

the class FormLayoutView method createCompositeLayout.

private void createCompositeLayout() {
    // And then just use it like a regular component
    MyCustomLayout layout = new MyCustomLayout();
    TextField name = new TextField();
    TextField email = new TextField();
    Checkbox emailUpdates = new Checkbox("E-mail me updates");
    layout.addItemWithLabel("Name", name);
    // Both the email field and the emailUpdates checkbox are wrapped inside
    // the same form item
    layout.addItemWithLabel("E-mail", email, emailUpdates);
    addCard("Using form layout inside a composite", layout);
}
Also used : Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField)

Example 62 with Checkbox

use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.

the class FormLayoutView method createFormLayoutWithBinder.

private void createFormLayoutWithBinder() {
    FormLayout layoutWithBinder = new FormLayout();
    Binder<Contact> binder = new Binder<>();
    // The object that will be edited
    Contact contactBeingEdited = new Contact();
    // Create the fields
    TextField firstName = new TextField();
    firstName.setValueChangeMode(ValueChangeMode.EAGER);
    TextField lastName = new TextField();
    lastName.setValueChangeMode(ValueChangeMode.EAGER);
    TextField phone = new TextField();
    phone.setValueChangeMode(ValueChangeMode.EAGER);
    TextField email = new TextField();
    email.setValueChangeMode(ValueChangeMode.EAGER);
    DatePicker birthDate = new DatePicker();
    Checkbox doNotCall = new Checkbox("Do not call");
    Label infoLabel = new Label();
    NativeButton save = new NativeButton("Save");
    NativeButton reset = new NativeButton("Reset");
    layoutWithBinder.addFormItem(firstName, "First name");
    layoutWithBinder.addFormItem(lastName, "Last name");
    layoutWithBinder.addFormItem(birthDate, "Birthdate");
    layoutWithBinder.addFormItem(email, "E-mail");
    FormItem phoneItem = layoutWithBinder.addFormItem(phone, "Phone");
    phoneItem.add(doNotCall);
    // Button bar
    HorizontalLayout actions = new HorizontalLayout();
    actions.add(save, reset);
    save.getStyle().set("marginRight", "10px");
    SerializablePredicate<String> phoneOrEmailPredicate = value -> !phone.getValue().trim().isEmpty() || !email.getValue().trim().isEmpty();
    // E-mail and phone have specific validators
    Binding<Contact, String> emailBinding = binder.forField(email).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").withValidator(new EmailValidator("Incorrect email address")).bind(Contact::getEmail, Contact::setEmail);
    Binding<Contact, String> phoneBinding = binder.forField(phone).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").bind(Contact::getPhone, Contact::setPhone);
    // Trigger cross-field validation when the other field is changed
    email.addValueChangeListener(event -> phoneBinding.validate());
    phone.addValueChangeListener(event -> emailBinding.validate());
    // First name and last name are required fields
    firstName.setRequiredIndicatorVisible(true);
    lastName.setRequiredIndicatorVisible(true);
    binder.forField(firstName).withValidator(new StringLengthValidator("Please add the first name", 1, null)).bind(Contact::getFirstName, Contact::setFirstName);
    binder.forField(lastName).withValidator(new StringLengthValidator("Please add the last name", 1, null)).bind(Contact::getLastName, Contact::setLastName);
    // Birthdate and doNotCall don't need any special validators
    binder.bind(doNotCall, Contact::isDoNotCall, Contact::setDoNotCall);
    binder.bind(birthDate, Contact::getBirthDate, Contact::setBirthDate);
    // Click listeners for the buttons
    save.addClickListener(event -> {
        if (binder.writeBeanIfValid(contactBeingEdited)) {
            infoLabel.setText("Saved bean values: " + contactBeingEdited);
        } else {
            BinderValidationStatus<Contact> validate = binder.validate();
            String errorText = validate.getFieldValidationStatuses().stream().filter(BindingValidationStatus::isError).map(BindingValidationStatus::getMessage).map(Optional::get).distinct().collect(Collectors.joining(", "));
            infoLabel.setText("There are errors: " + errorText);
        }
    });
    reset.addClickListener(event -> {
        // clear fields by setting null
        binder.readBean(null);
        infoLabel.setText("");
        doNotCall.setValue(false);
    });
    infoLabel.setId("binder-info");
    firstName.setId("binder-first-name");
    lastName.setId("binder-last-name");
    phone.setId("binder-phone");
    email.setId("binder-email");
    birthDate.setId("binder-birth-date");
    doNotCall.setId("binder-do-not-call");
    save.setId("binder-save");
    reset.setId("binder-reset");
    addCard("A form layout with fields using Binder", layoutWithBinder, actions, infoLabel);
}
Also used : FormLayout(com.vaadin.flow.component.formlayout.FormLayout) BinderValidationStatus(com.vaadin.flow.data.binder.BinderValidationStatus) Component(com.vaadin.flow.component.Component) Composite(com.vaadin.flow.component.Composite) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Binder(com.vaadin.flow.data.binder.Binder) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) ResponsiveStep(com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) Route(com.vaadin.flow.router.Route) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) TextField(com.vaadin.flow.component.textfield.TextField) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) H2(com.vaadin.flow.component.html.H2) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) FormLayout(com.vaadin.flow.component.formlayout.FormLayout) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) DatePicker(com.vaadin.flow.component.datepicker.DatePicker) LocalDate(java.time.LocalDate) Optional(java.util.Optional) Binding(com.vaadin.flow.data.binder.Binder.Binding) NativeButton(com.vaadin.flow.component.html.NativeButton) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Optional(java.util.Optional) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Label(com.vaadin.flow.component.html.Label) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField) DatePicker(com.vaadin.flow.component.datepicker.DatePicker)

Example 63 with Checkbox

use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.

the class AbstractItemCountGridPage method initDataProvider.

private void initDataProvider() {
    menuBar.add("Defined / Undefined size");
    dataProvider = new LazyLoadingProvider();
    Button button1 = new Button("withUndefinedSize() -> undefined size", event -> switchToUndefinedSize());
    button1.setId(UNDEFINED_SIZE_BUTTON_ID);
    menuBar.add(button1);
    menuBar.add(new Hr());
    Button button2 = new Button("setDataProvider(FetchCallback) -> undefined size", event -> switchToUndefinedSizeCallback());
    menuBar.add(button2);
    button2.setId("fetchcallback");
    dataProviderSizeInput = new IntegerField("Fixed size backend size");
    menuBar.add(dataProviderSizeInput, new Hr());
    fetchCallbackSizeInput = new IntegerField("Undefined-size backend size", event -> fetchCallbackSize = event.getValue());
    fetchCallbackSizeInput.setId(UNDEFINED_SIZE_BACKEND_SIZE_INPUT_ID);
    fetchCallbackSizeInput.setWidthFull();
    menuBar.add(fetchCallbackSizeInput, new Hr());
    Button button3 = new Button("setDefinedSize(CountCallback) -> defined size", event -> switchToDefinedSize());
    menuBar.add(button3);
    button3.setId(DEFINED_SIZE_BUTTON_ID);
    Button button4 = new Button("setDataProvider(DataProvider) -> defined size", event -> switchToDataProvider());
    menuBar.add(button4);
    button4.setId(DATA_PROVIDER_BUTTON_ID);
    dataProviderSizeInput.setId(DATA_PROVIDER_SIZE_INPUT_ID);
    dataProviderSizeInput.setValue(dataProviderSize);
    dataProviderSizeInput.setWidthFull();
    dataProviderSizeInput.addValueChangeListener(event -> {
        dataProviderSize = event.getValue();
        dataProvider.refreshAll();
    });
    dataProviderSizeInput.setEnabled(false);
    menuBar.add(dataProviderSizeInput, new Hr());
    Checkbox checkbox = new Checkbox("Show fetch query logs", event -> logPanel.setVisible(event.getSource().getValue()));
    checkbox.setValue(true);
    menuBar.add(checkbox);
}
Also used : IntStream(java.util.stream.IntStream) Grid(com.vaadin.flow.component.grid.Grid) Query(com.vaadin.flow.data.provider.Query) ValueProvider(com.vaadin.flow.function.ValueProvider) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) AbstractBackEndDataProvider(com.vaadin.flow.data.provider.AbstractBackEndDataProvider) Hr(com.vaadin.flow.component.html.Hr) Logger(java.util.logging.Logger) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) Button(com.vaadin.flow.component.button.Button) Stream(java.util.stream.Stream) FlexLayout(com.vaadin.flow.component.orderedlayout.FlexLayout) IntegerField(com.vaadin.flow.component.textfield.IntegerField) RouterLink(com.vaadin.flow.router.RouterLink) BeforeEnterObserver(com.vaadin.flow.router.BeforeEnterObserver) Range(com.vaadin.flow.internal.Range) Button(com.vaadin.flow.component.button.Button) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) Hr(com.vaadin.flow.component.html.Hr) IntegerField(com.vaadin.flow.component.textfield.IntegerField)

Example 64 with Checkbox

use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.

the class CheckboxDemoPage method addDefaultCheckbox.

private void addDefaultCheckbox() {
    Checkbox checkbox = new Checkbox();
    checkbox.setLabel("Default Checkbox");
    checkbox.setId("default-checkbox");
    NativeButton button = new NativeButton("Change label", event -> {
        checkbox.setLabel("New Label");
    });
    button.setId("change-default-label");
    addCard("Default Checkbox", checkbox, button);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) Checkbox(com.vaadin.flow.component.checkbox.Checkbox)

Example 65 with Checkbox

use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.

the class CheckboxDemoPage method addValueChangeCheckbox.

private void addValueChangeCheckbox() {
    Checkbox valueChangeCheckbox = new Checkbox("Checkbox with a ValueChangeListener");
    Div message = new Div();
    valueChangeCheckbox.addValueChangeListener(event -> message.setText(String.format("Checkbox value changed from '%s' to '%s'", event.getOldValue(), event.getValue())));
    addCard("Checkbox with a ValueChangeListener", valueChangeCheckbox, message);
    valueChangeCheckbox.setId("value-change-checkbox");
    message.setId("value-change-checkbox-message");
}
Also used : Div(com.vaadin.flow.component.html.Div) Checkbox(com.vaadin.flow.component.checkbox.Checkbox)

Aggregations

Checkbox (com.vaadin.flow.component.checkbox.Checkbox)68 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)16 Div (com.vaadin.flow.component.html.Div)14 TextField (com.vaadin.flow.component.textfield.TextField)14 Route (com.vaadin.flow.router.Route)14 Button (com.vaadin.flow.component.button.Button)12 Grid (com.vaadin.flow.component.grid.Grid)12 Label (com.vaadin.flow.component.html.Label)12 Collectors (java.util.stream.Collectors)10 Component (com.vaadin.flow.component.Component)8 NativeButton (com.vaadin.flow.component.html.NativeButton)8 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)8 Optional (java.util.Optional)8 Binder (com.vaadin.flow.data.binder.Binder)7 EmailValidator (com.vaadin.flow.data.validator.EmailValidator)7 Set (java.util.Set)7 FurmsViewComponent (io.imunity.furms.ui.components.FurmsViewComponent)6 PageTitle (io.imunity.furms.ui.components.PageTitle)6 NotificationUtils.showErrorNotification (io.imunity.furms.ui.utils.NotificationUtils.showErrorNotification)6 DateTimeFormatter (java.time.format.DateTimeFormatter)6