@insticc/genericform 2.1.0 β†’ 2.1.1

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.
@@ -15,45 +15,18 @@ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r)
15
15
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
16
16
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
17
17
  function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
18
- var COUNTRY_CODES = [{
19
- label: "πŸ‡΅πŸ‡Ή +351",
20
- value: "+351"
21
- }, {
22
- label: "πŸ‡ͺπŸ‡Έ +34",
23
- value: "+34"
24
- }, {
25
- label: "πŸ‡¬πŸ‡§ +44",
26
- value: "+44"
27
- }, {
28
- label: "πŸ‡ΊπŸ‡Έ +1",
29
- value: "+1"
30
- }, {
31
- label: "πŸ‡«πŸ‡· +33",
32
- value: "+33"
33
- }, {
34
- label: "πŸ‡©πŸ‡ͺ +49",
35
- value: "+49"
36
- }
37
- // ...
38
- ];
39
-
40
- /**
41
- * Mobile phone input with country code selector.
42
- * value format: "+351 916999999"
43
- * onChange(value, name) β€” same signature as other custom inputs.
44
- */
45
18
  var CustomMobilePhone = function CustomMobilePhone(_ref) {
46
19
  var onChange = _ref.onChange,
47
20
  name = _ref.name,
48
21
  label = _ref.label,
49
22
  _ref$value = _ref.value,
50
- value = _ref$value === void 0 ? "" : _ref$value,
23
+ externalValue = _ref$value === void 0 ? "" : _ref$value,
51
24
  _ref$disabled = _ref.disabled,
52
25
  disabled = _ref$disabled === void 0 ? false : _ref$disabled,
53
26
  _ref$required = _ref.required,
54
27
  required = _ref$required === void 0 ? false : _ref$required,
55
28
  _ref$placeholder = _ref.placeholder,
56
- placeholder = _ref$placeholder === void 0 ? "912 345 678" : _ref$placeholder,
29
+ placeholder = _ref$placeholder === void 0 ? "+351 912345678" : _ref$placeholder,
57
30
  error = _ref.error,
58
31
  _ref$tooltip = _ref.tooltip,
59
32
  tooltip = _ref$tooltip === void 0 ? "" : _ref$tooltip,
@@ -70,33 +43,33 @@ var CustomMobilePhone = function CustomMobilePhone(_ref) {
70
43
  _ref$inputStyle = _ref.inputStyle,
71
44
  inputStyle = _ref$inputStyle === void 0 ? {} : _ref$inputStyle,
72
45
  _ref$tooltipStyle = _ref.tooltipStyle,
73
- tooltipStyle = _ref$tooltipStyle === void 0 ? {} : _ref$tooltipStyle,
74
- _ref$countryCodes = _ref.countryCodes,
75
- countryCodes = _ref$countryCodes === void 0 ? COUNTRY_CODES : _ref$countryCodes,
76
- _ref$defaultCountryCo = _ref.defaultCountryCode,
77
- defaultCountryCode = _ref$defaultCountryCo === void 0 ? "+351" : _ref$defaultCountryCo;
78
- // Split stored value into prefix + local number
79
- var _ref2 = function () {
80
- if (!value) return [defaultCountryCode, ""];
81
- var found = countryCodes.find(function (c) {
82
- return value.startsWith(c.value);
83
- });
84
- if (found) return [found.value, value.slice(found.value.length)];
85
- return [defaultCountryCode, value];
86
- }(),
87
- _ref3 = _slicedToArray(_ref2, 2),
88
- prefix = _ref3[0],
89
- localNumber = _ref3[1];
90
- var handlePrefixChange = (0, _react.useCallback)(function (e) {
91
- return onChange === null || onChange === void 0 ? void 0 : onChange(name, "".concat(e.target.value).concat(localNumber));
92
- }, [onChange, name, localNumber]);
93
- var handleNumberChange = (0, _react.useCallback)(function (e) {
94
- var digits = e.target.value.replace(/[^\d]/g, "");
95
- onChange === null || onChange === void 0 || onChange(name, "".concat(prefix).concat(digits));
96
- }, [onChange, name, prefix]);
46
+ tooltipStyle = _ref$tooltipStyle === void 0 ? {} : _ref$tooltipStyle;
47
+ var _useState = (0, _react.useState)(externalValue),
48
+ _useState2 = _slicedToArray(_useState, 2),
49
+ localValue = _useState2[0],
50
+ setLocalValue = _useState2[1];
51
+
52
+ // Sync with external value
53
+ (0, _react.useEffect)(function () {
54
+ setLocalValue(externalValue);
55
+ }, [externalValue]);
56
+ var handleChange = (0, _react.useCallback)(function (e) {
57
+ var rawValue = e.target.value;
58
+
59
+ // Auto-add "+" if user starts typing digits without it
60
+ var newValue = rawValue;
61
+ if (rawValue && !rawValue.startsWith('+')) {
62
+ var digits = rawValue.replace(/\D/g, '');
63
+ newValue = "+".concat(digits);
64
+ } else {
65
+ // Keep existing + and clean other characters
66
+ newValue = rawValue.replace(/[^\d+]/g, '');
67
+ }
68
+ setLocalValue(newValue);
69
+ onChange === null || onChange === void 0 || onChange(name, newValue);
70
+ }, [onChange, name]);
97
71
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
98
72
  className: "ufg-field-group",
99
- id: "form-telDiv-".concat(name),
100
73
  style: mainDivStyle,
101
74
  children: [(0, _defaults.labelJsx)({
102
75
  name: name,
@@ -106,37 +79,20 @@ var CustomMobilePhone = function CustomMobilePhone(_ref) {
106
79
  spanStyle: spanStyle,
107
80
  showHelp: showHelp,
108
81
  helpText: helpText
109
- }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
110
- style: {
111
- display: "flex",
112
- gap: "8px"
113
- },
114
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("select", {
115
- value: prefix,
116
- disabled: disabled,
117
- onChange: handlePrefixChange,
118
- className: "ufg-input".concat(error ? " error" : ""),
119
- style: {
120
- width: "110px",
121
- flexShrink: 0
122
- },
123
- children: countryCodes.map(function (c) {
124
- return /*#__PURE__*/(0, _jsxRuntime.jsx)("option", {
125
- value: c.value,
126
- children: c.label
127
- }, c.value);
128
- })
129
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
130
- id: "form-input-".concat(name),
131
- type: "tel",
132
- value: localNumber,
133
- disabled: disabled,
134
- required: required,
135
- placeholder: placeholder,
136
- onChange: handleNumberChange,
137
- className: "ufg-input".concat(error ? " error" : ""),
138
- style: inputStyle
139
- })]
82
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
83
+ id: "form-input-".concat(name),
84
+ type: "tel",
85
+ value: localValue,
86
+ disabled: disabled,
87
+ required: required,
88
+ placeholder: placeholder,
89
+ onChange: handleChange,
90
+ className: "ufg-input".concat(error ? " error" : ""),
91
+ style: inputStyle,
92
+ inputMode: "numeric",
93
+ pattern: "[0-9+]*",
94
+ autoComplete: "tel",
95
+ title: "Phone number (auto-adds + prefix)"
140
96
  }), (0, _defaults.errorJsx)(error), (0, _defaults.tooltipJsx)(tooltip, tooltipStyle)]
141
97
  });
