@wordpress/components 30.1.1-next.46f643fa0.0 → 30.2.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 (211) hide show
  1. package/CHANGELOG.md +20 -4
  2. package/build/custom-gradient-picker/serializer.js +14 -0
  3. package/build/custom-gradient-picker/serializer.js.map +1 -1
  4. package/build/custom-gradient-picker/utils.js +12 -0
  5. package/build/custom-gradient-picker/utils.js.map +1 -1
  6. package/build/date-time/date/index.js +2 -4
  7. package/build/date-time/date/index.js.map +1 -1
  8. package/build/date-time/date/styles.js +41 -20
  9. package/build/date-time/date/styles.js.map +1 -1
  10. package/build/menu-item/index.js +1 -0
  11. package/build/menu-item/index.js.map +1 -1
  12. package/build/validated-form-controls/components/checkbox-control.js +5 -3
  13. package/build/validated-form-controls/components/checkbox-control.js.map +1 -1
  14. package/build/validated-form-controls/components/combobox-control.js +5 -3
  15. package/build/validated-form-controls/components/combobox-control.js.map +1 -1
  16. package/build/validated-form-controls/components/custom-select-control.js +5 -3
  17. package/build/validated-form-controls/components/custom-select-control.js.map +1 -1
  18. package/build/validated-form-controls/components/input-control.js +5 -3
  19. package/build/validated-form-controls/components/input-control.js.map +1 -1
  20. package/build/validated-form-controls/components/number-control.js +5 -3
  21. package/build/validated-form-controls/components/number-control.js.map +1 -1
  22. package/build/validated-form-controls/components/radio-control.js +5 -3
  23. package/build/validated-form-controls/components/radio-control.js.map +1 -1
  24. package/build/validated-form-controls/components/range-control.js +5 -3
  25. package/build/validated-form-controls/components/range-control.js.map +1 -1
  26. package/build/validated-form-controls/components/select-control.js +5 -3
  27. package/build/validated-form-controls/components/select-control.js.map +1 -1
  28. package/build/validated-form-controls/components/text-control.js +5 -3
  29. package/build/validated-form-controls/components/text-control.js.map +1 -1
  30. package/build/validated-form-controls/components/textarea-control.js +5 -3
  31. package/build/validated-form-controls/components/textarea-control.js.map +1 -1
  32. package/build/validated-form-controls/components/toggle-control.js +5 -3
  33. package/build/validated-form-controls/components/toggle-control.js.map +1 -1
  34. package/build/validated-form-controls/components/toggle-group-control.js +5 -3
  35. package/build/validated-form-controls/components/toggle-group-control.js.map +1 -1
  36. package/build/validated-form-controls/components/types.js.map +1 -1
  37. package/build/validated-form-controls/control-with-error.js +57 -22
  38. package/build/validated-form-controls/control-with-error.js.map +1 -1
  39. package/build/validated-form-controls/validity-indicator.js +45 -0
  40. package/build/validated-form-controls/validity-indicator.js.map +1 -0
  41. package/build-module/custom-gradient-picker/serializer.js +14 -0
  42. package/build-module/custom-gradient-picker/serializer.js.map +1 -1
  43. package/build-module/custom-gradient-picker/utils.js +12 -0
  44. package/build-module/custom-gradient-picker/utils.js.map +1 -1
  45. package/build-module/date-time/date/index.js +3 -4
  46. package/build-module/date-time/date/index.js.map +1 -1
  47. package/build-module/date-time/date/styles.js +39 -14
  48. package/build-module/date-time/date/styles.js.map +1 -1
  49. package/build-module/menu-item/index.js +1 -0
  50. package/build-module/menu-item/index.js.map +1 -1
  51. package/build-module/validated-form-controls/components/checkbox-control.js +5 -3
  52. package/build-module/validated-form-controls/components/checkbox-control.js.map +1 -1
  53. package/build-module/validated-form-controls/components/combobox-control.js +5 -3
  54. package/build-module/validated-form-controls/components/combobox-control.js.map +1 -1
  55. package/build-module/validated-form-controls/components/custom-select-control.js +5 -3
  56. package/build-module/validated-form-controls/components/custom-select-control.js.map +1 -1
  57. package/build-module/validated-form-controls/components/input-control.js +5 -3
  58. package/build-module/validated-form-controls/components/input-control.js.map +1 -1
  59. package/build-module/validated-form-controls/components/number-control.js +5 -3
  60. package/build-module/validated-form-controls/components/number-control.js.map +1 -1
  61. package/build-module/validated-form-controls/components/radio-control.js +5 -3
  62. package/build-module/validated-form-controls/components/radio-control.js.map +1 -1
  63. package/build-module/validated-form-controls/components/range-control.js +5 -3
  64. package/build-module/validated-form-controls/components/range-control.js.map +1 -1
  65. package/build-module/validated-form-controls/components/select-control.js +5 -3
  66. package/build-module/validated-form-controls/components/select-control.js.map +1 -1
  67. package/build-module/validated-form-controls/components/text-control.js +5 -3
  68. package/build-module/validated-form-controls/components/text-control.js.map +1 -1
  69. package/build-module/validated-form-controls/components/textarea-control.js +5 -3
  70. package/build-module/validated-form-controls/components/textarea-control.js.map +1 -1
  71. package/build-module/validated-form-controls/components/toggle-control.js +5 -3
  72. package/build-module/validated-form-controls/components/toggle-control.js.map +1 -1
  73. package/build-module/validated-form-controls/components/toggle-group-control.js +5 -3
  74. package/build-module/validated-form-controls/components/toggle-group-control.js.map +1 -1
  75. package/build-module/validated-form-controls/components/types.js.map +1 -1
  76. package/build-module/validated-form-controls/control-with-error.js +57 -21
  77. package/build-module/validated-form-controls/control-with-error.js.map +1 -1
  78. package/build-module/validated-form-controls/validity-indicator.js +37 -0
  79. package/build-module/validated-form-controls/validity-indicator.js.map +1 -0
  80. package/build-style/style-rtl.css +39 -29
  81. package/build-style/style.css +39 -29
  82. package/build-types/confirm-dialog/stories/index.story.d.ts.map +1 -1
  83. package/build-types/custom-gradient-picker/serializer.d.ts.map +1 -1
  84. package/build-types/custom-gradient-picker/utils.d.ts.map +1 -1
  85. package/build-types/date-time/date/index.d.ts.map +1 -1
  86. package/build-types/date-time/date/styles.d.ts +6 -0
  87. package/build-types/date-time/date/styles.d.ts.map +1 -1
  88. package/build-types/divider/stories/index.story.d.ts.map +1 -1
  89. package/build-types/elevation/stories/index.story.d.ts.map +1 -1
  90. package/build-types/form-token-field/stories/index.story.d.ts +10 -5
  91. package/build-types/form-token-field/stories/index.story.d.ts.map +1 -1
  92. package/build-types/gradient-picker/stories/index.story.d.ts +2 -1
  93. package/build-types/gradient-picker/stories/index.story.d.ts.map +1 -1
  94. package/build-types/grid/stories/index.story.d.ts.map +1 -1
  95. package/build-types/h-stack/stories/index.story.d.ts.map +1 -1
  96. package/build-types/heading/stories/index.story.d.ts.map +1 -1
  97. package/build-types/input-control/stories/index.story.d.ts.map +1 -1
  98. package/build-types/item-group/stories/index.story.d.ts.map +1 -1
  99. package/build-types/menu-item/index.d.ts.map +1 -1
  100. package/build-types/number-control/stories/index.story.d.ts.map +1 -1
  101. package/build-types/scrollable/stories/index.story.d.ts.map +1 -1
  102. package/build-types/spacer/stories/index.story.d.ts.map +1 -1
  103. package/build-types/surface/stories/index.story.d.ts.map +1 -1
  104. package/build-types/text/stories/index.story.d.ts.map +1 -1
  105. package/build-types/toggle-group-control/stories/index.story.d.ts.map +1 -1
  106. package/build-types/tools-panel/stories/index.story.d.ts.map +1 -1
  107. package/build-types/tree-grid/stories/index.story.d.ts.map +1 -1
  108. package/build-types/truncate/stories/index.story.d.ts.map +1 -1
  109. package/build-types/unit-control/stories/index.story.d.ts.map +1 -1
  110. package/build-types/v-stack/stories/index.story.d.ts.map +1 -1
  111. package/build-types/validated-form-controls/components/checkbox-control.d.ts.map +1 -1
  112. package/build-types/validated-form-controls/components/combobox-control.d.ts.map +1 -1
  113. package/build-types/validated-form-controls/components/custom-select-control.d.ts.map +1 -1
  114. package/build-types/validated-form-controls/components/input-control.d.ts.map +1 -1
  115. package/build-types/validated-form-controls/components/number-control.d.ts.map +1 -1
  116. package/build-types/validated-form-controls/components/radio-control.d.ts.map +1 -1
  117. package/build-types/validated-form-controls/components/range-control.d.ts.map +1 -1
  118. package/build-types/validated-form-controls/components/select-control.d.ts.map +1 -1
  119. package/build-types/validated-form-controls/components/stories/checkbox-control.story.d.ts.map +1 -1
  120. package/build-types/validated-form-controls/components/stories/combobox-control.story.d.ts.map +1 -1
  121. package/build-types/validated-form-controls/components/stories/custom-select-control.story.d.ts.map +1 -1
  122. package/build-types/validated-form-controls/components/stories/input-control.story.d.ts.map +1 -1
  123. package/build-types/validated-form-controls/components/stories/number-control.story.d.ts.map +1 -1
  124. package/build-types/validated-form-controls/components/stories/overview.story.d.ts +13 -0
  125. package/build-types/validated-form-controls/components/stories/overview.story.d.ts.map +1 -1
  126. package/build-types/validated-form-controls/components/stories/radio-control.story.d.ts.map +1 -1
  127. package/build-types/validated-form-controls/components/stories/range-control.story.d.ts.map +1 -1
  128. package/build-types/validated-form-controls/components/stories/select-control.story.d.ts.map +1 -1
  129. package/build-types/validated-form-controls/components/stories/text-control.story.d.ts.map +1 -1
  130. package/build-types/validated-form-controls/components/stories/textarea-control.story.d.ts.map +1 -1
  131. package/build-types/validated-form-controls/components/stories/toggle-control.story.d.ts.map +1 -1
  132. package/build-types/validated-form-controls/components/stories/toggle-group-control.story.d.ts.map +1 -1
  133. package/build-types/validated-form-controls/components/text-control.d.ts.map +1 -1
  134. package/build-types/validated-form-controls/components/textarea-control.d.ts.map +1 -1
  135. package/build-types/validated-form-controls/components/toggle-control.d.ts.map +1 -1
  136. package/build-types/validated-form-controls/components/toggle-group-control.d.ts.map +1 -1
  137. package/build-types/validated-form-controls/components/types.d.ts +21 -10
  138. package/build-types/validated-form-controls/components/types.d.ts.map +1 -1
  139. package/build-types/validated-form-controls/control-with-error.d.ts +4 -5
  140. package/build-types/validated-form-controls/control-with-error.d.ts.map +1 -1
  141. package/build-types/validated-form-controls/validity-indicator.d.ts +5 -0
  142. package/build-types/validated-form-controls/validity-indicator.d.ts.map +1 -0
  143. package/build-types/view/stories/index.story.d.ts.map +1 -1
  144. package/build-types/z-stack/stories/index.story.d.ts.map +1 -1
  145. package/package.json +21 -21
  146. package/src/button/style.scss +3 -3
  147. package/src/calendar/style.scss +22 -22
  148. package/src/confirm-dialog/stories/index.story.tsx +3 -2
  149. package/src/custom-gradient-picker/serializer.ts +14 -0
  150. package/src/custom-gradient-picker/test/serializer.ts +25 -0
  151. package/src/custom-gradient-picker/utils.ts +10 -0
  152. package/src/date-time/date/index.tsx +4 -3
  153. package/src/date-time/date/styles.ts +13 -20
  154. package/src/divider/stories/index.story.tsx +2 -1
  155. package/src/elevation/stories/index.story.tsx +2 -1
  156. package/src/form-token-field/stories/index.story.tsx +15 -7
  157. package/src/gradient-picker/stories/index.story.tsx +48 -0
  158. package/src/grid/stories/index.story.tsx +2 -1
  159. package/src/h-stack/stories/e2e/index.story.tsx +1 -1
  160. package/src/h-stack/stories/index.story.tsx +3 -2
  161. package/src/heading/stories/index.story.tsx +3 -2
  162. package/src/input-control/stories/index.story.tsx +3 -2
  163. package/src/item-group/stories/index.story.tsx +2 -1
  164. package/src/menu/stories/index.story.tsx +1 -1
  165. package/src/menu-item/index.tsx +1 -0
  166. package/src/number-control/stories/index.story.tsx +3 -2
  167. package/src/scrollable/stories/index.story.tsx +2 -1
  168. package/src/spacer/stories/index.story.tsx +2 -1
  169. package/src/surface/stories/index.story.tsx +2 -1
  170. package/src/text/stories/index.story.tsx +3 -2
  171. package/src/toggle-group-control/stories/index.story.tsx +3 -2
  172. package/src/tools-panel/stories/index.story.tsx +2 -1
  173. package/src/tree-grid/stories/index.story.tsx +3 -2
  174. package/src/truncate/stories/index.story.tsx +3 -2
  175. package/src/unit-control/stories/index.story.tsx +3 -2
  176. package/src/utils/theme-variables.scss +1 -0
  177. package/src/v-stack/stories/e2e/index.story.tsx +1 -1
  178. package/src/v-stack/stories/index.story.tsx +3 -2
  179. package/src/validated-form-controls/components/checkbox-control.tsx +5 -3
  180. package/src/validated-form-controls/components/combobox-control.tsx +5 -3
  181. package/src/validated-form-controls/components/custom-select-control.tsx +5 -3
  182. package/src/validated-form-controls/components/input-control.tsx +5 -3
  183. package/src/validated-form-controls/components/number-control.tsx +5 -3
  184. package/src/validated-form-controls/components/radio-control.tsx +5 -3
  185. package/src/validated-form-controls/components/range-control.tsx +5 -3
  186. package/src/validated-form-controls/components/select-control.tsx +5 -3
  187. package/src/validated-form-controls/components/stories/checkbox-control.story.tsx +19 -7
  188. package/src/validated-form-controls/components/stories/combobox-control.story.tsx +19 -7
  189. package/src/validated-form-controls/components/stories/custom-select-control.story.tsx +19 -7
  190. package/src/validated-form-controls/components/stories/input-control.story.tsx +53 -19
  191. package/src/validated-form-controls/components/stories/number-control.story.tsx +19 -7
  192. package/src/validated-form-controls/components/stories/overview.mdx +2 -2
  193. package/src/validated-form-controls/components/stories/overview.story.tsx +124 -16
  194. package/src/validated-form-controls/components/stories/radio-control.story.tsx +19 -7
  195. package/src/validated-form-controls/components/stories/range-control.story.tsx +19 -7
  196. package/src/validated-form-controls/components/stories/select-control.story.tsx +19 -7
  197. package/src/validated-form-controls/components/stories/text-control.story.tsx +19 -7
  198. package/src/validated-form-controls/components/stories/textarea-control.story.tsx +19 -7
  199. package/src/validated-form-controls/components/stories/toggle-control.story.tsx +19 -7
  200. package/src/validated-form-controls/components/stories/toggle-group-control.story.tsx +19 -7
  201. package/src/validated-form-controls/components/text-control.tsx +5 -3
  202. package/src/validated-form-controls/components/textarea-control.tsx +5 -3
  203. package/src/validated-form-controls/components/toggle-control.tsx +5 -3
  204. package/src/validated-form-controls/components/toggle-group-control.tsx +5 -3
  205. package/src/validated-form-controls/components/types.ts +21 -12
  206. package/src/validated-form-controls/control-with-error.tsx +77 -26
  207. package/src/validated-form-controls/style.scss +19 -5
  208. package/src/validated-form-controls/validity-indicator.tsx +48 -0
  209. package/src/view/stories/index.story.tsx +2 -1
  210. package/src/z-stack/stories/index.story.tsx +2 -1
  211. package/tsconfig.tsbuildinfo +1 -1
