Skip to content

Commit

Permalink
fix(demo): add null check to dial code & phone number fields
Browse files Browse the repository at this point in the history
Close #12
  • Loading branch information
paodb authored and javier-godoy committed Mar 22, 2024
1 parent d18fc3e commit 490df56
Showing 1 changed file with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@
import com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.FlexLayout;
import com.vaadin.flow.component.textfield.NumberField;
import com.vaadin.flow.component.textfield.IntegerField;
import com.vaadin.flow.component.textfield.TextArea;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.WebBrowser;
import org.apache.commons.lang3.StringUtils;

@DemoSource
@PageTitle("Whatsapp Button Demo")
@Route(value = "whatsappbutton/whatsappbutton", layout = WhatsappButtonDemoView.class)
@SuppressWarnings("serial")
public class WhatsappButtonDemo extends Div {

private Checkbox redirect;

public WhatsappButtonDemo() {

WhatsappButton whatsappButton = new WhatsappButton();

Div buttonContainer = new Div();
buttonContainer.setClassName("centered button-container");
buttonContainer.add(whatsappButton);
Expand All @@ -36,30 +38,32 @@ public WhatsappButtonDemo() {
// button label
TextField label = createTextField("Enter a label for the button");
label.addValueChangeListener(e -> whatsappButton.setLabel(e.getValue()));

// message text
TextArea text = new TextArea();
text.setPlaceholder("Enter a text message");
text.setWidthFull();
text.setClearButtonVisible(true);
text.addValueChangeListener(e -> whatsappButton.setText(e.getValue()));

// phone number (dial code + phone number)
FlexLayout phoneLayout = new FlexLayout();
phoneLayout.setWidthFull();
NumberField dialCode = createNumberField("Code");
IntegerField dialCode = createIntegerField("Code");
dialCode.addClassName("dialcode");
dialCode.addValueChangeListener(e -> whatsappButton.setDialCode(String.valueOf(e.getValue().intValue())));
NumberField phone = createNumberField("Phone number");
dialCode.addValueChangeListener(e -> whatsappButton
.setDialCode(e.getValue() != null ? String.valueOf(e.getValue()) : StringUtils.EMPTY));
IntegerField phone = createIntegerField("Phone number");
phone.addClassName("phone");
phone.addValueChangeListener(e -> whatsappButton.setPhone(String.valueOf(e.getValue().intValue())));
phone.addValueChangeListener(e -> whatsappButton
.setPhone(e.getValue() != null ? String.valueOf(e.getValue()) : StringUtils.EMPTY));
phoneLayout.add(dialCode, phone);
phoneLayout.expand(phone);

// invite code
TextField inviteCode = createTextField("Enter a invite code");
inviteCode.addValueChangeListener(e -> whatsappButton.setInviteCode(e.getValue()));

FormLayout formLayout = new FormLayout();
formLayout.addClassName("centered");
formLayout.addFormItem(label, "Label");
Expand All @@ -68,10 +72,7 @@ public WhatsappButtonDemo() {
formLayout.addFormItem(inviteCode, "Invite Code");
add(formLayout);

formLayout.setResponsiveSteps(
new ResponsiveStep("0", 1),
new ResponsiveStep("900px", 2)
);
formLayout.setResponsiveSteps(new ResponsiveStep("0", 1), new ResponsiveStep("900px", 2));

Checkbox leadingIcon = new Checkbox();
leadingIcon.addValueChangeListener(e -> whatsappButton.setLeadingIcon(e.getValue()));
Expand All @@ -87,10 +88,10 @@ public WhatsappButtonDemo() {
checksLayout.addFormItem(leadingIcon, "Leading Icon");
checksLayout.addFormItem(redirect, "Redirect");
checksLayout.addFormItem(responsive, "Responsive");
checksLayout.addFormItem(dialog, "Dialog");
add(checksLayout);
checksLayout.addFormItem(dialog, "Dialog");
add(checksLayout);
}

private TextField createTextField(String placeholder) {
TextField textField = new TextField();
textField.setPlaceholder(placeholder);
Expand All @@ -99,25 +100,26 @@ private TextField createTextField(String placeholder) {
return textField;
}

private NumberField createNumberField(String placeholder) {
NumberField numberField = new NumberField();
numberField.setPlaceholder(placeholder);
numberField.setClearButtonVisible(true);
return numberField;
private IntegerField createIntegerField(String placeholder) {
IntegerField integerField = new IntegerField();
integerField.setPlaceholder(placeholder);
integerField.setClearButtonVisible(true);
return integerField;
}

@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
this.initializeRedirect();
}

private void initializeRedirect() {
UI.getCurrent().getPage().retrieveExtendedClientDetails(details -> {
WebBrowser webBrowser = VaadinSession.getCurrent().getBrowser();
boolean isMobile = webBrowser != null && (webBrowser.isAndroid() || webBrowser.isIPhone() || webBrowser.isWindowsPhone() || details.isIPad());
boolean isMobile = webBrowser != null && (webBrowser.isAndroid() || webBrowser.isIPhone()
|| webBrowser.isWindowsPhone() || details.isIPad());
redirect.setValue(!isMobile);
});
});
}

}

0 comments on commit 490df56

Please sign in to comment.