@maratus-registry/field 0.1.0 → 0.2.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.
- package/css-files/Field.tsx +8 -13
- package/css-files/Field.types.ts +22 -11
- package/css-files/FieldContext.tsx +66 -36
- package/css-files/useControl.ts +45 -15
- package/css-files/useDescription.ts +2 -2
- package/css-files/useErrorMessage.ts +2 -2
- package/css-files/useLabel.ts +3 -3
- package/css-modules/Field.tsx +8 -13
- package/css-modules/Field.types.ts +22 -11
- package/css-modules/FieldContext.tsx +66 -36
- package/css-modules/useControl.ts +45 -15
- package/css-modules/useDescription.ts +2 -2
- package/css-modules/useErrorMessage.ts +2 -2
- package/css-modules/useLabel.ts +3 -3
- package/package.json +1 -1
- package/tailwind-css/Field.tsx +8 -13
- package/tailwind-css/Field.types.ts +22 -11
- package/tailwind-css/FieldContext.tsx +66 -36
- package/tailwind-css/useControl.ts +45 -15
- package/tailwind-css/useDescription.ts +2 -2
- package/tailwind-css/useErrorMessage.ts +2 -2
- package/tailwind-css/useLabel.ts +3 -3
package/css-files/Field.tsx
CHANGED
|
@@ -4,13 +4,11 @@ import type {
|
|
|
4
4
|
FieldErrorKey,
|
|
5
5
|
FieldErrorPolicy,
|
|
6
6
|
UseControlOptions,
|
|
7
|
+
UseDescriptionOptions,
|
|
8
|
+
UseErrorMessageOptions,
|
|
9
|
+
UseLabelOptions,
|
|
7
10
|
} from './Field.types'
|
|
8
|
-
import type {
|
|
9
|
-
ElementType,
|
|
10
|
-
HTMLAttributes,
|
|
11
|
-
LabelHTMLAttributes,
|
|
12
|
-
ReactNode,
|
|
13
|
-
} from 'react'
|
|
11
|
+
import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
|
|
14
12
|
import { FieldProvider } from './FieldContext'
|
|
15
13
|
import { useControl } from './useControl'
|
|
16
14
|
import { useDescription } from './useDescription'
|
|
@@ -18,7 +16,7 @@ import { useErrorMessage } from './useErrorMessage'
|
|
|
18
16
|
import { useFieldRoot } from './useFieldRoot'
|
|
19
17
|
import { useLabel } from './useLabel'
|
|
20
18
|
|
|
21
|
-
export type FieldRootProps =
|
|
19
|
+
export type FieldRootProps = ComponentPropsWithRef<'div'> & {
|
|
22
20
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
|
23
21
|
as?: ElementType
|
|
24
22
|
controlId?: string
|
|
@@ -67,7 +65,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
67
65
|
)
|
|
68
66
|
}
|
|
69
67
|
|
|
70
|
-
export type LabelProps =
|
|
68
|
+
export type LabelProps = UseLabelOptions
|
|
71
69
|
|
|
72
70
|
export function Label(props: LabelProps) {
|
|
73
71
|
const { labelProps } = useLabel(props)
|
|
@@ -86,7 +84,7 @@ export function Control(props: ControlProps) {
|
|
|
86
84
|
return children(control)
|
|
87
85
|
}
|
|
88
86
|
|
|
89
|
-
export type DescriptionProps =
|
|
87
|
+
export type DescriptionProps = UseDescriptionOptions & {
|
|
90
88
|
as?: ElementType
|
|
91
89
|
}
|
|
92
90
|
|
|
@@ -97,10 +95,7 @@ export function Description(props: DescriptionProps) {
|
|
|
97
95
|
return <Root {...descriptionProps} />
|
|
98
96
|
}
|
|
99
97
|
|
|
100
|
-
export type ErrorMessageProps =
|
|
101
|
-
HTMLAttributes<HTMLDivElement>,
|
|
102
|
-
'children'
|
|
103
|
-
> & {
|
|
98
|
+
export type ErrorMessageProps = UseErrorMessageOptions & {
|
|
104
99
|
as?: ElementType
|
|
105
100
|
renderChildren?: (props: ErrorMessageItemProps) => ReactNode
|
|
106
101
|
}
|
package/css-files/Field.types.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AriaAttributes,
|
|
3
3
|
ChangeEventHandler,
|
|
4
|
+
ComponentPropsWithRef,
|
|
4
5
|
FocusEventHandler,
|
|
5
6
|
HTMLAttributes,
|
|
6
|
-
LabelHTMLAttributes,
|
|
7
7
|
ReactEventHandler,
|
|
8
8
|
ReactNode,
|
|
9
|
+
Ref,
|
|
9
10
|
} from 'react'
|
|
10
11
|
|
|
11
12
|
export type ValidityErrorKey = Exclude<keyof ValidityState, 'valid'>
|
|
@@ -45,7 +46,7 @@ export type FieldContextValue = {
|
|
|
45
46
|
descriptionId: string
|
|
46
47
|
errorId: string
|
|
47
48
|
errorMap?: ReadonlyMap<FieldErrorKey, ReactNode>
|
|
48
|
-
|
|
49
|
+
updateValidityState(
|
|
49
50
|
event: FieldErrorPolicyArgs['event'],
|
|
50
51
|
control: { validity: ValidityState },
|
|
51
52
|
): void
|
|
@@ -72,11 +73,11 @@ export type FieldProviderProps = {
|
|
|
72
73
|
name: string
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
export type UseFieldRootOptions =
|
|
76
|
+
export type UseFieldRootOptions = ComponentPropsWithRef<'div'> & {
|
|
76
77
|
isLoading?: boolean
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
export type FieldRootRenderProps =
|
|
80
|
+
export type FieldRootRenderProps = ComponentPropsWithRef<'div'> & {
|
|
80
81
|
'data-loading'?: ''
|
|
81
82
|
}
|
|
82
83
|
|
|
@@ -84,9 +85,14 @@ export type UseFieldRootResult = {
|
|
|
84
85
|
fieldRootProps: FieldRootRenderProps
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
|
|
88
|
+
// GPRD-006, REQ-002 and REQ-004: Field owns label relationship wiring so
|
|
89
|
+
// consumers cannot override the generated label id or control association.
|
|
90
|
+
export type UseLabelOptions = Omit<
|
|
91
|
+
ComponentPropsWithRef<'label'>,
|
|
92
|
+
'htmlFor' | 'id'
|
|
93
|
+
>
|
|
88
94
|
|
|
89
|
-
export type LabelRootProps =
|
|
95
|
+
export type LabelRootProps = ComponentPropsWithRef<'label'> & {
|
|
90
96
|
'data-loading'?: ''
|
|
91
97
|
'data-readonly'?: ''
|
|
92
98
|
'data-required'?: ''
|
|
@@ -125,6 +131,7 @@ export type ControlRenderProps = ControlElementProps & {
|
|
|
125
131
|
onInput?: ReactEventHandler<ControlElement>
|
|
126
132
|
onInvalid?: ReactEventHandler<ControlElement>
|
|
127
133
|
readOnly?: boolean
|
|
134
|
+
ref?: Ref<any>
|
|
128
135
|
required?: boolean
|
|
129
136
|
role?: ControlRole
|
|
130
137
|
}
|
|
@@ -177,15 +184,19 @@ export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
|
|
|
177
184
|
}
|
|
178
185
|
}
|
|
179
186
|
|
|
180
|
-
|
|
187
|
+
// GPRD-006 and REQ-005: Field owns the description id because Control uses it
|
|
188
|
+
// for aria-describedby.
|
|
189
|
+
export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
|
|
181
190
|
|
|
182
191
|
export type UseDescriptionResult = {
|
|
183
|
-
descriptionProps:
|
|
192
|
+
descriptionProps: ComponentPropsWithRef<'div'>
|
|
184
193
|
}
|
|
185
194
|
|
|
195
|
+
// GPRD-006 and REQ-007: Field owns the error id because Control uses it for
|
|
196
|
+
// aria-errormessage when errors are visible.
|
|
186
197
|
export type UseErrorMessageOptions = Omit<
|
|
187
|
-
|
|
188
|
-
'children'
|
|
198
|
+
ComponentPropsWithRef<'div'>,
|
|
199
|
+
'children' | 'id'
|
|
189
200
|
>
|
|
190
201
|
|
|
191
202
|
export type ErrorMessageItemProps = {
|
|
@@ -196,6 +207,6 @@ export type ErrorMessageItemProps = {
|
|
|
196
207
|
}
|
|
197
208
|
|
|
198
209
|
export type UseErrorMessageResult = {
|
|
199
|
-
errorMessageProps:
|
|
210
|
+
errorMessageProps: ComponentPropsWithRef<'div'>
|
|
200
211
|
items: readonly ErrorMessageItemProps[]
|
|
201
212
|
}
|
|
@@ -68,21 +68,7 @@ function useFieldProviderValue({
|
|
|
68
68
|
form: defaultFormState,
|
|
69
69
|
nativeState,
|
|
70
70
|
})
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
nativeState.isErrorVisible !== nextIsErrorVisible ||
|
|
75
|
-
(nextIsErrorVisible && !nativeState.field.wasErrored)
|
|
76
|
-
) {
|
|
77
|
-
setNativeState({
|
|
78
|
-
...nativeState,
|
|
79
|
-
field: {
|
|
80
|
-
...nativeState.field,
|
|
81
|
-
wasErrored: nativeState.field.wasErrored || nextIsErrorVisible,
|
|
82
|
-
},
|
|
83
|
-
isErrorVisible: nextIsErrorVisible,
|
|
84
|
-
})
|
|
85
|
-
}
|
|
71
|
+
const isErrorVisible = visibleErrors.length > 0
|
|
86
72
|
|
|
87
73
|
return {
|
|
88
74
|
activeErrors,
|
|
@@ -91,29 +77,20 @@ function useFieldProviderValue({
|
|
|
91
77
|
descriptionId: `${generatedId}-description`,
|
|
92
78
|
errorId: `${generatedId}-error`,
|
|
93
79
|
errorMap,
|
|
94
|
-
|
|
80
|
+
updateValidityState: (event, control) => {
|
|
95
81
|
const nextErrors = getValidityErrorKeys(control.validity)
|
|
82
|
+
const nextActiveErrors = activeErrors ?? new Set(nextErrors)
|
|
96
83
|
|
|
97
|
-
setNativeState((currentNativeState) =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
wasTouched:
|
|
108
|
-
currentNativeState.field.wasTouched ||
|
|
109
|
-
event === 'focus' ||
|
|
110
|
-
event === 'blur' ||
|
|
111
|
-
event === 'change' ||
|
|
112
|
-
event === 'input' ||
|
|
113
|
-
event === 'invalid',
|
|
114
|
-
},
|
|
115
|
-
isErrorVisible: currentNativeState.isErrorVisible,
|
|
116
|
-
}))
|
|
84
|
+
setNativeState((currentNativeState) =>
|
|
85
|
+
getNextNativeState({
|
|
86
|
+
currentNativeState,
|
|
87
|
+
errorPolicy,
|
|
88
|
+
event,
|
|
89
|
+
isErrorVisible,
|
|
90
|
+
nextActiveErrors,
|
|
91
|
+
nextErrors,
|
|
92
|
+
}),
|
|
93
|
+
)
|
|
117
94
|
},
|
|
118
95
|
isLoading,
|
|
119
96
|
isReadOnly,
|
|
@@ -125,6 +102,59 @@ function useFieldProviderValue({
|
|
|
125
102
|
}
|
|
126
103
|
}
|
|
127
104
|
|
|
105
|
+
function getNextNativeState({
|
|
106
|
+
currentNativeState,
|
|
107
|
+
errorPolicy,
|
|
108
|
+
event,
|
|
109
|
+
isErrorVisible,
|
|
110
|
+
nextActiveErrors,
|
|
111
|
+
nextErrors,
|
|
112
|
+
}: {
|
|
113
|
+
currentNativeState: FieldNativeState
|
|
114
|
+
errorPolicy: FieldErrorPolicy
|
|
115
|
+
event: FieldErrorPolicyArgs['event']
|
|
116
|
+
isErrorVisible: boolean
|
|
117
|
+
nextActiveErrors: ReadonlySet<FieldErrorKey>
|
|
118
|
+
nextErrors: readonly FieldErrorKey[]
|
|
119
|
+
}): FieldNativeState {
|
|
120
|
+
const eventNativeState: FieldNativeState = {
|
|
121
|
+
errors: nextErrors,
|
|
122
|
+
event,
|
|
123
|
+
field: {
|
|
124
|
+
wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
|
|
125
|
+
wasChanged:
|
|
126
|
+
currentNativeState.field.wasChanged ||
|
|
127
|
+
event === 'change' ||
|
|
128
|
+
event === 'input',
|
|
129
|
+
wasErrored: currentNativeState.field.wasErrored || isErrorVisible,
|
|
130
|
+
wasTouched:
|
|
131
|
+
currentNativeState.field.wasTouched ||
|
|
132
|
+
event === 'focus' ||
|
|
133
|
+
event === 'blur' ||
|
|
134
|
+
event === 'change' ||
|
|
135
|
+
event === 'input' ||
|
|
136
|
+
event === 'invalid',
|
|
137
|
+
},
|
|
138
|
+
isErrorVisible,
|
|
139
|
+
}
|
|
140
|
+
const nextVisibleErrors = resolveVisibleErrors({
|
|
141
|
+
activeErrors: nextActiveErrors,
|
|
142
|
+
errorPolicy,
|
|
143
|
+
form: defaultFormState,
|
|
144
|
+
nativeState: eventNativeState,
|
|
145
|
+
})
|
|
146
|
+
const nextIsErrorVisible = nextVisibleErrors.length > 0
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
...eventNativeState,
|
|
150
|
+
field: {
|
|
151
|
+
...eventNativeState.field,
|
|
152
|
+
wasErrored: eventNativeState.field.wasErrored || nextIsErrorVisible,
|
|
153
|
+
},
|
|
154
|
+
isErrorVisible: nextIsErrorVisible,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
128
158
|
const validityErrorKeys = [
|
|
129
159
|
'badInput',
|
|
130
160
|
'customError',
|
package/css-files/useControl.ts
CHANGED
|
@@ -11,7 +11,16 @@ import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
|
|
|
11
11
|
import './Field.css'
|
|
12
12
|
|
|
13
13
|
export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
className,
|
|
16
|
+
onBlur,
|
|
17
|
+
onChange,
|
|
18
|
+
onFocus,
|
|
19
|
+
onInput,
|
|
20
|
+
onInvalid,
|
|
21
|
+
role,
|
|
22
|
+
...controlRootProps
|
|
23
|
+
} = options
|
|
15
24
|
const field = useRequiredFieldContext('Control')
|
|
16
25
|
const relationshipProps = {
|
|
17
26
|
'aria-busy': field.isLoading ? true : undefined,
|
|
@@ -34,8 +43,15 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
|
34
43
|
| 'data-loading'
|
|
35
44
|
| 'id'
|
|
36
45
|
>
|
|
37
|
-
const validityHandlerProps =
|
|
38
|
-
|
|
46
|
+
const validityHandlerProps = composeValidityHandlerProps(
|
|
47
|
+
{
|
|
48
|
+
onBlur,
|
|
49
|
+
onChange,
|
|
50
|
+
onFocus,
|
|
51
|
+
onInput,
|
|
52
|
+
onInvalid,
|
|
53
|
+
},
|
|
54
|
+
field.updateValidityState,
|
|
39
55
|
)
|
|
40
56
|
|
|
41
57
|
if (role) {
|
|
@@ -81,23 +97,37 @@ const withValidity: WithValidity = (event, validity) => {
|
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
function
|
|
85
|
-
|
|
100
|
+
function composeValidityHandlerProps(
|
|
101
|
+
userHandlers: Pick<
|
|
102
|
+
UseControlOptions,
|
|
103
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
104
|
+
>,
|
|
105
|
+
updateValidityState: FieldContextValue['updateValidityState'],
|
|
86
106
|
): Pick<
|
|
87
107
|
ControlRenderProps,
|
|
88
108
|
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
109
|
> {
|
|
90
110
|
return {
|
|
91
|
-
onBlur: (event) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
onBlur: (event) => {
|
|
112
|
+
userHandlers.onBlur?.(event)
|
|
113
|
+
updateValidityState('blur', getValidityControl(event))
|
|
114
|
+
},
|
|
115
|
+
onChange: (event) => {
|
|
116
|
+
userHandlers.onChange?.(event)
|
|
117
|
+
updateValidityState('change', getValidityControl(event))
|
|
118
|
+
},
|
|
119
|
+
onFocus: (event) => {
|
|
120
|
+
userHandlers.onFocus?.(event)
|
|
121
|
+
updateValidityState('focus', getValidityControl(event))
|
|
122
|
+
},
|
|
123
|
+
onInput: (event) => {
|
|
124
|
+
userHandlers.onInput?.(event)
|
|
125
|
+
updateValidityState('input', getValidityControl(event))
|
|
126
|
+
},
|
|
127
|
+
onInvalid: (event) => {
|
|
128
|
+
userHandlers.onInvalid?.(event)
|
|
129
|
+
updateValidityState('invalid', getValidityControl(event))
|
|
130
|
+
},
|
|
101
131
|
}
|
|
102
132
|
}
|
|
103
133
|
|
|
@@ -6,7 +6,7 @@ import './Field.css'
|
|
|
6
6
|
export function useDescription(
|
|
7
7
|
options: UseDescriptionOptions,
|
|
8
8
|
): UseDescriptionResult {
|
|
9
|
-
const { children, className,
|
|
9
|
+
const { children, className, ...descriptionRootProps } = options
|
|
10
10
|
const field = useFieldContext('Description')
|
|
11
11
|
|
|
12
12
|
return {
|
|
@@ -14,7 +14,7 @@ export function useDescription(
|
|
|
14
14
|
...descriptionRootProps,
|
|
15
15
|
children: children ?? field.description,
|
|
16
16
|
className: clsx('maratus__Field__description', className),
|
|
17
|
-
id:
|
|
17
|
+
id: field.descriptionId,
|
|
18
18
|
},
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -12,7 +12,7 @@ import './Field.css'
|
|
|
12
12
|
export function useErrorMessage(
|
|
13
13
|
options: UseErrorMessageOptions,
|
|
14
14
|
): UseErrorMessageResult {
|
|
15
|
-
const { className,
|
|
15
|
+
const { className, ...errorMessageRootProps } = options
|
|
16
16
|
const field = useFieldContext('ErrorMessage')
|
|
17
17
|
const items = field.visibleErrors
|
|
18
18
|
.map((errorKey) => [errorKey, field.errorMap?.get(errorKey)] as const)
|
|
@@ -32,7 +32,7 @@ export function useErrorMessage(
|
|
|
32
32
|
errorMessageProps: {
|
|
33
33
|
...errorMessageRootProps,
|
|
34
34
|
className: clsx('maratus__Field__errorMessageRoot', className),
|
|
35
|
-
id:
|
|
35
|
+
id: field.errorId,
|
|
36
36
|
role: field.visibleErrors.length > 0 ? 'alert' : undefined,
|
|
37
37
|
},
|
|
38
38
|
items,
|
package/css-files/useLabel.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { useFieldContext } from './useFieldContext'
|
|
|
4
4
|
import './Field.css'
|
|
5
5
|
|
|
6
6
|
export function useLabel(options: UseLabelOptions): UseLabelResult {
|
|
7
|
-
const { children, className,
|
|
7
|
+
const { children, className, ...labelRootProps } = options
|
|
8
8
|
const field = useFieldContext('Label')
|
|
9
9
|
|
|
10
10
|
return {
|
|
@@ -15,8 +15,8 @@ export function useLabel(options: UseLabelOptions): UseLabelResult {
|
|
|
15
15
|
'data-loading': field.isLoading ? '' : undefined,
|
|
16
16
|
'data-readonly': field.isReadOnly ? '' : undefined,
|
|
17
17
|
'data-required': field.isRequired ? '' : undefined,
|
|
18
|
-
htmlFor:
|
|
19
|
-
id:
|
|
18
|
+
htmlFor: field.controlId,
|
|
19
|
+
id: field.labelId,
|
|
20
20
|
},
|
|
21
21
|
}
|
|
22
22
|
}
|
package/css-modules/Field.tsx
CHANGED
|
@@ -4,13 +4,11 @@ import type {
|
|
|
4
4
|
FieldErrorKey,
|
|
5
5
|
FieldErrorPolicy,
|
|
6
6
|
UseControlOptions,
|
|
7
|
+
UseDescriptionOptions,
|
|
8
|
+
UseErrorMessageOptions,
|
|
9
|
+
UseLabelOptions,
|
|
7
10
|
} from './Field.types'
|
|
8
|
-
import type {
|
|
9
|
-
ElementType,
|
|
10
|
-
HTMLAttributes,
|
|
11
|
-
LabelHTMLAttributes,
|
|
12
|
-
ReactNode,
|
|
13
|
-
} from 'react'
|
|
11
|
+
import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
|
|
14
12
|
import { FieldProvider } from './FieldContext'
|
|
15
13
|
import { useControl } from './useControl'
|
|
16
14
|
import { useDescription } from './useDescription'
|
|
@@ -18,7 +16,7 @@ import { useErrorMessage } from './useErrorMessage'
|
|
|
18
16
|
import { useFieldRoot } from './useFieldRoot'
|
|
19
17
|
import { useLabel } from './useLabel'
|
|
20
18
|
|
|
21
|
-
export type FieldRootProps =
|
|
19
|
+
export type FieldRootProps = ComponentPropsWithRef<'div'> & {
|
|
22
20
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
|
23
21
|
as?: ElementType
|
|
24
22
|
controlId?: string
|
|
@@ -67,7 +65,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
67
65
|
)
|
|
68
66
|
}
|
|
69
67
|
|
|
70
|
-
export type LabelProps =
|
|
68
|
+
export type LabelProps = UseLabelOptions
|
|
71
69
|
|
|
72
70
|
export function Label(props: LabelProps) {
|
|
73
71
|
const { labelProps } = useLabel(props)
|
|
@@ -86,7 +84,7 @@ export function Control(props: ControlProps) {
|
|
|
86
84
|
return children(control)
|
|
87
85
|
}
|
|
88
86
|
|
|
89
|
-
export type DescriptionProps =
|
|
87
|
+
export type DescriptionProps = UseDescriptionOptions & {
|
|
90
88
|
as?: ElementType
|
|
91
89
|
}
|
|
92
90
|
|
|
@@ -97,10 +95,7 @@ export function Description(props: DescriptionProps) {
|
|
|
97
95
|
return <Root {...descriptionProps} />
|
|
98
96
|
}
|
|
99
97
|
|
|
100
|
-
export type ErrorMessageProps =
|
|
101
|
-
HTMLAttributes<HTMLDivElement>,
|
|
102
|
-
'children'
|
|
103
|
-
> & {
|
|
98
|
+
export type ErrorMessageProps = UseErrorMessageOptions & {
|
|
104
99
|
as?: ElementType
|
|
105
100
|
renderChildren?: (props: ErrorMessageItemProps) => ReactNode
|
|
106
101
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AriaAttributes,
|
|
3
3
|
ChangeEventHandler,
|
|
4
|
+
ComponentPropsWithRef,
|
|
4
5
|
FocusEventHandler,
|
|
5
6
|
HTMLAttributes,
|
|
6
|
-
LabelHTMLAttributes,
|
|
7
7
|
ReactEventHandler,
|
|
8
8
|
ReactNode,
|
|
9
|
+
Ref,
|
|
9
10
|
} from 'react'
|
|
10
11
|
|
|
11
12
|
export type ValidityErrorKey = Exclude<keyof ValidityState, 'valid'>
|
|
@@ -45,7 +46,7 @@ export type FieldContextValue = {
|
|
|
45
46
|
descriptionId: string
|
|
46
47
|
errorId: string
|
|
47
48
|
errorMap?: ReadonlyMap<FieldErrorKey, ReactNode>
|
|
48
|
-
|
|
49
|
+
updateValidityState(
|
|
49
50
|
event: FieldErrorPolicyArgs['event'],
|
|
50
51
|
control: { validity: ValidityState },
|
|
51
52
|
): void
|
|
@@ -72,11 +73,11 @@ export type FieldProviderProps = {
|
|
|
72
73
|
name: string
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
export type UseFieldRootOptions =
|
|
76
|
+
export type UseFieldRootOptions = ComponentPropsWithRef<'div'> & {
|
|
76
77
|
isLoading?: boolean
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
export type FieldRootRenderProps =
|
|
80
|
+
export type FieldRootRenderProps = ComponentPropsWithRef<'div'> & {
|
|
80
81
|
'data-loading'?: ''
|
|
81
82
|
}
|
|
82
83
|
|
|
@@ -84,9 +85,14 @@ export type UseFieldRootResult = {
|
|
|
84
85
|
fieldRootProps: FieldRootRenderProps
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
|
|
88
|
+
// GPRD-006, REQ-002 and REQ-004: Field owns label relationship wiring so
|
|
89
|
+
// consumers cannot override the generated label id or control association.
|
|
90
|
+
export type UseLabelOptions = Omit<
|
|
91
|
+
ComponentPropsWithRef<'label'>,
|
|
92
|
+
'htmlFor' | 'id'
|
|
93
|
+
>
|
|
88
94
|
|
|
89
|
-
export type LabelRootProps =
|
|
95
|
+
export type LabelRootProps = ComponentPropsWithRef<'label'> & {
|
|
90
96
|
'data-loading'?: ''
|
|
91
97
|
'data-readonly'?: ''
|
|
92
98
|
'data-required'?: ''
|
|
@@ -125,6 +131,7 @@ export type ControlRenderProps = ControlElementProps & {
|
|
|
125
131
|
onInput?: ReactEventHandler<ControlElement>
|
|
126
132
|
onInvalid?: ReactEventHandler<ControlElement>
|
|
127
133
|
readOnly?: boolean
|
|
134
|
+
ref?: Ref<any>
|
|
128
135
|
required?: boolean
|
|
129
136
|
role?: ControlRole
|
|
130
137
|
}
|
|
@@ -177,15 +184,19 @@ export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
|
|
|
177
184
|
}
|
|
178
185
|
}
|
|
179
186
|
|
|
180
|
-
|
|
187
|
+
// GPRD-006 and REQ-005: Field owns the description id because Control uses it
|
|
188
|
+
// for aria-describedby.
|
|
189
|
+
export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
|
|
181
190
|
|
|
182
191
|
export type UseDescriptionResult = {
|
|
183
|
-
descriptionProps:
|
|
192
|
+
descriptionProps: ComponentPropsWithRef<'div'>
|
|
184
193
|
}
|
|
185
194
|
|
|
195
|
+
// GPRD-006 and REQ-007: Field owns the error id because Control uses it for
|
|
196
|
+
// aria-errormessage when errors are visible.
|
|
186
197
|
export type UseErrorMessageOptions = Omit<
|
|
187
|
-
|
|
188
|
-
'children'
|
|
198
|
+
ComponentPropsWithRef<'div'>,
|
|
199
|
+
'children' | 'id'
|
|
189
200
|
>
|
|
190
201
|
|
|
191
202
|
export type ErrorMessageItemProps = {
|
|
@@ -196,6 +207,6 @@ export type ErrorMessageItemProps = {
|
|
|
196
207
|
}
|
|
197
208
|
|
|
198
209
|
export type UseErrorMessageResult = {
|
|
199
|
-
errorMessageProps:
|
|
210
|
+
errorMessageProps: ComponentPropsWithRef<'div'>
|
|
200
211
|
items: readonly ErrorMessageItemProps[]
|
|
201
212
|
}
|
|
@@ -68,21 +68,7 @@ function useFieldProviderValue({
|
|
|
68
68
|
form: defaultFormState,
|
|
69
69
|
nativeState,
|
|
70
70
|
})
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
nativeState.isErrorVisible !== nextIsErrorVisible ||
|
|
75
|
-
(nextIsErrorVisible && !nativeState.field.wasErrored)
|
|
76
|
-
) {
|
|
77
|
-
setNativeState({
|
|
78
|
-
...nativeState,
|
|
79
|
-
field: {
|
|
80
|
-
...nativeState.field,
|
|
81
|
-
wasErrored: nativeState.field.wasErrored || nextIsErrorVisible,
|
|
82
|
-
},
|
|
83
|
-
isErrorVisible: nextIsErrorVisible,
|
|
84
|
-
})
|
|
85
|
-
}
|
|
71
|
+
const isErrorVisible = visibleErrors.length > 0
|
|
86
72
|
|
|
87
73
|
return {
|
|
88
74
|
activeErrors,
|
|
@@ -91,29 +77,20 @@ function useFieldProviderValue({
|
|
|
91
77
|
descriptionId: `${generatedId}-description`,
|
|
92
78
|
errorId: `${generatedId}-error`,
|
|
93
79
|
errorMap,
|
|
94
|
-
|
|
80
|
+
updateValidityState: (event, control) => {
|
|
95
81
|
const nextErrors = getValidityErrorKeys(control.validity)
|
|
82
|
+
const nextActiveErrors = activeErrors ?? new Set(nextErrors)
|
|
96
83
|
|
|
97
|
-
setNativeState((currentNativeState) =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
wasTouched:
|
|
108
|
-
currentNativeState.field.wasTouched ||
|
|
109
|
-
event === 'focus' ||
|
|
110
|
-
event === 'blur' ||
|
|
111
|
-
event === 'change' ||
|
|
112
|
-
event === 'input' ||
|
|
113
|
-
event === 'invalid',
|
|
114
|
-
},
|
|
115
|
-
isErrorVisible: currentNativeState.isErrorVisible,
|
|
116
|
-
}))
|
|
84
|
+
setNativeState((currentNativeState) =>
|
|
85
|
+
getNextNativeState({
|
|
86
|
+
currentNativeState,
|
|
87
|
+
errorPolicy,
|
|
88
|
+
event,
|
|
89
|
+
isErrorVisible,
|
|
90
|
+
nextActiveErrors,
|
|
91
|
+
nextErrors,
|
|
92
|
+
}),
|
|
93
|
+
)
|
|
117
94
|
},
|
|
118
95
|
isLoading,
|
|
119
96
|
isReadOnly,
|
|
@@ -125,6 +102,59 @@ function useFieldProviderValue({
|
|
|
125
102
|
}
|
|
126
103
|
}
|
|
127
104
|
|
|
105
|
+
function getNextNativeState({
|
|
106
|
+
currentNativeState,
|
|
107
|
+
errorPolicy,
|
|
108
|
+
event,
|
|
109
|
+
isErrorVisible,
|
|
110
|
+
nextActiveErrors,
|
|
111
|
+
nextErrors,
|
|
112
|
+
}: {
|
|
113
|
+
currentNativeState: FieldNativeState
|
|
114
|
+
errorPolicy: FieldErrorPolicy
|
|
115
|
+
event: FieldErrorPolicyArgs['event']
|
|
116
|
+
isErrorVisible: boolean
|
|
117
|
+
nextActiveErrors: ReadonlySet<FieldErrorKey>
|
|
118
|
+
nextErrors: readonly FieldErrorKey[]
|
|
119
|
+
}): FieldNativeState {
|
|
120
|
+
const eventNativeState: FieldNativeState = {
|
|
121
|
+
errors: nextErrors,
|
|
122
|
+
event,
|
|
123
|
+
field: {
|
|
124
|
+
wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
|
|
125
|
+
wasChanged:
|
|
126
|
+
currentNativeState.field.wasChanged ||
|
|
127
|
+
event === 'change' ||
|
|
128
|
+
event === 'input',
|
|
129
|
+
wasErrored: currentNativeState.field.wasErrored || isErrorVisible,
|
|
130
|
+
wasTouched:
|
|
131
|
+
currentNativeState.field.wasTouched ||
|
|
132
|
+
event === 'focus' ||
|
|
133
|
+
event === 'blur' ||
|
|
134
|
+
event === 'change' ||
|
|
135
|
+
event === 'input' ||
|
|
136
|
+
event === 'invalid',
|
|
137
|
+
},
|
|
138
|
+
isErrorVisible,
|
|
139
|
+
}
|
|
140
|
+
const nextVisibleErrors = resolveVisibleErrors({
|
|
141
|
+
activeErrors: nextActiveErrors,
|
|
142
|
+
errorPolicy,
|
|
143
|
+
form: defaultFormState,
|
|
144
|
+
nativeState: eventNativeState,
|
|
145
|
+
})
|
|
146
|
+
const nextIsErrorVisible = nextVisibleErrors.length > 0
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
...eventNativeState,
|
|
150
|
+
field: {
|
|
151
|
+
...eventNativeState.field,
|
|
152
|
+
wasErrored: eventNativeState.field.wasErrored || nextIsErrorVisible,
|
|
153
|
+
},
|
|
154
|
+
isErrorVisible: nextIsErrorVisible,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
128
158
|
const validityErrorKeys = [
|
|
129
159
|
'badInput',
|
|
130
160
|
'customError',
|
|
@@ -11,7 +11,16 @@ import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
|
|
|
11
11
|
import styles from './Field.module.css'
|
|
12
12
|
|
|
13
13
|
export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
className,
|
|
16
|
+
onBlur,
|
|
17
|
+
onChange,
|
|
18
|
+
onFocus,
|
|
19
|
+
onInput,
|
|
20
|
+
onInvalid,
|
|
21
|
+
role,
|
|
22
|
+
...controlRootProps
|
|
23
|
+
} = options
|
|
15
24
|
const field = useRequiredFieldContext('Control')
|
|
16
25
|
const relationshipProps = {
|
|
17
26
|
'aria-busy': field.isLoading ? true : undefined,
|
|
@@ -34,8 +43,15 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
|
34
43
|
| 'data-loading'
|
|
35
44
|
| 'id'
|
|
36
45
|
>
|
|
37
|
-
const validityHandlerProps =
|
|
38
|
-
|
|
46
|
+
const validityHandlerProps = composeValidityHandlerProps(
|
|
47
|
+
{
|
|
48
|
+
onBlur,
|
|
49
|
+
onChange,
|
|
50
|
+
onFocus,
|
|
51
|
+
onInput,
|
|
52
|
+
onInvalid,
|
|
53
|
+
},
|
|
54
|
+
field.updateValidityState,
|
|
39
55
|
)
|
|
40
56
|
|
|
41
57
|
if (role) {
|
|
@@ -81,23 +97,37 @@ const withValidity: WithValidity = (event, validity) => {
|
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
function
|
|
85
|
-
|
|
100
|
+
function composeValidityHandlerProps(
|
|
101
|
+
userHandlers: Pick<
|
|
102
|
+
UseControlOptions,
|
|
103
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
104
|
+
>,
|
|
105
|
+
updateValidityState: FieldContextValue['updateValidityState'],
|
|
86
106
|
): Pick<
|
|
87
107
|
ControlRenderProps,
|
|
88
108
|
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
109
|
> {
|
|
90
110
|
return {
|
|
91
|
-
onBlur: (event) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
onBlur: (event) => {
|
|
112
|
+
userHandlers.onBlur?.(event)
|
|
113
|
+
updateValidityState('blur', getValidityControl(event))
|
|
114
|
+
},
|
|
115
|
+
onChange: (event) => {
|
|
116
|
+
userHandlers.onChange?.(event)
|
|
117
|
+
updateValidityState('change', getValidityControl(event))
|
|
118
|
+
},
|
|
119
|
+
onFocus: (event) => {
|
|
120
|
+
userHandlers.onFocus?.(event)
|
|
121
|
+
updateValidityState('focus', getValidityControl(event))
|
|
122
|
+
},
|
|
123
|
+
onInput: (event) => {
|
|
124
|
+
userHandlers.onInput?.(event)
|
|
125
|
+
updateValidityState('input', getValidityControl(event))
|
|
126
|
+
},
|
|
127
|
+
onInvalid: (event) => {
|
|
128
|
+
userHandlers.onInvalid?.(event)
|
|
129
|
+
updateValidityState('invalid', getValidityControl(event))
|
|
130
|
+
},
|
|
101
131
|
}
|
|
102
132
|
}
|
|
103
133
|
|
|
@@ -6,7 +6,7 @@ import styles from './Field.module.css'
|
|
|
6
6
|
export function useDescription(
|
|
7
7
|
options: UseDescriptionOptions,
|
|
8
8
|
): UseDescriptionResult {
|
|
9
|
-
const { children, className,
|
|
9
|
+
const { children, className, ...descriptionRootProps } = options
|
|
10
10
|
const field = useFieldContext('Description')
|
|
11
11
|
|
|
12
12
|
return {
|
|
@@ -14,7 +14,7 @@ export function useDescription(
|
|
|
14
14
|
...descriptionRootProps,
|
|
15
15
|
children: children ?? field.description,
|
|
16
16
|
className: clsx(styles.description, className),
|
|
17
|
-
id:
|
|
17
|
+
id: field.descriptionId,
|
|
18
18
|
},
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -12,7 +12,7 @@ import styles from './Field.module.css'
|
|
|
12
12
|
export function useErrorMessage(
|
|
13
13
|
options: UseErrorMessageOptions,
|
|
14
14
|
): UseErrorMessageResult {
|
|
15
|
-
const { className,
|
|
15
|
+
const { className, ...errorMessageRootProps } = options
|
|
16
16
|
const field = useFieldContext('ErrorMessage')
|
|
17
17
|
const items = field.visibleErrors
|
|
18
18
|
.map((errorKey) => [errorKey, field.errorMap?.get(errorKey)] as const)
|
|
@@ -32,7 +32,7 @@ export function useErrorMessage(
|
|
|
32
32
|
errorMessageProps: {
|
|
33
33
|
...errorMessageRootProps,
|
|
34
34
|
className: clsx(styles.errorMessageRoot, className),
|
|
35
|
-
id:
|
|
35
|
+
id: field.errorId,
|
|
36
36
|
role: field.visibleErrors.length > 0 ? 'alert' : undefined,
|
|
37
37
|
},
|
|
38
38
|
items,
|
package/css-modules/useLabel.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { useFieldContext } from './useFieldContext'
|
|
|
4
4
|
import styles from './Field.module.css'
|
|
5
5
|
|
|
6
6
|
export function useLabel(options: UseLabelOptions): UseLabelResult {
|
|
7
|
-
const { children, className,
|
|
7
|
+
const { children, className, ...labelRootProps } = options
|
|
8
8
|
const field = useFieldContext('Label')
|
|
9
9
|
|
|
10
10
|
return {
|
|
@@ -15,8 +15,8 @@ export function useLabel(options: UseLabelOptions): UseLabelResult {
|
|
|
15
15
|
'data-loading': field.isLoading ? '' : undefined,
|
|
16
16
|
'data-readonly': field.isReadOnly ? '' : undefined,
|
|
17
17
|
'data-required': field.isRequired ? '' : undefined,
|
|
18
|
-
htmlFor:
|
|
19
|
-
id:
|
|
18
|
+
htmlFor: field.controlId,
|
|
19
|
+
id: field.labelId,
|
|
20
20
|
},
|
|
21
21
|
}
|
|
22
22
|
}
|
package/package.json
CHANGED
package/tailwind-css/Field.tsx
CHANGED
|
@@ -4,13 +4,11 @@ import type {
|
|
|
4
4
|
FieldErrorKey,
|
|
5
5
|
FieldErrorPolicy,
|
|
6
6
|
UseControlOptions,
|
|
7
|
+
UseDescriptionOptions,
|
|
8
|
+
UseErrorMessageOptions,
|
|
9
|
+
UseLabelOptions,
|
|
7
10
|
} from './Field.types'
|
|
8
|
-
import type {
|
|
9
|
-
ElementType,
|
|
10
|
-
HTMLAttributes,
|
|
11
|
-
LabelHTMLAttributes,
|
|
12
|
-
ReactNode,
|
|
13
|
-
} from 'react'
|
|
11
|
+
import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
|
|
14
12
|
import { FieldProvider } from './FieldContext'
|
|
15
13
|
import { useControl } from './useControl'
|
|
16
14
|
import { useDescription } from './useDescription'
|
|
@@ -18,7 +16,7 @@ import { useErrorMessage } from './useErrorMessage'
|
|
|
18
16
|
import { useFieldRoot } from './useFieldRoot'
|
|
19
17
|
import { useLabel } from './useLabel'
|
|
20
18
|
|
|
21
|
-
export type FieldRootProps =
|
|
19
|
+
export type FieldRootProps = ComponentPropsWithRef<'div'> & {
|
|
22
20
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
|
23
21
|
as?: ElementType
|
|
24
22
|
controlId?: string
|
|
@@ -67,7 +65,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
67
65
|
)
|
|
68
66
|
}
|
|
69
67
|
|
|
70
|
-
export type LabelProps =
|
|
68
|
+
export type LabelProps = UseLabelOptions
|
|
71
69
|
|
|
72
70
|
export function Label(props: LabelProps) {
|
|
73
71
|
const { labelProps } = useLabel(props)
|
|
@@ -86,7 +84,7 @@ export function Control(props: ControlProps) {
|
|
|
86
84
|
return children(control)
|
|
87
85
|
}
|
|
88
86
|
|
|
89
|
-
export type DescriptionProps =
|
|
87
|
+
export type DescriptionProps = UseDescriptionOptions & {
|
|
90
88
|
as?: ElementType
|
|
91
89
|
}
|
|
92
90
|
|
|
@@ -97,10 +95,7 @@ export function Description(props: DescriptionProps) {
|
|
|
97
95
|
return <Root {...descriptionProps} />
|
|
98
96
|
}
|
|
99
97
|
|
|
100
|
-
export type ErrorMessageProps =
|
|
101
|
-
HTMLAttributes<HTMLDivElement>,
|
|
102
|
-
'children'
|
|
103
|
-
> & {
|
|
98
|
+
export type ErrorMessageProps = UseErrorMessageOptions & {
|
|
104
99
|
as?: ElementType
|
|
105
100
|
renderChildren?: (props: ErrorMessageItemProps) => ReactNode
|
|
106
101
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AriaAttributes,
|
|
3
3
|
ChangeEventHandler,
|
|
4
|
+
ComponentPropsWithRef,
|
|
4
5
|
FocusEventHandler,
|
|
5
6
|
HTMLAttributes,
|
|
6
|
-
LabelHTMLAttributes,
|
|
7
7
|
ReactEventHandler,
|
|
8
8
|
ReactNode,
|
|
9
|
+
Ref,
|
|
9
10
|
} from 'react'
|
|
10
11
|
|
|
11
12
|
export type ValidityErrorKey = Exclude<keyof ValidityState, 'valid'>
|
|
@@ -45,7 +46,7 @@ export type FieldContextValue = {
|
|
|
45
46
|
descriptionId: string
|
|
46
47
|
errorId: string
|
|
47
48
|
errorMap?: ReadonlyMap<FieldErrorKey, ReactNode>
|
|
48
|
-
|
|
49
|
+
updateValidityState(
|
|
49
50
|
event: FieldErrorPolicyArgs['event'],
|
|
50
51
|
control: { validity: ValidityState },
|
|
51
52
|
): void
|
|
@@ -72,11 +73,11 @@ export type FieldProviderProps = {
|
|
|
72
73
|
name: string
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
export type UseFieldRootOptions =
|
|
76
|
+
export type UseFieldRootOptions = ComponentPropsWithRef<'div'> & {
|
|
76
77
|
isLoading?: boolean
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
export type FieldRootRenderProps =
|
|
80
|
+
export type FieldRootRenderProps = ComponentPropsWithRef<'div'> & {
|
|
80
81
|
'data-loading'?: ''
|
|
81
82
|
}
|
|
82
83
|
|
|
@@ -84,9 +85,14 @@ export type UseFieldRootResult = {
|
|
|
84
85
|
fieldRootProps: FieldRootRenderProps
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
|
|
88
|
+
// GPRD-006, REQ-002 and REQ-004: Field owns label relationship wiring so
|
|
89
|
+
// consumers cannot override the generated label id or control association.
|
|
90
|
+
export type UseLabelOptions = Omit<
|
|
91
|
+
ComponentPropsWithRef<'label'>,
|
|
92
|
+
'htmlFor' | 'id'
|
|
93
|
+
>
|
|
88
94
|
|
|
89
|
-
export type LabelRootProps =
|
|
95
|
+
export type LabelRootProps = ComponentPropsWithRef<'label'> & {
|
|
90
96
|
'data-loading'?: ''
|
|
91
97
|
'data-readonly'?: ''
|
|
92
98
|
'data-required'?: ''
|
|
@@ -125,6 +131,7 @@ export type ControlRenderProps = ControlElementProps & {
|
|
|
125
131
|
onInput?: ReactEventHandler<ControlElement>
|
|
126
132
|
onInvalid?: ReactEventHandler<ControlElement>
|
|
127
133
|
readOnly?: boolean
|
|
134
|
+
ref?: Ref<any>
|
|
128
135
|
required?: boolean
|
|
129
136
|
role?: ControlRole
|
|
130
137
|
}
|
|
@@ -177,15 +184,19 @@ export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
|
|
|
177
184
|
}
|
|
178
185
|
}
|
|
179
186
|
|
|
180
|
-
|
|
187
|
+
// GPRD-006 and REQ-005: Field owns the description id because Control uses it
|
|
188
|
+
// for aria-describedby.
|
|
189
|
+
export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
|
|
181
190
|
|
|
182
191
|
export type UseDescriptionResult = {
|
|
183
|
-
descriptionProps:
|
|
192
|
+
descriptionProps: ComponentPropsWithRef<'div'>
|
|
184
193
|
}
|
|
185
194
|
|
|
195
|
+
// GPRD-006 and REQ-007: Field owns the error id because Control uses it for
|
|
196
|
+
// aria-errormessage when errors are visible.
|
|
186
197
|
export type UseErrorMessageOptions = Omit<
|
|
187
|
-
|
|
188
|
-
'children'
|
|
198
|
+
ComponentPropsWithRef<'div'>,
|
|
199
|
+
'children' | 'id'
|
|
189
200
|
>
|
|
190
201
|
|
|
191
202
|
export type ErrorMessageItemProps = {
|
|
@@ -196,6 +207,6 @@ export type ErrorMessageItemProps = {
|
|
|
196
207
|
}
|
|
197
208
|
|
|
198
209
|
export type UseErrorMessageResult = {
|
|
199
|
-
errorMessageProps:
|
|
210
|
+
errorMessageProps: ComponentPropsWithRef<'div'>
|
|
200
211
|
items: readonly ErrorMessageItemProps[]
|
|
201
212
|
}
|
|
@@ -68,21 +68,7 @@ function useFieldProviderValue({
|
|
|
68
68
|
form: defaultFormState,
|
|
69
69
|
nativeState,
|
|
70
70
|
})
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
nativeState.isErrorVisible !== nextIsErrorVisible ||
|
|
75
|
-
(nextIsErrorVisible && !nativeState.field.wasErrored)
|
|
76
|
-
) {
|
|
77
|
-
setNativeState({
|
|
78
|
-
...nativeState,
|
|
79
|
-
field: {
|
|
80
|
-
...nativeState.field,
|
|
81
|
-
wasErrored: nativeState.field.wasErrored || nextIsErrorVisible,
|
|
82
|
-
},
|
|
83
|
-
isErrorVisible: nextIsErrorVisible,
|
|
84
|
-
})
|
|
85
|
-
}
|
|
71
|
+
const isErrorVisible = visibleErrors.length > 0
|
|
86
72
|
|
|
87
73
|
return {
|
|
88
74
|
activeErrors,
|
|
@@ -91,29 +77,20 @@ function useFieldProviderValue({
|
|
|
91
77
|
descriptionId: `${generatedId}-description`,
|
|
92
78
|
errorId: `${generatedId}-error`,
|
|
93
79
|
errorMap,
|
|
94
|
-
|
|
80
|
+
updateValidityState: (event, control) => {
|
|
95
81
|
const nextErrors = getValidityErrorKeys(control.validity)
|
|
82
|
+
const nextActiveErrors = activeErrors ?? new Set(nextErrors)
|
|
96
83
|
|
|
97
|
-
setNativeState((currentNativeState) =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
wasTouched:
|
|
108
|
-
currentNativeState.field.wasTouched ||
|
|
109
|
-
event === 'focus' ||
|
|
110
|
-
event === 'blur' ||
|
|
111
|
-
event === 'change' ||
|
|
112
|
-
event === 'input' ||
|
|
113
|
-
event === 'invalid',
|
|
114
|
-
},
|
|
115
|
-
isErrorVisible: currentNativeState.isErrorVisible,
|
|
116
|
-
}))
|
|
84
|
+
setNativeState((currentNativeState) =>
|
|
85
|
+
getNextNativeState({
|
|
86
|
+
currentNativeState,
|
|
87
|
+
errorPolicy,
|
|
88
|
+
event,
|
|
89
|
+
isErrorVisible,
|
|
90
|
+
nextActiveErrors,
|
|
91
|
+
nextErrors,
|
|
92
|
+
}),
|
|
93
|
+
)
|
|
117
94
|
},
|
|
118
95
|
isLoading,
|
|
119
96
|
isReadOnly,
|
|
@@ -125,6 +102,59 @@ function useFieldProviderValue({
|
|
|
125
102
|
}
|
|
126
103
|
}
|
|
127
104
|
|
|
105
|
+
function getNextNativeState({
|
|
106
|
+
currentNativeState,
|
|
107
|
+
errorPolicy,
|
|
108
|
+
event,
|
|
109
|
+
isErrorVisible,
|
|
110
|
+
nextActiveErrors,
|
|
111
|
+
nextErrors,
|
|
112
|
+
}: {
|
|
113
|
+
currentNativeState: FieldNativeState
|
|
114
|
+
errorPolicy: FieldErrorPolicy
|
|
115
|
+
event: FieldErrorPolicyArgs['event']
|
|
116
|
+
isErrorVisible: boolean
|
|
117
|
+
nextActiveErrors: ReadonlySet<FieldErrorKey>
|
|
118
|
+
nextErrors: readonly FieldErrorKey[]
|
|
119
|
+
}): FieldNativeState {
|
|
120
|
+
const eventNativeState: FieldNativeState = {
|
|
121
|
+
errors: nextErrors,
|
|
122
|
+
event,
|
|
123
|
+
field: {
|
|
124
|
+
wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
|
|
125
|
+
wasChanged:
|
|
126
|
+
currentNativeState.field.wasChanged ||
|
|
127
|
+
event === 'change' ||
|
|
128
|
+
event === 'input',
|
|
129
|
+
wasErrored: currentNativeState.field.wasErrored || isErrorVisible,
|
|
130
|
+
wasTouched:
|
|
131
|
+
currentNativeState.field.wasTouched ||
|
|
132
|
+
event === 'focus' ||
|
|
133
|
+
event === 'blur' ||
|
|
134
|
+
event === 'change' ||
|
|
135
|
+
event === 'input' ||
|
|
136
|
+
event === 'invalid',
|
|
137
|
+
},
|
|
138
|
+
isErrorVisible,
|
|
139
|
+
}
|
|
140
|
+
const nextVisibleErrors = resolveVisibleErrors({
|
|
141
|
+
activeErrors: nextActiveErrors,
|
|
142
|
+
errorPolicy,
|
|
143
|
+
form: defaultFormState,
|
|
144
|
+
nativeState: eventNativeState,
|
|
145
|
+
})
|
|
146
|
+
const nextIsErrorVisible = nextVisibleErrors.length > 0
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
...eventNativeState,
|
|
150
|
+
field: {
|
|
151
|
+
...eventNativeState.field,
|
|
152
|
+
wasErrored: eventNativeState.field.wasErrored || nextIsErrorVisible,
|
|
153
|
+
},
|
|
154
|
+
isErrorVisible: nextIsErrorVisible,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
128
158
|
const validityErrorKeys = [
|
|
129
159
|
'badInput',
|
|
130
160
|
'customError',
|
|
@@ -11,7 +11,16 @@ import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
|
|
|
11
11
|
import './Field.css'
|
|
12
12
|
|
|
13
13
|
export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
className,
|
|
16
|
+
onBlur,
|
|
17
|
+
onChange,
|
|
18
|
+
onFocus,
|
|
19
|
+
onInput,
|
|
20
|
+
onInvalid,
|
|
21
|
+
role,
|
|
22
|
+
...controlRootProps
|
|
23
|
+
} = options
|
|
15
24
|
const field = useRequiredFieldContext('Control')
|
|
16
25
|
const relationshipProps = {
|
|
17
26
|
'aria-busy': field.isLoading ? true : undefined,
|
|
@@ -34,8 +43,15 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
|
34
43
|
| 'data-loading'
|
|
35
44
|
| 'id'
|
|
36
45
|
>
|
|
37
|
-
const validityHandlerProps =
|
|
38
|
-
|
|
46
|
+
const validityHandlerProps = composeValidityHandlerProps(
|
|
47
|
+
{
|
|
48
|
+
onBlur,
|
|
49
|
+
onChange,
|
|
50
|
+
onFocus,
|
|
51
|
+
onInput,
|
|
52
|
+
onInvalid,
|
|
53
|
+
},
|
|
54
|
+
field.updateValidityState,
|
|
39
55
|
)
|
|
40
56
|
|
|
41
57
|
if (role) {
|
|
@@ -81,23 +97,37 @@ const withValidity: WithValidity = (event, validity) => {
|
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
function
|
|
85
|
-
|
|
100
|
+
function composeValidityHandlerProps(
|
|
101
|
+
userHandlers: Pick<
|
|
102
|
+
UseControlOptions,
|
|
103
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
104
|
+
>,
|
|
105
|
+
updateValidityState: FieldContextValue['updateValidityState'],
|
|
86
106
|
): Pick<
|
|
87
107
|
ControlRenderProps,
|
|
88
108
|
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
109
|
> {
|
|
90
110
|
return {
|
|
91
|
-
onBlur: (event) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
onBlur: (event) => {
|
|
112
|
+
userHandlers.onBlur?.(event)
|
|
113
|
+
updateValidityState('blur', getValidityControl(event))
|
|
114
|
+
},
|
|
115
|
+
onChange: (event) => {
|
|
116
|
+
userHandlers.onChange?.(event)
|
|
117
|
+
updateValidityState('change', getValidityControl(event))
|
|
118
|
+
},
|
|
119
|
+
onFocus: (event) => {
|
|
120
|
+
userHandlers.onFocus?.(event)
|
|
121
|
+
updateValidityState('focus', getValidityControl(event))
|
|
122
|
+
},
|
|
123
|
+
onInput: (event) => {
|
|
124
|
+
userHandlers.onInput?.(event)
|
|
125
|
+
updateValidityState('input', getValidityControl(event))
|
|
126
|
+
},
|
|
127
|
+
onInvalid: (event) => {
|
|
128
|
+
userHandlers.onInvalid?.(event)
|
|
129
|
+
updateValidityState('invalid', getValidityControl(event))
|
|
130
|
+
},
|
|
101
131
|
}
|
|
102
132
|
}
|
|
103
133
|
|
|
@@ -6,7 +6,7 @@ import './Field.css'
|
|
|
6
6
|
export function useDescription(
|
|
7
7
|
options: UseDescriptionOptions,
|
|
8
8
|
): UseDescriptionResult {
|
|
9
|
-
const { children, className,
|
|
9
|
+
const { children, className, ...descriptionRootProps } = options
|
|
10
10
|
const field = useFieldContext('Description')
|
|
11
11
|
|
|
12
12
|
return {
|
|
@@ -14,7 +14,7 @@ export function useDescription(
|
|
|
14
14
|
...descriptionRootProps,
|
|
15
15
|
children: children ?? field.description,
|
|
16
16
|
className: clsx('maratus__Field__description', className),
|
|
17
|
-
id:
|
|
17
|
+
id: field.descriptionId,
|
|
18
18
|
},
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -12,7 +12,7 @@ import './Field.css'
|
|
|
12
12
|
export function useErrorMessage(
|
|
13
13
|
options: UseErrorMessageOptions,
|
|
14
14
|
): UseErrorMessageResult {
|
|
15
|
-
const { className,
|
|
15
|
+
const { className, ...errorMessageRootProps } = options
|
|
16
16
|
const field = useFieldContext('ErrorMessage')
|
|
17
17
|
const items = field.visibleErrors
|
|
18
18
|
.map((errorKey) => [errorKey, field.errorMap?.get(errorKey)] as const)
|
|
@@ -32,7 +32,7 @@ export function useErrorMessage(
|
|
|
32
32
|
errorMessageProps: {
|
|
33
33
|
...errorMessageRootProps,
|
|
34
34
|
className: clsx('maratus__Field__errorMessageRoot', className),
|
|
35
|
-
id:
|
|
35
|
+
id: field.errorId,
|
|
36
36
|
role: field.visibleErrors.length > 0 ? 'alert' : undefined,
|
|
37
37
|
},
|
|
38
38
|
items,
|
package/tailwind-css/useLabel.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { useFieldContext } from './useFieldContext'
|
|
|
4
4
|
import './Field.css'
|
|
5
5
|
|
|
6
6
|
export function useLabel(options: UseLabelOptions): UseLabelResult {
|
|
7
|
-
const { children, className,
|
|
7
|
+
const { children, className, ...labelRootProps } = options
|
|
8
8
|
const field = useFieldContext('Label')
|
|
9
9
|
|
|
10
10
|
return {
|
|
@@ -15,8 +15,8 @@ export function useLabel(options: UseLabelOptions): UseLabelResult {
|
|
|
15
15
|
'data-loading': field.isLoading ? '' : undefined,
|
|
16
16
|
'data-readonly': field.isReadOnly ? '' : undefined,
|
|
17
17
|
'data-required': field.isRequired ? '' : undefined,
|
|
18
|
-
htmlFor:
|
|
19
|
-
id:
|
|
18
|
+
htmlFor: field.controlId,
|
|
19
|
+
id: field.labelId,
|
|
20
20
|
},
|
|
21
21
|
}
|
|
22
22
|
}
|