@@ -15,7 +15,8 @@ import { formDecorator } from './story-utils';
15
15
  import { ValidatedRangeControl } from '../range-control';
16
16
 
17
17
  const meta: Meta< typeof ValidatedRangeControl > = {
18
- title: 'Components (Experimental)/Validated Form Controls/ValidatedRangeControl',
18
+ title: 'Components/Selection & Input/Validated Form Controls/ValidatedRangeControl',
19
+ id: 'components-validatedrangecontrol',
19
20
  component: ValidatedRangeControl,
20
21
  tags: [ 'status-private' ],
21
22
  decorators: formDecorator,
@@ -32,6 +33,12 @@ export const Default: StoryObj< typeof ValidatedRangeControl > = {
32
33
  useState<
33
34
  React.ComponentProps< typeof ValidatedRangeControl >[ 'value' ]
34
35
  >();
36
+ const [ customValidity, setCustomValidity ] =
37
+ useState<
38
+ React.ComponentProps<
39
+ typeof ValidatedRangeControl
40
+ >[ 'customValidity' ]
41
+ >( undefined );
35
42
 
36
43
  return (
37
44
  <ValidatedRangeControl
@@ -41,6 +48,17 @@ export const Default: StoryObj< typeof ValidatedRangeControl > = {
41
48
  setValue( newValue );
42
49
  onChange?.( newValue );
43
50
  } }
51
+ onValidate={ ( v ) => {
52
+ if ( v && v % 2 !== 0 ) {
53
+ setCustomValidity( {
54
+ type: 'invalid',
55
+ message: 'Choose an even number.',
56
+ } );
57
+ } else {
58
+ setCustomValidity( undefined );
59
+ }
60
+ } }
61
+ customValidity={ customValidity }
44
62
  />
45
63
  );
46
64
  },