142
98
  };
@@ -156,11 +112,6 @@ CustomMobilePhone.propTypes = {
156
112
  labelStyle: _propTypes["default"].object,
157
113
  spanStyle: _propTypes["default"].object,
158
114
  inputStyle: _propTypes["default"].object,
159
- tooltipStyle: _propTypes["default"].object,
160
- countryCodes: _propTypes["default"].arrayOf(_propTypes["default"].shape({
161
- label: _propTypes["default"].string.isRequired,
162
- value: _propTypes["default"].string.isRequired
163
- })),
164
- defaultCountryCode: _propTypes["default"].string
115
+ tooltipStyle: _propTypes["default"].object
165
116
  };
166
117
  var _default = exports["default"] = CustomMobilePhone;
package/build/index.js CHANGED
@@ -615,9 +615,7 @@ var GenericForm = function GenericForm(_ref) {
615
615
  case 'tel':
616
616
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomMobilePhone["default"], _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, cp), sp), fp), {}, {
617
617
  value: value,
618
- onChange: fieldHandler,
619
- countryCodes: field.countryCodes,
620
- defaultCountryCode: field.defaultCountryCode
618
+ onChange: fieldHandler(field)
621
619
  }), field.name);
622
620
 
623
621
  // Date
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@insticc/genericform",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "A generic form with a comprehensive set of field types, validation, and external state management",
5
5
  "main": "build/index.js",
6
6
  "scripts": {