Skip to content

Commit

Permalink
InputPhone: use E.164 format by default (#1445)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaIsotta committed Feb 18, 2024
1 parent db56491 commit f51c9a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class InputPhone extends AbstractPrimeHtmlInputText implements Widget, In
public static final String STYLE_CLASS = "ui-inputphone ui-widget";
public static final String EVENT_COUNTRY_SELECT = "countrySelect";
public static final String COUNTRY_AUTO = "auto";
public static final String INPUT_SUFFIX = "_input";

private static final Collection<String> EVENT_NAMES = LangUtils
.unmodifiableList("blur", "change", "valueChange", "select", "click", "dblclick", "focus", "keydown", "keypress", "keyup", "mousedown",
Expand Down Expand Up @@ -134,12 +135,12 @@ public String getDefaultEventName() {

@Override
public String getInputClientId() {
return getClientId() + "_input";
return getClientId() + INPUT_SUFFIX;
}

@Override
public String getValidatableInputClientId() {
return getClientId() + "_input";
return getClientId() + INPUT_SUFFIX;
}

@Override
Expand All @@ -152,6 +153,7 @@ public void setLabelledBy(final String labelledBy) {
getStateHelper().put("labelledby", labelledBy);
}

@Override
public void setDir(final String _dir) {
getStateHelper().put(PropertyKeys.dir, _dir);
}
Expand Down Expand Up @@ -337,10 +339,6 @@ public void setCountrySearch(final boolean countrySearch) {
getStateHelper().put(PropertyKeys.countrySearch, countrySearch);
}

public boolean isUtilsScriptRequired() {
return !AutoPlaceholder.off.name().equals(getAutoPlaceholder()) || isFormatOnDisplay() || isFormatAsYouType();
}

@Override
public Collection<String> getEventNames() {
return EVENT_NAMES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
public class InputPhoneRenderer extends InputRenderer {

private static final Logger LOGGER = Logger.getLogger(InputPhoneRenderer.class.getName());
private static final String HIDDEN_ID = "_iso2";
private static final String HIDDEN_ID = "_hidden";
private static final String ISO2_ID = "_iso2";

@Override
public void decode(final FacesContext context, final UIComponent component) {
Expand All @@ -67,7 +68,7 @@ public void decode(final FacesContext context, final UIComponent component) {

decodeBehaviors(context, inputPhone);

final String inputId = inputPhone.getClientId(context) + "_input";
final String inputId = inputPhone.getClientId(context) + HIDDEN_ID;
final String submittedValue = context.getExternalContext().getRequestParameterMap().get(inputId);

if (submittedValue != null) {
Expand Down Expand Up @@ -105,7 +106,7 @@ public Object getConvertedValue(final FacesContext context, final UIComponent co
}

String country = context.getExternalContext().getRequestParameterMap()
.get(inputPhone.getClientId() + HIDDEN_ID);
.get(inputPhone.getClientId() + ISO2_ID);
if (country == null || InputPhone.COUNTRY_AUTO.equals(country)) {
country = Constants.EMPTY_STRING;
}
Expand Down Expand Up @@ -137,7 +138,7 @@ protected void encodeMarkup(final FacesContext context, final InputPhone inputPh
}

encodeInput(context, inputPhone, clientId, valueToRender);
encodeHiddenInput(context, inputPhone, clientId);
encodeHiddenInputs(context, inputPhone, clientId, valueToRender);

writer.endElement("span");
}
Expand All @@ -147,7 +148,7 @@ protected void encodeInput(final FacesContext context, final InputPhone inputPho
throws IOException {

final ResponseWriter writer = context.getResponseWriter();
final String inputId = clientId + "_input";
final String inputId = clientId + InputPhone.INPUT_SUFFIX;
final String inputStyle = inputPhone.getInputStyle();

writer.startElement("input", null);
Expand All @@ -169,9 +170,10 @@ protected void encodeInput(final FacesContext context, final InputPhone inputPho
writer.endElement("input");
}

protected void encodeHiddenInput(final FacesContext context, final InputPhone inputPhone, final String clientId)
protected void encodeHiddenInputs(final FacesContext context, final InputPhone inputPhone, final String clientId, final String valueToRender)
throws IOException {
renderHiddenInput(context, clientId + HIDDEN_ID, inputPhone.getInitialCountry(), inputPhone.isDisabled());
renderHiddenInput(context, clientId + ISO2_ID, inputPhone.getInitialCountry(), inputPhone.isDisabled());
renderHiddenInput(context, clientId + HIDDEN_ID, valueToRender, inputPhone.isDisabled());
}

protected void encodeScript(final FacesContext context, final InputPhone inputPhone) throws IOException {
Expand Down Expand Up @@ -204,13 +206,11 @@ protected void encodeScript(final FacesContext context, final InputPhone inputPh
}
encodeCountries(wb, "preferredCountries", inputPhone.getPreferredCountries());

if (inputPhone.isUtilsScriptRequired()) {
wb.attr("utilsScript",
context.getApplication()
.getResourceHandler()
.createResource("inputphone/utils.js", "primefaces-extensions")
.getRequestPath());
}
wb.attr("utilsScript",
context.getApplication()
.getResourceHandler()
.createResource("inputphone/utils.js", "primefaces-extensions")
.getRequestPath());
if (inputPhone.getLocalizedCountries() != null) {
wb.nativeAttr("i18n", objectToJsonString(inputPhone.getLocalizedCountries()));
}
Expand Down Expand Up @@ -246,4 +246,4 @@ private String objectToJsonString(final Object object) {
return jsonObj.toString();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PrimeFaces.widget.ExtInputPhone = PrimeFaces.widget.BaseWidget.extend({
// JQuery inputs
this.inputJq = $(this.jqId + '_input');
this.inputIso2Jq = $(this.jqId + '_iso2');
this.inputHiddenJq = $(this.jqId + '_hidden');

// pfs metadata
this.inputJq.data(PrimeFaces.CLIENT_ID_DATA, this.id);
Expand All @@ -30,6 +31,7 @@ PrimeFaces.widget.ExtInputPhone = PrimeFaces.widget.BaseWidget.extend({
this.inputJq.attr("disabled", "disabled");
this.inputJq.addClass("ui-state-disabled");
this.inputIso2Jq.attr("disabled", "disabled");
this.inputHiddenJq.attr("disabled", "disabled");
}

// visual effects
Expand All @@ -48,6 +50,7 @@ PrimeFaces.widget.ExtInputPhone = PrimeFaces.widget.BaseWidget.extend({
this.input.addEventListener('countrychange', function () {
var country = $this.iti.getSelectedCountryData();
$this.inputIso2Jq.val(country.iso2);
$this.inputHiddenJq.val($this.getNumber());
if ($this.hasBehavior('countrySelect')) {
var ext = {
params: [{
Expand All @@ -64,6 +67,9 @@ PrimeFaces.widget.ExtInputPhone = PrimeFaces.widget.BaseWidget.extend({
$this.callBehavior('countrySelect', ext);
}
});
this.input.addEventListener('input', function () {
$this.inputHiddenJq.val($this.getNumber());
});
},

/**
Expand Down Expand Up @@ -171,6 +177,7 @@ PrimeFaces.widget.ExtInputPhone = PrimeFaces.widget.BaseWidget.extend({
enable: function () {
PrimeFaces.utils.enableInputWidget(this.inputJq);
PrimeFaces.utils.enableInputWidget(this.inputIso2Jq);
PrimeFaces.utils.enableInputWidget(this.inputHiddenJq);
this.disabled = false;
},

Expand All @@ -180,6 +187,7 @@ PrimeFaces.widget.ExtInputPhone = PrimeFaces.widget.BaseWidget.extend({
disable: function () {
PrimeFaces.utils.disableInputWidget(this.inputJq);
PrimeFaces.utils.disableInputWidget(this.inputIso2Jq);
PrimeFaces.utils.disableInputWidget(this.inputHiddenJq);
this.disabled = true;
},

Expand All @@ -199,4 +207,4 @@ PrimeFaces.widget.ExtInputPhone = PrimeFaces.widget.BaseWidget.extend({
}
}

});
});

0 comments on commit f51c9a7

Please sign in to comment.