@@ -51,10 +69,4 @@ Default.args = {
51
69
  help: 'Odd numbers are not allowed.',
52
70
  min: 0,
53
71
  max: 20,
54
- customValidator: ( value ) => {
55
- if ( value && value % 2 !== 0 ) {
56
- return 'Choose an even number.';
57
- }
58
- return undefined;
59
- },
60
72
  };
@@ -15,7 +15,8 @@ import { ValidatedSelectControl } from '../select-control';
15
15
  import { formDecorator } from './story-utils';
16
16
 
17
17
  const meta: Meta< typeof ValidatedSelectControl > = {
18
- title: 'Components (Experimental)/Validated Form Controls/ValidatedSelectControl',
18
+ title: 'Components/Selection & Input/Validated Form Controls/ValidatedSelectControl',
19
+ id: 'components-validatedselectcontrol',
19
20
  component: ValidatedSelectControl,
20
21
  tags: [ 'status-private' ],
21
22
  decorators: formDecorator,
@@ -29,6 +30,12 @@ export default meta;
29
30
  export const Default: StoryObj< typeof ValidatedSelectControl > = {
30
31
  render: function Template( { onChange, ...args } ) {
31
32
  const [ value, setValue ] = useState( '' );
33
+ const [ customValidity, setCustomValidity ] =
34
+ useState<
35
+ React.ComponentProps<
36
+ typeof ValidatedSelectControl
37
+ >[ 'customValidity' ]
38
+ >( undefined );
32
39
 
33
40
  return (
34
41
  <ValidatedSelectControl
@@ -38,6 +45,17 @@ export const Default: StoryObj< typeof ValidatedSelectControl > = {
38
45
  setValue( newValue );
39
46
  onChange?.( newValue );
40
47
  } }
48
+ onValidate={ ( v ) => {
49
+ if ( v === '1' ) {
50
+ setCustomValidity( {
51
+ type: 'invalid',
52
+ message: 'Option 1 is not allowed.',
53
+ } );
54
+ } else {
55
+ setCustomValidity( undefined );
56
+ }
57
+ } }
58
+ customValidity={ customValidity }
41
59
  />
42
60
  );
43
61
  },
@@ -51,10 +69,4 @@ Default.args = {
51
69
  { value: '1', label: 'Option 1 (not allowed)' },
52
70
  { value: '2', label: 'Option 2' },
53
71
  ],
54
- customValidator: ( value ) => {
55
- if ( value === '1' ) {
56
- return 'Option 1 is not allowed.';
57
- }
58
- return undefined;
59
- },
60
72
  };
