@wordpress/components 29.13.1-next.719a03cbe.0 → 30.1.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/CHANGELOG.md +34 -3
- package/build/autocomplete/index.js +4 -0
- package/build/autocomplete/index.js.map +1 -1
- package/build/calendar/date-calendar/index.js +13 -4
- package/build/calendar/date-calendar/index.js.map +1 -1
- package/build/calendar/date-range-calendar/index.js +8 -4
- package/build/calendar/date-range-calendar/index.js.map +1 -1
- package/build/calendar/types.js.map +1 -1
- package/build/color-picker/styles.js +7 -7
- package/build/color-picker/styles.js.map +1 -1
- package/build/icon/index.js +2 -0
- package/build/icon/index.js.map +1 -1
- package/build/index.js +0 -19
- package/build/index.js.map +1 -1
- package/build/palette-edit/index.js +1 -1
- package/build/palette-edit/index.js.map +1 -1
- package/build/private-apis.js +9 -1
- package/build/private-apis.js.map +1 -1
- package/build/range-control/index.js +1 -1
- package/build/range-control/index.js.map +1 -1
- package/build/range-control/styles/range-control-styles.js +35 -35
- package/build/range-control/styles/range-control-styles.js.map +1 -1
- package/build/range-control/tooltip.js +15 -15
- package/build/range-control/tooltip.js.map +1 -1
- package/build/range-control/types.js.map +1 -1
- package/build/select-control/index.js +1 -1
- package/build/select-control/index.js.map +1 -1
- package/build/textarea-control/index.js +7 -1
- package/build/textarea-control/index.js.map +1 -1
- package/build/toggle-group-control/toggle-group-control/as-button-group.js.map +1 -1
- package/build/utils/hooks/use-controlled-value.js +8 -4
- package/build/utils/hooks/use-controlled-value.js.map +1 -1
- package/build/validated-form-controls/components/checkbox-control.js +52 -0
- package/build/validated-form-controls/components/checkbox-control.js.map +1 -0
- package/build/validated-form-controls/components/combobox-control.js +64 -0
- package/build/validated-form-controls/components/combobox-control.js.map +1 -0
- package/build/validated-form-controls/components/custom-select-control.js +71 -0
- package/build/validated-form-controls/components/custom-select-control.js.map +1 -0
- package/build/validated-form-controls/components/index.js +138 -0
- package/build/validated-form-controls/components/index.js.map +1 -0
- package/build/validated-form-controls/components/input-control.js +50 -0
- package/build/validated-form-controls/components/input-control.js.map +1 -0
- package/build/validated-form-controls/components/number-control.js +53 -0
- package/build/validated-form-controls/components/number-control.js.map +1 -0
- package/build/validated-form-controls/components/radio-control.js +51 -0
- package/build/validated-form-controls/components/radio-control.js.map +1 -0
- package/build/validated-form-controls/components/range-control.js +51 -0
- package/build/validated-form-controls/components/range-control.js.map +1 -0
- package/build/validated-form-controls/components/select-control.js +53 -0
- package/build/validated-form-controls/components/select-control.js.map +1 -0
- package/build/validated-form-controls/components/text-control.js +51 -0
- package/build/validated-form-controls/components/text-control.js.map +1 -0
- package/build/validated-form-controls/components/textarea-control.js +50 -0
- package/build/validated-form-controls/components/textarea-control.js.map +1 -0
- package/build/validated-form-controls/components/toggle-control.js +60 -0
- package/build/validated-form-controls/components/toggle-control.js.map +1 -0
- package/build/validated-form-controls/components/toggle-group-control.js +69 -0
- package/build/validated-form-controls/components/toggle-group-control.js.map +1 -0
- package/build/validated-form-controls/components/types.js +6 -0
- package/build/validated-form-controls/components/types.js.map +1 -0
- package/build/validated-form-controls/control-with-error.js +137 -0
- package/build/validated-form-controls/control-with-error.js.map +1 -0
- package/build/validated-form-controls/index.js +28 -0
- package/build/validated-form-controls/index.js.map +1 -0
- package/build-module/autocomplete/index.js +4 -0
- package/build-module/autocomplete/index.js.map +1 -1
- package/build-module/calendar/date-calendar/index.js +11 -3
- package/build-module/calendar/date-calendar/index.js.map +1 -1
- package/build-module/calendar/date-range-calendar/index.js +8 -4
- package/build-module/calendar/date-range-calendar/index.js.map +1 -1
- package/build-module/calendar/types.js.map +1 -1
- package/build-module/color-picker/styles.js +7 -7
- package/build-module/color-picker/styles.js.map +1 -1
- package/build-module/icon/index.js +2 -0
- package/build-module/icon/index.js.map +1 -1
- package/build-module/index.js +0 -1
- package/build-module/index.js.map +1 -1
- package/build-module/palette-edit/index.js +2 -2
- package/build-module/palette-edit/index.js.map +1 -1
- package/build-module/private-apis.js +9 -1
- package/build-module/private-apis.js.map +1 -1
- package/build-module/range-control/index.js +1 -1
- package/build-module/range-control/index.js.map +1 -1
- package/build-module/range-control/styles/range-control-styles.js +35 -35
- package/build-module/range-control/styles/range-control-styles.js.map +1 -1
- package/build-module/range-control/tooltip.js +15 -15
- package/build-module/range-control/tooltip.js.map +1 -1
- package/build-module/range-control/types.js.map +1 -1
- package/build-module/select-control/index.js +1 -1
- package/build-module/select-control/index.js.map +1 -1
- package/build-module/textarea-control/index.js +7 -1
- package/build-module/textarea-control/index.js.map +1 -1
- package/build-module/toggle-group-control/toggle-group-control/as-button-group.js.map +1 -1
- package/build-module/utils/hooks/use-controlled-value.js +9 -5
- package/build-module/utils/hooks/use-controlled-value.js.map +1 -1
- package/build-module/validated-form-controls/components/checkbox-control.js +44 -0
- package/build-module/validated-form-controls/components/checkbox-control.js.map +1 -0
- package/build-module/validated-form-controls/components/combobox-control.js +56 -0
- package/build-module/validated-form-controls/components/combobox-control.js.map +1 -0
- package/build-module/validated-form-controls/components/custom-select-control.js +63 -0
- package/build-module/validated-form-controls/components/custom-select-control.js.map +1 -0
- package/build-module/validated-form-controls/components/index.js +13 -0
- package/build-module/validated-form-controls/components/index.js.map +1 -0
- package/build-module/validated-form-controls/components/input-control.js +42 -0
- package/build-module/validated-form-controls/components/input-control.js.map +1 -0
- package/build-module/validated-form-controls/components/number-control.js +45 -0
- package/build-module/validated-form-controls/components/number-control.js.map +1 -0
- package/build-module/validated-form-controls/components/radio-control.js +43 -0
- package/build-module/validated-form-controls/components/radio-control.js.map +1 -0
- package/build-module/validated-form-controls/components/range-control.js +43 -0
- package/build-module/validated-form-controls/components/range-control.js.map +1 -0
- package/build-module/validated-form-controls/components/select-control.js +45 -0
- package/build-module/validated-form-controls/components/select-control.js.map +1 -0
- package/build-module/validated-form-controls/components/text-control.js +43 -0
- package/build-module/validated-form-controls/components/text-control.js.map +1 -0
- package/build-module/validated-form-controls/components/textarea-control.js +42 -0
- package/build-module/validated-form-controls/components/textarea-control.js.map +1 -0
- package/build-module/validated-form-controls/components/toggle-control.js +52 -0
- package/build-module/validated-form-controls/components/toggle-control.js.map +1 -0
- package/build-module/validated-form-controls/components/toggle-group-control.js +62 -0
- package/build-module/validated-form-controls/components/toggle-group-control.js.map +1 -0
- package/build-module/validated-form-controls/components/types.js +2 -0
- package/build-module/validated-form-controls/components/types.js.map +1 -0
- package/build-module/validated-form-controls/control-with-error.js +129 -0
- package/build-module/validated-form-controls/control-with-error.js.map +1 -0
- package/build-module/validated-form-controls/index.js +3 -0
- package/build-module/validated-form-controls/index.js.map +1 -0
- package/build-style/style-rtl.css +81 -71
- package/build-style/style.css +81 -71
- package/build-types/autocomplete/index.d.ts.map +1 -1
- package/build-types/box-control/utils.d.ts +7 -7
- package/build-types/calendar/date-calendar/index.d.ts.map +1 -1
- package/build-types/calendar/date-range-calendar/index.d.ts.map +1 -1
- package/build-types/calendar/types.d.ts +2 -2
- package/build-types/calendar/types.d.ts.map +1 -1
- package/build-types/calendar/utils/use-localization-props.d.ts +3 -3
- package/build-types/color-picker/styles.d.ts.map +1 -1
- package/build-types/custom-gradient-picker/constants.d.ts +2 -2
- package/build-types/dimension-control/sizes.d.ts +5 -5
- package/build-types/font-size-picker/constants.d.ts +2 -2
- package/build-types/font-size-picker/constants.d.ts.map +1 -1
- package/build-types/icon/index.d.ts.map +1 -1
- package/build-types/index.d.ts +0 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/private-apis.d.ts.map +1 -1
- package/build-types/range-control/types.d.ts +2 -2
- package/build-types/range-control/types.d.ts.map +1 -1
- package/build-types/select-control/stories/index.story.d.ts.map +1 -1
- package/build-types/text-control/stories/index.story.d.ts.map +1 -1
- package/build-types/textarea-control/index.d.ts.map +1 -1
- package/build-types/toggle-group-control/toggle-group-control/as-button-group.d.ts.map +1 -1
- package/build-types/utils/hooks/use-controlled-value.d.ts +2 -2
- package/build-types/utils/hooks/use-controlled-value.d.ts.map +1 -1
- package/build-types/validated-form-controls/components/checkbox-control.d.ts +9 -0
- package/build-types/validated-form-controls/components/checkbox-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/combobox-control.d.ts +21 -0
- package/build-types/validated-form-controls/components/combobox-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/custom-select-control.d.ts +4 -0
- package/build-types/validated-form-controls/components/custom-select-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/index.d.ts +13 -0
- package/build-types/validated-form-controls/components/index.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/input-control.d.ts +4 -0
- package/build-types/validated-form-controls/components/input-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/number-control.d.ts +17 -0
- package/build-types/validated-form-controls/components/number-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/radio-control.d.ts +11 -0
- package/build-types/validated-form-controls/components/radio-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/range-control.d.ts +36 -0
- package/build-types/validated-form-controls/components/range-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/select-control.d.ts +9 -0
- package/build-types/validated-form-controls/components/select-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/checkbox-control.story.d.ts +12 -0
- package/build-types/validated-form-controls/components/stories/checkbox-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/combobox-control.story.d.ts +12 -0
- package/build-types/validated-form-controls/components/stories/combobox-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/custom-select-control.story.d.ts +12 -0
- package/build-types/validated-form-controls/components/stories/custom-select-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/input-control.story.d.ts +18 -0
- package/build-types/validated-form-controls/components/stories/input-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/number-control.story.d.ts +12 -0
- package/build-types/validated-form-controls/components/stories/number-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/overview.story.d.ts +19 -0
- package/build-types/validated-form-controls/components/stories/overview.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/radio-control.story.d.ts +12 -0
- package/build-types/validated-form-controls/components/stories/radio-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/range-control.story.d.ts +9 -0
- package/build-types/validated-form-controls/components/stories/range-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/select-control.story.d.ts +12 -0
- package/build-types/validated-form-controls/components/stories/select-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/story-utils.d.ts +9 -0
- package/build-types/validated-form-controls/components/stories/story-utils.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/text-control.story.d.ts +9 -0
- package/build-types/validated-form-controls/components/stories/text-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/textarea-control.story.d.ts +9 -0
- package/build-types/validated-form-controls/components/stories/textarea-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/toggle-control.story.d.ts +9 -0
- package/build-types/validated-form-controls/components/stories/toggle-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/stories/toggle-group-control.story.d.ts +9 -0
- package/build-types/validated-form-controls/components/stories/toggle-group-control.story.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/text-control.d.ts +8 -0
- package/build-types/validated-form-controls/components/text-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/textarea-control.d.ts +7 -0
- package/build-types/validated-form-controls/components/textarea-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/toggle-control.d.ts +7 -0
- package/build-types/validated-form-controls/components/toggle-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/toggle-group-control.d.ts +15 -0
- package/build-types/validated-form-controls/components/toggle-group-control.d.ts.map +1 -0
- package/build-types/validated-form-controls/components/types.d.ts +27 -0
- package/build-types/validated-form-controls/components/types.d.ts.map +1 -0
- package/build-types/validated-form-controls/control-with-error.d.ts +36 -0
- package/build-types/validated-form-controls/control-with-error.d.ts.map +1 -0
- package/build-types/validated-form-controls/index.d.ts +3 -0
- package/build-types/validated-form-controls/index.d.ts.map +1 -0
- package/package.json +19 -19
- package/src/autocomplete/index.tsx +4 -0
- package/src/calendar/date-calendar/README.md +57 -46
- package/src/calendar/date-calendar/index.tsx +22 -8
- package/src/calendar/date-range-calendar/README.md +63 -52
- package/src/calendar/date-range-calendar/index.tsx +23 -11
- package/src/calendar/types.ts +2 -2
- package/src/color-picker/styles.ts +10 -0
- package/src/dimension-control/test/__snapshots__/index.test.js.snap +8 -8
- package/src/icon/index.tsx +2 -0
- package/src/index.ts +0 -1
- package/src/modal/style.scss +2 -2
- package/src/palette-edit/index.tsx +3 -3
- package/src/private-apis.ts +13 -0
- package/src/range-control/index.tsx +1 -1
- package/src/range-control/styles/range-control-styles.ts +3 -3
- package/src/range-control/tooltip.tsx +13 -13
- package/src/range-control/types.ts +2 -2
- package/src/select-control/index.tsx +1 -1
- package/src/style.scss +2 -2
- package/src/text-control/stories/index.story.tsx +1 -0
- package/src/text-control/style.scss +6 -1
- package/src/textarea-control/index.tsx +8 -1
- package/src/toggle-group-control/toggle-group-control/as-button-group.tsx +3 -1
- package/src/utils/hooks/use-controlled-value.ts +16 -8
- package/src/utils/theme-variables.scss +3 -0
- package/src/validated-form-controls/components/checkbox-control.tsx +64 -0
- package/src/validated-form-controls/components/combobox-control.tsx +77 -0
- package/src/validated-form-controls/components/custom-select-control.tsx +86 -0
- package/src/validated-form-controls/components/index.ts +12 -0
- package/src/validated-form-controls/components/input-control.tsx +59 -0
- package/src/validated-form-controls/components/number-control.tsx +61 -0
- package/src/validated-form-controls/components/radio-control.tsx +60 -0
- package/src/validated-form-controls/components/range-control.tsx +60 -0
- package/src/validated-form-controls/components/select-control.tsx +75 -0
- package/src/validated-form-controls/components/stories/checkbox-control.story.tsx +57 -0
- package/src/validated-form-controls/components/stories/combobox-control.story.tsx +64 -0
- package/src/validated-form-controls/components/stories/custom-select-control.story.tsx +64 -0
- package/src/validated-form-controls/components/stories/input-control.story.tsx +132 -0
- package/src/validated-form-controls/components/stories/number-control.story.tsx +62 -0
- package/src/validated-form-controls/components/stories/overview.mdx +52 -0
- package/src/validated-form-controls/components/stories/overview.story.tsx +100 -0
- package/src/validated-form-controls/components/stories/radio-control.story.tsx +64 -0
- package/src/validated-form-controls/components/stories/range-control.story.tsx +60 -0
- package/src/validated-form-controls/components/stories/select-control.story.tsx +60 -0
- package/src/validated-form-controls/components/stories/story-utils.tsx +46 -0
- package/src/validated-form-controls/components/stories/text-control.story.tsx +55 -0
- package/src/validated-form-controls/components/stories/textarea-control.story.tsx +52 -0
- package/src/validated-form-controls/components/stories/toggle-control.story.tsx +55 -0
- package/src/validated-form-controls/components/stories/toggle-group-control.story.tsx +66 -0
- package/src/validated-form-controls/components/text-control.tsx +60 -0
- package/src/validated-form-controls/components/textarea-control.tsx +59 -0
- package/src/validated-form-controls/components/toggle-control.tsx +69 -0
- package/src/validated-form-controls/components/toggle-group-control.tsx +82 -0
- package/src/validated-form-controls/components/types.ts +28 -0
- package/src/validated-form-controls/control-with-error.tsx +198 -0
- package/src/validated-form-controls/index.ts +2 -0
- package/src/validated-form-controls/style.scss +75 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/calendar/utils/use-controlled-value.js +0 -58
- package/build/calendar/utils/use-controlled-value.js.map +0 -1
- package/build-module/calendar/utils/use-controlled-value.js +0 -51
- package/build-module/calendar/utils/use-controlled-value.js.map +0 -1
- package/build-types/calendar/utils/use-controlled-value.d.ts +0 -27
- package/build-types/calendar/utils/use-controlled-value.d.ts.map +0 -1
- package/src/calendar/utils/use-controlled-value.ts +0 -61
- package/src/dimension-control/style.scss +0 -22
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { __ } from '@wordpress/i18n';
|
|
5
|
+
import { error } from '@wordpress/icons';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* External dependencies
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
cloneElement,
|
|
12
|
+
forwardRef,
|
|
13
|
+
useEffect,
|
|
14
|
+
useState,
|
|
15
|
+
} from '@wordpress/element';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Internal dependencies
|
|
19
|
+
*/
|
|
20
|
+
import { withIgnoreIMEEvents } from '../utils/with-ignore-ime-events';
|
|
21
|
+
|
|
22
|
+
import Icon from '../icon';
|
|
23
|
+
|
|
24
|
+
function appendRequiredIndicator(
|
|
25
|
+
label: React.ReactNode,
|
|
26
|
+
required: boolean | undefined,
|
|
27
|
+
markWhenOptional: boolean | undefined
|
|
28
|
+
) {
|
|
29
|
+
if ( required && ! markWhenOptional ) {
|
|
30
|
+
return (
|
|
31
|
+
<>
|
|
32
|
+
{ label } { `(${ __( 'Required' ) })` }
|
|
33
|
+
</>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
if ( ! required && markWhenOptional ) {
|
|
37
|
+
return (
|
|
38
|
+
<>
|
|
39
|
+
{ label } { `(${ __( 'Optional' ) })` }
|
|
40
|
+
</>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
return label;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* HTML elements that support the Constraint Validation API.
|
|
48
|
+
*
|
|
49
|
+
* Here, we exclude HTMLButtonElement because although it does technically support the API,
|
|
50
|
+
* normal buttons are actually exempted from any validation.
|
|
51
|
+
* @see https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Forms/Form_validation
|
|
52
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement/willValidate
|
|
53
|
+
*/
|
|
54
|
+
type ValidityTarget =
|
|
55
|
+
| HTMLFieldSetElement
|
|
56
|
+
| HTMLInputElement
|
|
57
|
+
| HTMLSelectElement
|
|
58
|
+
| HTMLTextAreaElement;
|
|
59
|
+
|
|
60
|
+
function UnforwardedControlWithError< C extends React.ReactElement >(
|
|
61
|
+
{
|
|
62
|
+
required,
|
|
63
|
+
markWhenOptional,
|
|
64
|
+
customValidator,
|
|
65
|
+
getValidityTarget,
|
|
66
|
+
children,
|
|
67
|
+
}: {
|
|
68
|
+
/**
|
|
69
|
+
* Whether the control is required.
|
|
70
|
+
*/
|
|
71
|
+
required?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Label the control as "optional" when _not_ `required`, instead of the inverse.
|
|
74
|
+
*/
|
|
75
|
+
markWhenOptional?: boolean;
|
|
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
|
|
81
|
+
*/
|
|
82
|
+
customValidator?: () => string | void;
|
|
83
|
+
/**
|
|
84
|
+
* A function that returns the actual element on which the validity data should be applied.
|
|
85
|
+
*/
|
|
86
|
+
getValidityTarget: () => ValidityTarget | null | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* The control component to apply validation to.
|
|
89
|
+
*/
|
|
90
|
+
children: C;
|
|
91
|
+
},
|
|
92
|
+
forwardedRef: React.ForwardedRef< HTMLDivElement >
|
|
93
|
+
) {
|
|
94
|
+
const [ errorMessage, setErrorMessage ] = useState< string | undefined >();
|
|
95
|
+
const [ isTouched, setIsTouched ] = useState( false );
|
|
96
|
+
|
|
97
|
+
// Ensure that error messages are visible after user attemps to submit a form
|
|
98
|
+
// with multiple invalid fields.
|
|
99
|
+
useEffect( () => {
|
|
100
|
+
const validityTarget = getValidityTarget();
|
|
101
|
+
const showValidationMessage = () =>
|
|
102
|
+
setErrorMessage( validityTarget?.validationMessage );
|
|
103
|
+
|
|
104
|
+
validityTarget?.addEventListener( 'invalid', showValidationMessage );
|
|
105
|
+
|
|
106
|
+
return () => {
|
|
107
|
+
validityTarget?.removeEventListener(
|
|
108
|
+
'invalid',
|
|
109
|
+
showValidationMessage
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
} );
|
|
113
|
+
|
|
114
|
+
const validate = () => {
|
|
115
|
+
const message = customValidator?.();
|
|
116
|
+
const validityTarget = getValidityTarget();
|
|
117
|
+
|
|
118
|
+
validityTarget?.setCustomValidity( message ?? '' );
|
|
119
|
+
setErrorMessage( validityTarget?.validationMessage );
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const onBlur = ( event: React.FocusEvent< HTMLDivElement > ) => {
|
|
123
|
+
// Only consider "blurred from the component" if focus has fully left the wrapping div.
|
|
124
|
+
// This prevents unnecessary blurs from components with multiple focusable elements.
|
|
125
|
+
if (
|
|
126
|
+
! event.relatedTarget ||
|
|
127
|
+
! event.currentTarget.contains( event.relatedTarget )
|
|
128
|
+
) {
|
|
129
|
+
setIsTouched( true );
|
|
130
|
+
|
|
131
|
+
const validityTarget = getValidityTarget();
|
|
132
|
+
|
|
133
|
+
// Prevents a double flash of the native error tooltip when the control is already showing one.
|
|
134
|
+
if ( ! validityTarget?.validity.valid ) {
|
|
135
|
+
if ( ! errorMessage ) {
|
|
136
|
+
setErrorMessage( validityTarget?.validationMessage );
|
|
137
|
+
}
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
validate();
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const onChange = ( ...args: unknown[] ) => {
|
|
146
|
+
children.props.onChange?.( ...args );
|
|
147
|
+
|
|
148
|
+
// Only validate incrementally if the field has blurred at least once,
|
|
149
|
+
// or currently has an error message.
|
|
150
|
+
if ( isTouched || errorMessage ) {
|
|
151
|
+
validate();
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const onKeyDown = ( event: React.KeyboardEvent< HTMLDivElement > ) => {
|
|
156
|
+
// Ensures that custom validators are triggered when the user submits by pressing Enter,
|
|
157
|
+
// without ever blurring the control.
|
|
158
|
+
if ( event.key === 'Enter' ) {
|
|
159
|
+
validate();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
// Disable reason: Just listening to a bubbled event, not for interaction.
|
|
165
|
+
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
|
|
166
|
+
<div
|
|
167
|
+
className="components-validated-control"
|
|
168
|
+
ref={ forwardedRef }
|
|
169
|
+
onBlur={ onBlur }
|
|
170
|
+
onKeyDown={ withIgnoreIMEEvents( onKeyDown ) }
|
|
171
|
+
>
|
|
172
|
+
{ cloneElement( children, {
|
|
173
|
+
label: appendRequiredIndicator(
|
|
174
|
+
children.props.label,
|
|
175
|
+
required,
|
|
176
|
+
markWhenOptional
|
|
177
|
+
),
|
|
178
|
+
onChange,
|
|
179
|
+
required,
|
|
180
|
+
} ) }
|
|
181
|
+
<div aria-live="polite">
|
|
182
|
+
{ 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>
|
|
192
|
+
) }
|
|
193
|
+
</div>
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export const ControlWithError = forwardRef( UnforwardedControlWithError );
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
.components-validated-control {
|
|
2
|
+
// For components based on InputBase
|
|
3
|
+
&:has(:is(input, select):user-invalid)
|
|
4
|
+
.components-input-control__backdrop {
|
|
5
|
+
--wp-components-color-accent: $alert-red;
|
|
6
|
+
border-color: $alert-red;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// For TextControl, TextareaControl
|
|
10
|
+
:is(textarea, input[type="text"]):user-invalid {
|
|
11
|
+
--wp-admin-theme-color: $alert-red;
|
|
12
|
+
--wp-components-color-accent: $alert-red;
|
|
13
|
+
border-color: $alert-red;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// For ComboboxControl
|
|
17
|
+
.components-combobox-control__suggestions-container:has(
|
|
18
|
+
input:user-invalid
|
|
19
|
+
):not(:has([aria-expanded="true"])) {
|
|
20
|
+
border-color: $alert-red;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.components-validated-control__wrapper-with-error-delegate {
|
|
25
|
+
position: relative;
|
|
26
|
+
|
|
27
|
+
// For CustomSelectControl
|
|
28
|
+
&:has(select:user-invalid) .components-input-control__backdrop {
|
|
29
|
+
--wp-components-color-accent: $alert-red;
|
|
30
|
+
border-color: $alert-red;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// For ToggleGroupControl
|
|
34
|
+
&:has(input[type="radio"]:invalid) {
|
|
35
|
+
--wp-components-color-accent: $alert-red;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.components-validated-control__error-delegate {
|
|
40
|
+
position: absolute;
|
|
41
|
+
top: 0;
|
|
42
|
+
height: 100%;
|
|
43
|
+
width: 100%;
|
|
44
|
+
opacity: 0;
|
|
45
|
+
pointer-events: none;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.components-validated-control__error {
|
|
49
|
+
display: flex;
|
|
50
|
+
align-items: flex-start;
|
|
51
|
+
gap: 4px;
|
|
52
|
+
margin: 8px 0 0;
|
|
53
|
+
font-family: $font-family-body;
|
|
54
|
+
font-size: 0.75rem;
|
|
55
|
+
line-height: 16px; // matches the icon size
|
|
56
|
+
color: $alert-red;
|
|
57
|
+
animation:
|
|
58
|
+
components-validated-control__error-jump 0.2s
|
|
59
|
+
cubic-bezier(0.68, -0.55, 0.27, 1.55);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.components-validated-control__error-icon {
|
|
63
|
+
flex-shrink: 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@keyframes components-validated-control__error-jump {
|
|
67
|
+
0% {
|
|
68
|
+
transform: translateY(-4px);
|
|
69
|
+
opacity: 0;
|
|
70
|
+
}
|
|
71
|
+
100% {
|
|
72
|
+
transform: translateY(0);
|
|
73
|
+
opacity: 1;
|
|
74
|
+
}
|
|
75
|
+
}
|