@popmenu/common-ui 0.125.0 → 0.126.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -1183,6 +1183,13 @@ var useStyles$2 = core.makeStyles(function () { return ({
1183
1183
  gridGap: 16,
1184
1184
  },
1185
1185
  }); });
1186
+ var northAmericanPhoneRegex = /^(\()?[2-9]{1}\d{2}(\))?[-\s.]?[2-9]{1}\d{2}[-\s.]?\d{4}$/;
1187
+ var ukPhoneRegex = /^((\+?44\s?|0044\s?)(\s?\d{3,5}|\(\d{3,5}\))(\s?\d{3,4})(\s?\d{3,4})$|^07\d{9}$)/;
1188
+ var validatePhoneNumber = function (phone) {
1189
+ if (!phone)
1190
+ return false;
1191
+ return northAmericanPhoneRegex.test(phone) || ukPhoneRegex.test(phone);
1192
+ };
1186
1193
  var FollowerAuthenticationForm = function () {
1187
1194
  var _a = useFollowerAuthenticationDialogContext(), state = _a.state, messages = _a.messages, locationOptions = _a.locationOptions, onActionButtonClick = _a.onActionButtonClick, loading = _a.loading;
1188
1195
  var classes = useStyles$2();
@@ -1197,24 +1204,33 @@ var FollowerAuthenticationForm = function () {
1197
1204
  var checkFieldValidity = function (e) {
1198
1205
  var _a, _b;
1199
1206
  var fieldName = e.target.name;
1200
- var fieldValidity = (_b = (_a = formRef.current) === null || _a === void 0 ? void 0 : _a.elements.namedItem(fieldName)) === null || _b === void 0 ? void 0 : _b.validity.valid;
1207
+ var fieldValue = e.target.value;
1201
1208
  switch (fieldName) {
1202
1209
  case 'phone':
1203
- setSignUpPhoneValue(e.target.value);
1204
- setShowPhoneErrorText(!fieldValidity);
1210
+ var isPhoneValid = validatePhoneNumber(fieldValue);
1211
+ setSignUpPhoneValue(fieldValue);
1212
+ setShowPhoneErrorText(!isPhoneValid && fieldValue.length > 0);
1205
1213
  break;
1206
1214
  case 'email':
1207
- setSignUpEmailValue(e.target.value);
1208
- setShowEmailErrorText(!fieldValidity);
1215
+ var emailField = (_a = formRef.current) === null || _a === void 0 ? void 0 : _a.elements.namedItem(fieldName);
1216
+ var isEmailValid = (_b = emailField === null || emailField === void 0 ? void 0 : emailField.validity.valid) !== null && _b !== void 0 ? _b : false;
1217
+ setSignUpEmailValue(fieldValue);
1218
+ setShowEmailErrorText(!isEmailValid && fieldValue.length > 0);
1209
1219
  break;
1210
1220
  case 'location':
1211
- setShowLocationErrorText(!e.target.value);
1221
+ setShowLocationErrorText(!fieldValue);
1212
1222
  break;
1213
1223
  }
1214
1224
  };
1215
1225
  React.useEffect(function () {
1216
- setFormValidity(['phone', 'email', 'location'].every(function (fieldName) { var _a, _b; return (_b = (_a = formRef.current) === null || _a === void 0 ? void 0 : _a.elements.namedItem(fieldName)) === null || _b === void 0 ? void 0 : _b.validity.valid; }));
1217
- }, [showPhoneErrorText, showEmailErrorText, showLocationErrorText, signUpPhoneValue, signUpEmailValue]);
1226
+ var _a;
1227
+ var hasValidPhone = Boolean(signUpPhoneValue) && !showPhoneErrorText;
1228
+ var hasValidEmail = Boolean(signUpEmailValue) && !showEmailErrorText;
1229
+ var locationField = (_a = formRef.current) === null || _a === void 0 ? void 0 : _a.elements.namedItem('location');
1230
+ var hasValidLocation = locationField ? Boolean(locationField.value) : true;
1231
+ var hasValidContact = hasValidPhone || hasValidEmail;
1232
+ setFormValidity(hasValidContact && hasValidLocation);
1233
+ }, [signUpPhoneValue, signUpEmailValue, showPhoneErrorText, showEmailErrorText, showLocationErrorText]);
1218
1234
  var handleSubmit = function (e) {
1219
1235
  e.preventDefault();
1220
1236
  var form = e.target;
@@ -1237,7 +1253,6 @@ var FollowerAuthenticationForm = function () {
1237
1253
  e.preventDefault();
1238
1254
  } }),
1239
1255
  React__default['default'].createElement(core.TextField, { disabled: loading, label: messages.signUpPhoneInputLabel, key: "phone-input", id: "phone-input", name: "phone", onChange: checkFieldValidity, error: showPhoneErrorText, helperText: showPhoneErrorText && messages.signUpPhoneErrorText, inputProps: {
1240
- pattern: '^[0-9]{10}$',
1241
1256
  required: signUpEmailValue ? false : true,
1242
1257
  }, defaultValue: state.context.phone }),
1243
1258
  React__default['default'].createElement(core.TextField, { disabled: loading, label: messages.signUpNameInputLabel, key: "name-input", id: "name-input", name: "name", inputProps: { required: true } }),