@@ -15,7 +15,8 @@ import { formDecorator } from './story-utils';
15
15
  import { ValidatedTextControl } from '../text-control';
16
16
 
17
17
  const meta: Meta< typeof ValidatedTextControl > = {
18
- title: 'Components (Experimental)/Validated Form Controls/ValidatedTextControl',
18
+ title: 'Components/Selection & Input/Validated Form Controls/ValidatedTextControl',
19
+ id: 'components-validatedtextcontrol',
19
20
  component: ValidatedTextControl,
20
21
  tags: [ 'status-private' ],
21
22
  decorators: formDecorator,
@@ -29,6 +30,12 @@ export default meta;
29
30
  export const Default: StoryObj< typeof ValidatedTextControl > = {
30
31
  render: function Template( { onChange, ...args } ) {
31
32
  const [ value, setValue ] = useState( '' );
33
+ const [ customValidity, setCustomValidity ] =
34
+ useState<
35
+ React.ComponentProps<
36
+ typeof ValidatedTextControl
37
+ >[ 'customValidity' ]
38
+ >( undefined );
32
39
 
33
40
  return (
34
41
  <ValidatedTextControl
@@ -38,6 +45,17 @@ export const Default: StoryObj< typeof ValidatedTextControl > = {
38
45
  setValue( newValue );
39
46
  onChange?.( newValue );
40
47
  } }
48
+ onValidate={ ( v ) => {
49
+ if ( v?.toString().toLowerCase() === 'error' ) {
50
+ setCustomValidity( {
51
+ type: 'invalid',
52
+ message: 'The word "error" is not allowed.',
53
+ } );
54
+ } else {
55
+ setCustomValidity( undefined );
56
+ }
57
+ } }
58
+ customValidity={ customValidity }
41
59
  />
42
60
  );
43
61
  },
@@ -46,10 +64,4 @@ Default.args = {
46
64
  required: true,
47
65
  label: 'Text',
48
66
  help: "The word 'error' will trigger an error.",
49
- customValidator: ( value ) => {
50
- if ( value?.toString().toLowerCase() === 'error' ) {
51
- return 'The word "error" is not allowed.';
52
- }
53
- return undefined;
54
- },
55
67
  };
@@ -14,7 +14,8 @@ import { formDecorator } from './story-utils';
14
14
  import { ValidatedTextareaControl } from '../textarea-control';
15
15
 
16
16
  const meta: Meta< typeof ValidatedTextareaControl > = {
17
- title: 'Components (Experimental)/Validated Form Controls/ValidatedTextareaControl',
17
+ title: 'Components/Selection & Input/Validated Form Controls/ValidatedTextareaControl',
18
+ id: 'components-validatedtextareacontrol',
18
19
  component: ValidatedTextareaControl,
19
20
  tags: [ 'status-private' ],
20
21
  decorators: formDecorator,
@@ -26,6 +27,12 @@ export default meta;
26
27
  export const Default: StoryObj< typeof ValidatedTextareaControl > = {
27
28
  render: function Template( { onChange, ...args } ) {
28
29
  const [ value, setValue ] = useState( '' );
30
+ const [ customValidity, setCustomValidity ] =
31
+ useState<
32
+ React.ComponentProps<
33
+ typeof ValidatedTextareaControl
34
+ >[ 'customValidity' ]
35
+ >( undefined );
29
36
 
30
37
  return (
31
38
  <ValidatedTextareaControl
@@ -35,6 +42,17 @@ export const Default: StoryObj< typeof ValidatedTextareaControl > = {
35
42
  onChange?.( newValue );
36
43
  } }
37
44
  value={ value }
45
+ onValidate={ ( v ) => {
46
+ if ( v?.toLowerCase() === 'error' ) {
47
+ setCustomValidity( {
48
+ type: 'invalid',
49
+ message: 'The word "error" is not allowed.',
50
+ } );
51
+ } else {
52
+ setCustomValidity( undefined );
53
+ }
54
+ } }
55
+ customValidity={ customValidity }
38
56
  />
39
57
  );
40
58
  },
@@ -43,10 +61,4 @@ Default.args = {
43
61
  required: true,
44
62
  label: 'Textarea',
45
63
  help: 'The word "error" will trigger an error.',
46
- customValidator: ( value ) => {
47
- if ( value?.toLowerCase() === 'error' ) {
48
- return 'The word "error" is not allowed.';
49
- }
50
- return undefined;
51
- },
52
64
  };
@@ -15,7 +15,8 @@ import { formDecorator } from './story-utils';
15
15
  import { ValidatedToggleControl } from '../toggle-control';
16
16
 
17
17
  const meta: Meta< typeof ValidatedToggleControl > = {
18
- title: 'Components (Experimental)/Validated Form Controls/ValidatedToggleControl',
18
+ title: 'Components/Selection & Input/Validated Form Controls/ValidatedToggleControl',
19
+ id: 'components-validatedtogglecontrol',
19
20
  component: ValidatedToggleControl,
20
21
  tags: [ 'status-private' ],
21
22
  decorators: formDecorator,
@@ -29,6 +30,12 @@ export default meta;
29
30
  export const Default: StoryObj< typeof ValidatedToggleControl > = {
30
31
  render: function Template( { onChange, ...args } ) {
31
32
  const [ checked, setChecked ] = useState( false );
33
+ const [ customValidity, setCustomValidity ] =
34
+ useState<
35
+ React.ComponentProps<
36
+ typeof ValidatedToggleControl
37
+ >[ 'customValidity' ]
38
+ >( undefined );
32
39
 
33
40
  return (
34
41
  <ValidatedToggleControl
@@ -38,6 +45,17 @@ export const Default: StoryObj< typeof ValidatedToggleControl > = {
38
45
  setChecked( value );
39
46
  onChange?.( value );
40
47
  } }
48
+ onValidate={ ( v ) => {
49
+ if ( v ) {
50
+ setCustomValidity( {
51
+ type: 'invalid',
52
+ message: 'This toggle may not be enabled.',
53
+ } );
54
+ } else {
55
+ setCustomValidity( undefined );
56
+ }
57
+ } }
58
+ customValidity={ customValidity }
41
59
  />
42
60
  );
43
61
  },
@@ -46,10 +64,4 @@ Default.args = {
46
64
  required: true,
47
65
  label: 'Toggle',
48
66
  help: 'This toggle may neither be enabled nor disabled.',
49
- customValidator: ( value ) => {
50
- if ( value ) {
51
- return 'This toggle may not be enabled.';
52
- }
53
- return undefined;
54
- },
55
67
  };
@@ -16,7 +16,8 @@ import { ValidatedToggleGroupControl } from '../toggle-group-control';
16
16
  import { ToggleGroupControlOption } from '../../../toggle-group-control';
17
17
 
18
18
  const meta: Meta< typeof ValidatedToggleGroupControl > = {
19
- title: 'Components (Experimental)/Validated Form Controls/ValidatedToggleGroupControl',
19
+ title: 'Components/Selection & Input/Validated Form Controls/ValidatedToggleGroupControl',
20
+ id: 'components-validatedtogglegroupcontrol',
20
21
  component: ValidatedToggleGroupControl,
21
22
  tags: [ 'status-private' ],
22
23
  decorators: formDecorator,
@@ -35,6 +36,12 @@ export const Default: StoryObj< typeof ValidatedToggleGroupControl > = {
35
36
  typeof ValidatedToggleGroupControl
36
37
  >[ 'value' ]
37
38
  >( '1' );
39
+ const [ customValidity, setCustomValidity ] =
40
+ useState<
41
+ React.ComponentProps<
42
+ typeof ValidatedToggleGroupControl
43
+ >[ 'customValidity' ]
44
+ >( undefined );
38
45
 
39
46
  return (
40
47
  <ValidatedToggleGroupControl
@@ -44,6 +51,17 @@ export const Default: StoryObj< typeof ValidatedToggleGroupControl > = {
44
51
  setValue( newValue );
45
52
  onChange?.( newValue );
46
53
  } }
54
+ onValidate={ ( v ) => {
55
+ if ( v === '2' ) {
56
+ setCustomValidity( {
57
+ type: 'invalid',
58
+ message: 'Option 2 is not allowed.',
59
+ } );
60
+ } else {
61
+ setCustomValidity( undefined );
62
+ }
63
+ } }
64
+ customValidity={ customValidity }
47
65
  />
48
66
  );
49
67
  },
@@ -57,10 +75,4 @@ Default.args = {
57
75
  <ToggleGroupControlOption value="2" key="2" label="Option 2" />,
58
76
  ],
59
77
  help: 'Selecting option 2 will trigger an error.',
60
- customValidator: ( value ) => {
61
- if ( value === '2' ) {
62
- return 'Option 2 is not allowed.';
63
- }
64
- return undefined;
65
- },
66
78
  };
@@ -17,7 +17,8 @@ type Value = TextControlProps[ 'value' ];
17
17
  const UnforwardedValidatedTextControl = (
18
18
  {
19
19
  required,
20
- customValidator,
20
+ onValidate,
21
+ customValidity,
21
22
  onChange,
22
23
  markWhenOptional,
23
24
  ...restProps
@@ -36,9 +37,10 @@ const UnforwardedValidatedTextControl = (
36
37
  <ControlWithError
37
38
  required={ required }
38
39
  markWhenOptional={ markWhenOptional }
39
- customValidator={ () => {
40
- return customValidator?.( valueRef.current );
40
+ onValidate={ () => {
41
+ return onValidate?.( valueRef.current );
41
42
  } }
43
+ customValidity={ customValidity }
42
44
  getValidityTarget={ () => validityTargetRef.current }
43
45
  >
44
46
  <TextControl
@@ -17,7 +17,8 @@ type Value = TextareaControlProps[ 'value' ];
17
17
  const UnforwardedValidatedTextareaControl = (
18
18
  {
19
19
  required,
20
- customValidator,
20
+ onValidate,
21
+ customValidity,
21
22
  onChange,
22
23
  markWhenOptional,
23
24
  ...restProps
@@ -36,9 +37,10 @@ const UnforwardedValidatedTextareaControl = (
36
37
  <ControlWithError
37
38
  required={ required }
38
39
  markWhenOptional={ markWhenOptional }
39
- customValidator={ () => {
40
- return customValidator?.( valueRef.current );
40
+ onValidate={ () => {
41
+ return onValidate?.( valueRef.current );
41
42
  } }
43
+ customValidity={ customValidity }
42
44
  getValidityTarget={ () => validityTargetRef.current }
43
45
  >
44
46
  <TextareaControl
@@ -19,7 +19,8 @@ type Value = ToggleControlProps[ 'checked' ];
19
19
  const UnforwardedValidatedToggleControl = (
20
20
  {
21
21
  required,
22
- customValidator,
22
+ onValidate,
23
+ customValidity,
23
24
  onChange,
24
25
  markWhenOptional,
25
26
  ...restProps
@@ -46,9 +47,10 @@ const UnforwardedValidatedToggleControl = (
46
47
  <ControlWithError
47
48
  required={ required }
48
49
  markWhenOptional={ markWhenOptional }
49
- customValidator={ () => {
50
- return customValidator?.( valueRef.current );
50
+ onValidate={ () => {
51
+ return onValidate?.( valueRef.current );
51
52
  } }
53
+ customValidity={ customValidity }
52
54
  getValidityTarget={ () => validityTargetRef.current }
53
55
  >
54
56
  <ToggleControl
@@ -16,7 +16,8 @@ type Value = ToggleGroupControlProps[ 'value' ];
16
16
  const UnforwardedValidatedToggleGroupControl = (
17
17
  {
18
18
  required,
19
- customValidator,
19
+ onValidate,
20
+ customValidity,
20
21
  onChange,
21
22
  markWhenOptional,
22
23
  ...restProps
@@ -37,9 +38,10 @@ const UnforwardedValidatedToggleGroupControl = (
37
38
  <ControlWithError
38
39
  required={ required }
39
40
  markWhenOptional={ markWhenOptional }
40
- customValidator={ () => {
41
- return customValidator?.( valueRef.current );
41
+ onValidate={ () => {
42
+ return onValidate?.( valueRef.current );
42
43
  } }
44
+ customValidity={ customValidity }
43
45
  getValidityTarget={ () => validityTargetRef.current }
44
46
  >
45
47
  <ToggleGroupControl
@@ -10,19 +10,28 @@ export type ValidatedControlProps< V > = {
10
10
  */
11
11
  markWhenOptional?: boolean;
12
12
  /**
13
- * A function that returns a custom validity message when applicable. This error message will be applied to the
14
- * underlying element using the native [`setCustomValidity()` method](https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity).
15
- * This means the custom validator will be run _in addition_ to any other HTML attribute-based validation, and
16
- * will be prioritized over any existing validity messages dictated by the HTML attributes.
17
- * An empty string or `undefined` return value will clear any existing custom validity message.
13
+ * Optional callback to run when the input should be validated. Use this to set
14
+ * a `customValidity` as necessary.
18
15
  *
19
- * Make sure you don't programatically pass a value (such as an initial value) to the control component
20
- * that fails this validator, because the validator will only run for user-initiated changes.
16
+ * Always prefer using standard HTML attributes like `required` and `min`/`max` over
17
+ * custom validators when possible, as they are simpler and have localized error messages built in.
18
+ */
19
+ onValidate?: ( currentValue: V ) => void;
20
+ /**
21
+ * Show a custom message based on the validation status.
21
22
  *
22
- * Always prefer using standard HTML attributes like `required` and `min`/`max` over custom validators
23
- * when possible, as they are simpler and have localized error messages built in.
23
+ * - When `type` is `invalid`, the message will be applied to the underlying element using the
24
+ * native [`setCustomValidity()` method](https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity).
25
+ * This means the custom message will be prioritized over any existing validity messages
26
+ * triggered by HTML attribute-based validation.
27
+ * - When `type` is `validating` or `valid`, the custom validity message of the underlying element
28
+ * will be cleared. If there are no remaining validity messages triggered by HTML attribute-based validation,
29
+ * the message will be presented as a status indicator rather than an error. These indicators are intended
30
+ * for asynchronous validation calls that may take more than 1 second to complete.
31
+ * Otherwise, custom errors can simply be cleared by setting the `customValidity` prop to `undefined`.
24
32
  */
25
- // TODO: Technically, we could add an optional `customValidity` string prop so the consumer can set
26
- // an error message at any point in time. We should wait until we have a use case though.
27
- customValidator?: ( currentValue: V ) => string | void;
33
+ customValidity?: {
34
+ type: 'validating' | 'valid' | 'invalid';
35
+ message: string;
36
+ };
28
37
  };
@@ -2,7 +2,6 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { __ } from '@wordpress/i18n';
5
- import { error } from '@wordpress/icons';
6
5
 
7
6
  /**
8
7
  * External dependencies
@@ -18,8 +17,8 @@ import {
18
17
  * Internal dependencies
19
18
  */
20
19
  import { withIgnoreIMEEvents } from '../utils/with-ignore-ime-events';
21
-
22
- import Icon from '../icon';
20
+ import type { ValidatedControlProps } from './components/types';
21
+ import { ValidityIndicator } from './validity-indicator';
23
22
 
24
23
  function appendRequiredIndicator(
25
24
  label: React.ReactNode,
@@ -61,7 +60,8 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
61
60
  {
62
61
  required,
63
62
  markWhenOptional,
64
- customValidator,
63
+ onValidate,
64
+ customValidity,
65
65
  getValidityTarget,
66
66
  children,
67
67
  }: {
@@ -74,12 +74,10 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
74
74
  */
75
75
  markWhenOptional?: boolean;
76
76
  /**
77
- * A function that returns a custom validity message when applicable.
78
- *
79
- * This message will be applied to the element returned by `getValidityTarget`.
80
- * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity
77
+ * The callback to run when the input should be validated.
81
78
  */
82
- customValidator?: () => string | void;
79
+ onValidate?: () => void;
80
+ customValidity?: ValidatedControlProps< unknown >[ 'customValidity' ];
83
81
  /**
84
82
  * A function that returns the actual element on which the validity data should be applied.
85
83
  */
@@ -92,6 +90,13 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
92
90
  forwardedRef: React.ForwardedRef< HTMLDivElement >
93
91
  ) {
94
92
  const [ errorMessage, setErrorMessage ] = useState< string | undefined >();
93
+ const [ statusMessage, setStatusMessage ] = useState<
94
+ | {
95
+ type: 'validating' | 'valid';
96
+ message?: string;
97
+ }
98
+ | undefined
99
+ >();
95
100
  const [ isTouched, setIsTouched ] = useState( false );
96
101
 
97
102
  // Ensure that error messages are visible after user attemps to submit a form
@@ -111,13 +116,58 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
111
116
  };
112
117
  } );
113
118
 
114
- const validate = () => {
115
- const message = customValidator?.();
119
+ useEffect( () => {
120
+ if ( ! isTouched ) {
121
+ return;
122
+ }
123
+
116
124
  const validityTarget = getValidityTarget();
117
125
 
118
- validityTarget?.setCustomValidity( message ?? '' );
119
- setErrorMessage( validityTarget?.validationMessage );
120
- };
126
+ if ( ! customValidity?.type ) {
127
+ validityTarget?.setCustomValidity( '' );
128
+ setErrorMessage( validityTarget?.validationMessage );
129
+ setStatusMessage( undefined );
130
+ return;
131
+ }
132
+
133
+ switch ( customValidity.type ) {
134
+ case 'validating': {
135
+ // Wait before showing a validating state.
136
+ const timer = setTimeout( () => {
137
+ setStatusMessage( {
138
+ type: 'validating',
139
+ message: customValidity.message,
140
+ } );
141
+ }, 1000 );
142
+
143
+ return () => clearTimeout( timer );
144
+ }
145
+ case 'valid': {
146
+ validityTarget?.setCustomValidity( '' );
147
+ setErrorMessage( validityTarget?.validationMessage );
148
+
149
+ setStatusMessage( {
150
+ type: 'valid',
151
+ message: customValidity.message,
152
+ } );
153
+ return;
154
+ }
155
+ case 'invalid': {
156
+ validityTarget?.setCustomValidity(
157
+ customValidity.message ?? ''
158
+ );
159
+ setErrorMessage( validityTarget?.validationMessage );
160
+
161
+ setStatusMessage( undefined );
162
+ return undefined;
163
+ }
164
+ }
165
+ }, [
166
+ isTouched,
167
+ customValidity?.type,
168
+ customValidity?.message,
169
+ getValidityTarget,
170
+ ] );
121
171
 
122
172
  const onBlur = ( event: React.FocusEvent< HTMLDivElement > ) => {
123
173
  // Only consider "blurred from the component" if focus has fully left the wrapping div.
@@ -138,7 +188,7 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
138
188
  return;
139
189
  }
140
190
 
141
- validate();
191
+ onValidate?.();
142
192
  }
143
193
  };
144
194
 
@@ -148,7 +198,7 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
148
198
  // Only validate incrementally if the field has blurred at least once,
149
199
  // or currently has an error message.
150
200
  if ( isTouched || errorMessage ) {
151
- validate();
201
+ onValidate?.();
152
202
  }
153
203
  };
154
204
 
@@ -156,7 +206,7 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
156
206
  // Ensures that custom validators are triggered when the user submits by pressing Enter,
157
207
  // without ever blurring the control.
158
208
  if ( event.key === 'Enter' ) {
159
- validate();
209
+ onValidate?.();
160
210
  }
161
211
  };
162
212
 
@@ -180,15 +230,16 @@ function UnforwardedControlWithError< C extends React.ReactElement >(
180
230
  } ) }
181
231
  <div aria-live="polite">
182
232
  { errorMessage && (
183
- <p className="components-validated-control__error">
184
- <Icon
185
- className="components-validated-control__error-icon"
186
- icon={ error }
187
- size={ 16 }
188
- fill="currentColor"
189
- />
190
- { errorMessage }
191
- </p>
233
+ <ValidityIndicator
234
+ type="invalid"
235
+ message={ errorMessage }
236
+ />
237
+ ) }
238
+ { ! errorMessage && statusMessage && (
239
+ <ValidityIndicator
240
+ type={ statusMessage.type }
241
+ message={ statusMessage.message }
242
+ />
192
243
  ) }
193
244
  </div>
194
245
  </div>
@@ -45,7 +45,7 @@
45
45
  pointer-events: none;
46
46
  }
47
47
 
48
- .components-validated-control__error {
48
+ .components-validated-control__indicator {
49
49
  display: flex;
50
50
  align-items: flex-start;
51
51
  gap: 4px;
@@ -53,17 +53,31 @@
53
53
  font-family: $font-family-body;
54
54
  font-size: 0.75rem;
55
55
  line-height: 16px; // matches the icon size
56
- color: $alert-red;
56
+ color: $components-color-gray-700;
57
57
  animation:
58
- components-validated-control__error-jump 0.2s
58
+ components-validated-control__indicator-jump 0.2s
59
59
  cubic-bezier(0.68, -0.55, 0.27, 1.55);
60
+
61
+ &.is-invalid {
62
+ color: $alert-red;
63
+ }
64
+
65
+ &.is-valid {
66
+ color: color-mix(in srgb, $black 30%, $alert-green);
67
+ }
60
68
  }
61
69
 
62
- .components-validated-control__error-icon {
70
+ .components-validated-control__indicator-icon {
63
71
  flex-shrink: 0;
64
72
  }
65
73
 
66
- @keyframes components-validated-control__error-jump {
74
+ .components-validated-control__indicator-spinner {
75
+ margin: 2px;
76
+ width: $grid-unit-15;
77
+ height: $grid-unit-15;
78
+ }
79
+
80
+ @keyframes components-validated-control__indicator-jump {
67
81
  0% {
68
82
  transform: translateY(-4px);
69
83
  opacity: 0;