@elastic/eui 116.3.1-snapshot.1782222821728 → 116.4.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.
Files changed (38) hide show
  1. package/es/components/date_picker/date_picker.js +1 -1
  2. package/es/components/form/field_password/field_password.styles.js +1 -1
  3. package/es/components/form/field_search/field_search.styles.js +1 -1
  4. package/es/components/form/form.styles.js +1 -1
  5. package/es/components/form/select/select.styles.js +1 -1
  6. package/es/components/form/text_area/text_area.styles.js +1 -1
  7. package/es/components/form/validatable_control/validatable_control.js +55 -8
  8. package/eui.d.ts +48 -54
  9. package/i18ntokens.json +970 -988
  10. package/lib/components/date_picker/date_picker.js +1 -1
  11. package/lib/components/form/field_password/field_password.styles.js +1 -1
  12. package/lib/components/form/field_search/field_search.styles.js +1 -1
  13. package/lib/components/form/form.styles.js +1 -1
  14. package/lib/components/form/select/select.styles.js +1 -1
  15. package/lib/components/form/text_area/text_area.styles.js +1 -1
  16. package/lib/components/form/validatable_control/validatable_control.js +52 -7
  17. package/optimize/es/components/date_picker/date_picker.js +1 -1
  18. package/optimize/es/components/form/field_password/field_password.styles.js +1 -1
  19. package/optimize/es/components/form/field_search/field_search.styles.js +1 -1
  20. package/optimize/es/components/form/form.styles.js +1 -1
  21. package/optimize/es/components/form/select/select.styles.js +1 -1
  22. package/optimize/es/components/form/text_area/text_area.styles.js +1 -1
  23. package/optimize/es/components/form/validatable_control/validatable_control.js +48 -7
  24. package/optimize/lib/components/date_picker/date_picker.js +1 -1
  25. package/optimize/lib/components/form/field_password/field_password.styles.js +1 -1
  26. package/optimize/lib/components/form/field_search/field_search.styles.js +1 -1
  27. package/optimize/lib/components/form/form.styles.js +1 -1
  28. package/optimize/lib/components/form/select/select.styles.js +1 -1
  29. package/optimize/lib/components/form/text_area/text_area.styles.js +1 -1
  30. package/optimize/lib/components/form/validatable_control/validatable_control.js +49 -6
  31. package/package.json +2 -3
  32. package/test-env/components/date_picker/date_picker.js +1 -1
  33. package/test-env/components/form/field_password/field_password.styles.js +1 -1
  34. package/test-env/components/form/field_search/field_search.styles.js +1 -1
  35. package/test-env/components/form/form.styles.js +1 -1
  36. package/test-env/components/form/select/select.styles.js +1 -1
  37. package/test-env/components/form/text_area/text_area.styles.js +1 -1
  38. package/test-env/components/form/validatable_control/validatable_control.js +48 -6
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.useEuiValidatableControl = exports.EuiValidatableControl = void 0;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
8
9
  var _react = require("react");
9
10
  var _propTypes = _interopRequireDefault(require("prop-types"));
10
11
  /*
@@ -15,22 +16,41 @@ var _propTypes = _interopRequireDefault(require("prop-types"));
15
16
  * Side Public License, v 1.
16
17
  */
17
18
 
19
+ function isMutableRef(ref) {
20
+ return ref != null && ref.hasOwnProperty('current');
21
+ }
22
+
18
23
  /**
19
24
  * The `EuiValidatableControl` component should be used in scenarios where
20
25
  * we can render the validated `<input>` as its direct child.
21
- *
22
- * It flags the underlying control as invalid via `aria-invalid`, which EUI form
23
- * controls use to render their invalid styling. Note that it intentionally does
24
- * *not* hook into the native constraint validation API (`setCustomValidity`) -
25
- * `isInvalid` is a presentational prop, and tying it to native validity would
26
- * block native form submission and surface a non-localized native error tooltip.
27
26
  */
28
27
 
29
28
  var EuiValidatableControl = exports.EuiValidatableControl = function EuiValidatableControl(_ref) {
30
29
  var isInvalid = _ref.isInvalid,
31
30
  children = _ref.children;
31
+ // Note that this must be state and not a ref to cause a rerender/set invalid state on initial mount
32
+ var _useState = (0, _react.useState)(null),
33
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
34
+ control = _useState2[0],
35
+ setControl = _useState2[1];
32
36
  var child = _react.Children.only(children);
37
+ var childRef = child.ref;
38
+ var replacedRef = (0, _react.useCallback)(function (element) {
39
+ setControl(element);
40
+
41
+ // Call the original ref, if any
42
+ if (typeof childRef === 'function') {
43
+ childRef(element);
44
+ } else if (isMutableRef(childRef)) {
45
+ childRef.current = element;
46
+ }
47
+ }, [childRef]);
48
+ useSetControlValidity({
49
+ controlEl: control,
50
+ isInvalid: isInvalid
51
+ });
33
52
  return /*#__PURE__*/(0, _react.cloneElement)(child, {
53
+ ref: replacedRef,
34
54
  'aria-invalid': isInvalid || child.props['aria-invalid']
35
55
  });
36
56
  };
@@ -53,6 +73,10 @@ EuiValidatableControl.propTypes = {
53
73
  var useEuiValidatableControl = exports.useEuiValidatableControl = function useEuiValidatableControl(_ref2) {
54
74
  var isInvalid = _ref2.isInvalid,
55
75
  controlEl = _ref2.controlEl;
76
+ useSetControlValidity({
77
+ controlEl: controlEl,
78
+ isInvalid: isInvalid
79
+ });
56
80
  (0, _react.useEffect)(function () {
57
81
  if (!controlEl) return;
58
82
  if (typeof isInvalid === 'boolean') {
@@ -61,4 +85,22 @@ var useEuiValidatableControl = exports.useEuiValidatableControl = function useEu
61
85
  controlEl.removeAttribute('aria-invalid');
62
86
  }
63
87
  }, [isInvalid, controlEl]);
88
+ };
89
+
90
+ /**
91
+ * Internal `setCustomValidity` helper
92
+ */
93
+ var useSetControlValidity = function useSetControlValidity(_ref3) {
94
+ var controlEl = _ref3.controlEl,
95
+ isInvalid = _ref3.isInvalid;
96
+ (0, _react.useEffect)(function () {
97
+ if (controlEl == null || typeof controlEl.setCustomValidity !== 'function') {
98
+ return;
99
+ }
100
+ if (isInvalid) {
101
+ controlEl.setCustomValidity('Invalid');
102
+ } else {
103
+ controlEl.setCustomValidity('');
104
+ }
105
+ }, [isInvalid, controlEl]);
64
106
  };