@maratus-registry/field 0.2.0 → 0.3.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/css-files/Field.css +5 -5
- package/css-files/Field.tsx +13 -13
- package/css-files/Field.types.ts +81 -70
- package/css-files/FieldContext.tsx +111 -57
- package/css-files/fieldValidity.ts +62 -0
- package/css-files/index.ts +17 -9
- package/css-files/useDescription.ts +2 -2
- package/css-files/useErrorMessage.ts +2 -2
- package/css-files/useFieldControl.ts +80 -0
- package/css-files/useLabel.ts +3 -3
- package/css-files/validityReport.ts +41 -0
- package/css-modules/Field.module.css +5 -5
- package/css-modules/Field.tsx +13 -13
- package/css-modules/Field.types.ts +81 -70
- package/css-modules/FieldContext.tsx +111 -57
- package/css-modules/fieldValidity.ts +62 -0
- package/css-modules/index.ts +17 -9
- package/css-modules/useDescription.ts +2 -2
- package/css-modules/useErrorMessage.ts +2 -2
- package/css-modules/useFieldControl.ts +80 -0
- package/css-modules/useLabel.ts +3 -3
- package/css-modules/validityReport.ts +41 -0
- package/package.json +4 -1
- package/tailwind-css/Field.css +5 -5
- package/tailwind-css/Field.tsx +13 -13
- package/tailwind-css/Field.types.ts +81 -70
- package/tailwind-css/FieldContext.tsx +111 -57
- package/tailwind-css/fieldValidity.ts +62 -0
- package/tailwind-css/index.ts +17 -9
- package/tailwind-css/useDescription.ts +2 -2
- package/tailwind-css/useErrorMessage.ts +2 -2
- package/tailwind-css/useFieldControl.ts +80 -0
- package/tailwind-css/useLabel.ts +3 -3
- package/tailwind-css/validityReport.ts +41 -0
- package/css-files/useControl.ts +0 -161
- package/css-modules/useControl.ts +0 -161
- package/tailwind-css/useControl.ts +0 -161
package/css-files/Field.css
CHANGED
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
color: var(--ara-field-label-fg--loading);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
.
|
|
51
|
+
.maratus__Field__fieldControl {
|
|
52
52
|
background-color: var(--ara-field-control-bg);
|
|
53
53
|
border-color: var(--ara-field-control-detail);
|
|
54
54
|
border-style: var(--ara-field-control-border-style);
|
|
@@ -58,18 +58,18 @@
|
|
|
58
58
|
padding-inline: var(--ara-field-control-padding-inline);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
.
|
|
61
|
+
.maratus__Field__fieldControl[aria-invalid='true'] {
|
|
62
62
|
border-color: var(--ara-field-control-detail--invalid);
|
|
63
63
|
color: var(--ara-field-control-fg--invalid);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
.
|
|
66
|
+
.maratus__Field__fieldControl[data-loading] {
|
|
67
67
|
border-color: var(--ara-field-control-detail--loading);
|
|
68
68
|
color: var(--ara-field-control-fg--loading);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
.
|
|
72
|
-
.
|
|
71
|
+
.maratus__Field__fieldControl[readonly],
|
|
72
|
+
.maratus__Field__fieldControl[aria-readonly='true'] {
|
|
73
73
|
color: var(--ara-field-control-fg--readonly);
|
|
74
74
|
}
|
|
75
75
|
|
package/css-files/Field.tsx
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
ControlRenderArgs,
|
|
3
2
|
ErrorMessageItemProps,
|
|
4
3
|
FieldErrorKey,
|
|
4
|
+
FieldControlRenderArgs,
|
|
5
5
|
FieldErrorPolicy,
|
|
6
|
-
|
|
6
|
+
UseDescriptionOptions,
|
|
7
|
+
UseErrorMessageOptions,
|
|
8
|
+
UseFieldControlOptions,
|
|
9
|
+
UseLabelOptions,
|
|
7
10
|
} from './Field.types'
|
|
8
11
|
import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
|
|
9
12
|
import { FieldProvider } from './FieldContext'
|
|
10
|
-
import { useControl } from './useControl'
|
|
11
13
|
import { useDescription } from './useDescription'
|
|
12
14
|
import { useErrorMessage } from './useErrorMessage'
|
|
15
|
+
import { useFieldControl } from './useFieldControl'
|
|
13
16
|
import { useFieldRoot } from './useFieldRoot'
|
|
14
17
|
import { useLabel } from './useLabel'
|
|
15
18
|
|
|
@@ -62,7 +65,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
62
65
|
)
|
|
63
66
|
}
|
|
64
67
|
|
|
65
|
-
export type LabelProps =
|
|
68
|
+
export type LabelProps = UseLabelOptions
|
|
66
69
|
|
|
67
70
|
export function Label(props: LabelProps) {
|
|
68
71
|
const { labelProps } = useLabel(props)
|
|
@@ -70,18 +73,18 @@ export function Label(props: LabelProps) {
|
|
|
70
73
|
return <label {...labelProps} />
|
|
71
74
|
}
|
|
72
75
|
|
|
73
|
-
export type
|
|
74
|
-
children: (props:
|
|
76
|
+
export type FieldControlProps = UseFieldControlOptions & {
|
|
77
|
+
children: (props: FieldControlRenderArgs) => ReactNode
|
|
75
78
|
}
|
|
76
79
|
|
|
77
|
-
export function
|
|
80
|
+
export function FieldControl(props: FieldControlProps) {
|
|
78
81
|
const { children, ...hookProps } = props
|
|
79
|
-
const control =
|
|
82
|
+
const control = useFieldControl(hookProps)
|
|
80
83
|
|
|
81
84
|
return children(control)
|
|
82
85
|
}
|
|
83
86
|
|
|
84
|
-
export type DescriptionProps =
|
|
87
|
+
export type DescriptionProps = UseDescriptionOptions & {
|
|
85
88
|
as?: ElementType
|
|
86
89
|
}
|
|
87
90
|
|
|
@@ -92,10 +95,7 @@ export function Description(props: DescriptionProps) {
|
|
|
92
95
|
return <Root {...descriptionProps} />
|
|
93
96
|
}
|
|
94
97
|
|
|
95
|
-
export type ErrorMessageProps =
|
|
96
|
-
ComponentPropsWithRef<'div'>,
|
|
97
|
-
'children'
|
|
98
|
-
> & {
|
|
98
|
+
export type ErrorMessageProps = UseErrorMessageOptions & {
|
|
99
99
|
as?: ElementType
|
|
100
100
|
renderChildren?: (props: ErrorMessageItemProps) => ReactNode
|
|
101
101
|
}
|
package/css-files/Field.types.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AriaAttributes,
|
|
3
|
-
|
|
3
|
+
ChangeEvent,
|
|
4
4
|
ComponentPropsWithRef,
|
|
5
|
-
|
|
5
|
+
FocusEvent,
|
|
6
6
|
HTMLAttributes,
|
|
7
|
-
ReactEventHandler,
|
|
8
7
|
ReactNode,
|
|
9
8
|
Ref,
|
|
9
|
+
RefCallback,
|
|
10
|
+
SyntheticEvent,
|
|
10
11
|
} from 'react'
|
|
11
12
|
|
|
12
13
|
export type ValidityErrorKey = Exclude<keyof ValidityState, 'valid'>
|
|
@@ -46,9 +47,9 @@ export type FieldContextValue = {
|
|
|
46
47
|
descriptionId: string
|
|
47
48
|
errorId: string
|
|
48
49
|
errorMap?: ReadonlyMap<FieldErrorKey, ReactNode>
|
|
49
|
-
|
|
50
|
+
updateValidityState(
|
|
50
51
|
event: FieldErrorPolicyArgs['event'],
|
|
51
|
-
|
|
52
|
+
report: ValidityReport,
|
|
52
53
|
): void
|
|
53
54
|
isLoading: boolean
|
|
54
55
|
isReadOnly: boolean
|
|
@@ -56,6 +57,7 @@ export type FieldContextValue = {
|
|
|
56
57
|
label: ReactNode
|
|
57
58
|
labelId: string
|
|
58
59
|
name: string
|
|
60
|
+
validityReporterRef: RefCallback<ControlElement>
|
|
59
61
|
visibleErrors: readonly FieldErrorKey[]
|
|
60
62
|
}
|
|
61
63
|
|
|
@@ -85,7 +87,12 @@ export type UseFieldRootResult = {
|
|
|
85
87
|
fieldRootProps: FieldRootRenderProps
|
|
86
88
|
}
|
|
87
89
|
|
|
88
|
-
|
|
90
|
+
// Field owns label relationship wiring so consumers cannot override the
|
|
91
|
+
// generated label id or control association.
|
|
92
|
+
export type UseLabelOptions = Omit<
|
|
93
|
+
ComponentPropsWithRef<'label'>,
|
|
94
|
+
'htmlFor' | 'id'
|
|
95
|
+
>
|
|
89
96
|
|
|
90
97
|
export type LabelRootProps = ComponentPropsWithRef<'label'> & {
|
|
91
98
|
'data-loading'?: ''
|
|
@@ -103,12 +110,12 @@ export type ControlElement =
|
|
|
103
110
|
| HTMLSelectElement
|
|
104
111
|
| HTMLTextAreaElement
|
|
105
112
|
|
|
106
|
-
type
|
|
113
|
+
type FieldControlElementProps = Omit<
|
|
107
114
|
HTMLAttributes<ControlElement>,
|
|
108
|
-
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
115
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid' | 'role'
|
|
109
116
|
>
|
|
110
117
|
|
|
111
|
-
export type
|
|
118
|
+
export type FieldControlRenderProps = FieldControlElementProps & {
|
|
112
119
|
'aria-busy'?: AriaAttributes['aria-busy']
|
|
113
120
|
'aria-describedby'?: AriaAttributes['aria-describedby']
|
|
114
121
|
'aria-disabled'?: AriaAttributes['aria-disabled']
|
|
@@ -117,77 +124,81 @@ export type ControlRenderProps = ControlElementProps & {
|
|
|
117
124
|
'aria-readonly'?: AriaAttributes['aria-readonly']
|
|
118
125
|
'aria-required'?: AriaAttributes['aria-required']
|
|
119
126
|
'data-loading'?: ''
|
|
120
|
-
disabled?: boolean
|
|
121
127
|
id: string
|
|
122
|
-
|
|
123
|
-
onBlur?: FocusEventHandler<ControlElement>
|
|
124
|
-
onChange?: ChangeEventHandler<ControlElement>
|
|
125
|
-
onFocus?: FocusEventHandler<ControlElement>
|
|
126
|
-
onInput?: ReactEventHandler<ControlElement>
|
|
127
|
-
onInvalid?: ReactEventHandler<ControlElement>
|
|
128
|
-
readOnly?: boolean
|
|
129
|
-
ref?: Ref<any>
|
|
130
|
-
required?: boolean
|
|
131
|
-
role?: ControlRole
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export type ControlRenderArgs = {
|
|
135
|
-
controlProps: ControlRenderProps
|
|
136
|
-
withValidity: WithValidity
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export type ControlRole =
|
|
140
|
-
| 'checkbox'
|
|
141
|
-
| 'combobox'
|
|
142
|
-
| 'listbox'
|
|
143
|
-
| 'searchbox'
|
|
144
|
-
| 'spinbutton'
|
|
145
|
-
| 'textbox'
|
|
146
|
-
|
|
147
|
-
export type UseControlOptions = Omit<
|
|
148
|
-
ControlRenderProps,
|
|
149
|
-
| 'aria-describedby'
|
|
150
|
-
| 'aria-busy'
|
|
151
|
-
| 'aria-disabled'
|
|
152
|
-
| 'aria-errormessage'
|
|
153
|
-
| 'aria-invalid'
|
|
154
|
-
| 'aria-readonly'
|
|
155
|
-
| 'aria-required'
|
|
156
|
-
| 'children'
|
|
157
|
-
| 'data-loading'
|
|
158
|
-
| 'disabled'
|
|
159
|
-
| 'id'
|
|
160
|
-
| 'name'
|
|
161
|
-
| 'readOnly'
|
|
162
|
-
| 'required'
|
|
163
|
-
| 'role'
|
|
164
|
-
> & {
|
|
165
|
-
role?: ControlRole
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export type UseControlResult = {
|
|
169
|
-
controlProps: ControlRenderProps
|
|
170
|
-
withValidity: WithValidity
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
|
|
174
|
-
event: TEvent,
|
|
175
|
-
validity: Partial<ValidityState>,
|
|
176
|
-
) => TEvent & {
|
|
177
|
-
currentTarget: TEvent['currentTarget'] & {
|
|
178
|
-
validity: ValidityState
|
|
179
|
-
}
|
|
128
|
+
ref?: RefCallback<ControlElement>
|
|
180
129
|
}
|
|
181
130
|
|
|
182
|
-
|
|
131
|
+
// A custom root passes the control's validity as the second argument. Native
|
|
132
|
+
// controls leave it out, so the field reads the element's own ValidityState.
|
|
133
|
+
// Method signatures keep the handlers assignable to React's event props.
|
|
134
|
+
export type ValidityHandlerProps<TElement extends Element = ControlElement> = {
|
|
135
|
+
onBlur(event: FocusEvent<TElement>, validity?: Partial<ValidityState>): void
|
|
136
|
+
onChange(
|
|
137
|
+
event: ChangeEvent<TElement>,
|
|
138
|
+
validity?: Partial<ValidityState>,
|
|
139
|
+
): void
|
|
140
|
+
onFocus(event: FocusEvent<TElement>, validity?: Partial<ValidityState>): void
|
|
141
|
+
onInput(
|
|
142
|
+
event: SyntheticEvent<TElement>,
|
|
143
|
+
validity?: Partial<ValidityState>,
|
|
144
|
+
): void
|
|
145
|
+
onInvalid(
|
|
146
|
+
event: SyntheticEvent<TElement>,
|
|
147
|
+
validity?: Partial<ValidityState>,
|
|
148
|
+
): void
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Spread on each reporting input. The ref lets the field drop a removed
|
|
152
|
+
// input's report; merge it with a consumer ref through mergeRefs.
|
|
153
|
+
export type ValidityReporterProps<TElement extends Element = ControlElement> =
|
|
154
|
+
ValidityHandlerProps<TElement> & {
|
|
155
|
+
ref: RefCallback<TElement>
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export type FieldControlRenderArgs = {
|
|
159
|
+
fieldControlProps: FieldControlRenderProps
|
|
160
|
+
validityHandlerProps: ValidityReporterProps
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export type UseFieldControlOptions<TElement extends Element = ControlElement> =
|
|
164
|
+
Omit<
|
|
165
|
+
FieldControlRenderProps,
|
|
166
|
+
| 'aria-describedby'
|
|
167
|
+
| 'aria-busy'
|
|
168
|
+
| 'aria-disabled'
|
|
169
|
+
| 'aria-errormessage'
|
|
170
|
+
| 'aria-invalid'
|
|
171
|
+
| 'aria-readonly'
|
|
172
|
+
| 'aria-required'
|
|
173
|
+
| 'children'
|
|
174
|
+
| 'data-loading'
|
|
175
|
+
| 'id'
|
|
176
|
+
| 'ref'
|
|
177
|
+
> &
|
|
178
|
+
Partial<ValidityHandlerProps<TElement>> & {
|
|
179
|
+
ref?: Ref<TElement>
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export type UseFieldControlResult = FieldControlRenderArgs
|
|
183
|
+
|
|
184
|
+
export type ValidityReport = {
|
|
185
|
+
reporter: EventTarget | symbol
|
|
186
|
+
validity: ValidityState | null
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Field owns the description id because FieldControl
|
|
190
|
+
// uses it for aria-describedby.
|
|
191
|
+
export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
|
|
183
192
|
|
|
184
193
|
export type UseDescriptionResult = {
|
|
185
194
|
descriptionProps: ComponentPropsWithRef<'div'>
|
|
186
195
|
}
|
|
187
196
|
|
|
197
|
+
// Field owns the error id because FieldControl uses it
|
|
198
|
+
// for aria-errormessage when errors are visible.
|
|
188
199
|
export type UseErrorMessageOptions = Omit<
|
|
189
200
|
ComponentPropsWithRef<'div'>,
|
|
190
|
-
'children'
|
|
201
|
+
'children' | 'id'
|
|
191
202
|
>
|
|
192
203
|
|
|
193
204
|
export type ErrorMessageItemProps = {
|
|
@@ -6,9 +6,14 @@ import type {
|
|
|
6
6
|
FieldErrorPolicyFieldState,
|
|
7
7
|
FieldErrorPolicyFormState,
|
|
8
8
|
FieldProviderProps,
|
|
9
|
-
ValidityErrorKey,
|
|
10
9
|
} from './Field.types'
|
|
11
|
-
import {
|
|
10
|
+
import type { ValidityReports } from './fieldValidity'
|
|
11
|
+
import { createContext, useCallback, useId, useRef, useState } from 'react'
|
|
12
|
+
import {
|
|
13
|
+
getReportedErrorKeys,
|
|
14
|
+
pruneDisconnectedReporters,
|
|
15
|
+
recordValidityReport,
|
|
16
|
+
} from './fieldValidity'
|
|
12
17
|
|
|
13
18
|
type FieldNativeState = {
|
|
14
19
|
errors: readonly FieldErrorKey[]
|
|
@@ -59,6 +64,21 @@ function useFieldProviderValue({
|
|
|
59
64
|
},
|
|
60
65
|
isErrorVisible: false,
|
|
61
66
|
})
|
|
67
|
+
const validityReports = useRef<ValidityReports>(new Map())
|
|
68
|
+
// A reporting input can unmount without a validity event. Its ref cleanup
|
|
69
|
+
// runs before React removes the node, so the prune waits for a microtask.
|
|
70
|
+
const validityReporterRef = useCallback(
|
|
71
|
+
() => () => {
|
|
72
|
+
queueMicrotask(() => {
|
|
73
|
+
if (!pruneDisconnectedReporters(validityReports.current)) return
|
|
74
|
+
|
|
75
|
+
const errors = getReportedErrorKeys(validityReports.current)
|
|
76
|
+
setNativeState((state) => ({ ...state, errors }))
|
|
77
|
+
})
|
|
78
|
+
},
|
|
79
|
+
[],
|
|
80
|
+
)
|
|
81
|
+
|
|
62
82
|
const generatedId = useId()
|
|
63
83
|
const resolvedControlId = controlId ?? `${generatedId}-control`
|
|
64
84
|
const currentErrors = activeErrors ?? new Set(nativeState.errors)
|
|
@@ -68,21 +88,7 @@ function useFieldProviderValue({
|
|
|
68
88
|
form: defaultFormState,
|
|
69
89
|
nativeState,
|
|
70
90
|
})
|
|
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
|
-
}
|
|
91
|
+
const isErrorVisible = visibleErrors.length > 0
|
|
86
92
|
|
|
87
93
|
return {
|
|
88
94
|
activeErrors,
|
|
@@ -91,29 +97,25 @@ function useFieldProviderValue({
|
|
|
91
97
|
descriptionId: `${generatedId}-description`,
|
|
92
98
|
errorId: `${generatedId}-error`,
|
|
93
99
|
errorMap,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
event === 'invalid',
|
|
114
|
-
},
|
|
115
|
-
isErrorVisible: currentNativeState.isErrorVisible,
|
|
116
|
-
}))
|
|
100
|
+
updateValidityState: (event, report) => {
|
|
101
|
+
recordValidityReport(validityReports.current, report)
|
|
102
|
+
const nextErrors = getReportedErrorKeys(validityReports.current)
|
|
103
|
+
const nextActiveErrors = activeErrors ?? new Set(nextErrors)
|
|
104
|
+
|
|
105
|
+
setNativeState((currentNativeState) => {
|
|
106
|
+
const nextNativeState = getNextNativeState({
|
|
107
|
+
currentNativeState,
|
|
108
|
+
errorPolicy,
|
|
109
|
+
event,
|
|
110
|
+
isErrorVisible,
|
|
111
|
+
nextActiveErrors,
|
|
112
|
+
nextErrors,
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
return isSameNativeState(currentNativeState, nextNativeState)
|
|
116
|
+
? currentNativeState
|
|
117
|
+
: nextNativeState
|
|
118
|
+
})
|
|
117
119
|
},
|
|
118
120
|
isLoading,
|
|
119
121
|
isReadOnly,
|
|
@@ -121,27 +123,79 @@ function useFieldProviderValue({
|
|
|
121
123
|
label,
|
|
122
124
|
labelId: `${generatedId}-label`,
|
|
123
125
|
name,
|
|
126
|
+
validityReporterRef,
|
|
124
127
|
visibleErrors,
|
|
125
128
|
}
|
|
126
129
|
}
|
|
127
130
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
'
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
131
|
+
function getNextNativeState({
|
|
132
|
+
currentNativeState,
|
|
133
|
+
errorPolicy,
|
|
134
|
+
event,
|
|
135
|
+
isErrorVisible,
|
|
136
|
+
nextActiveErrors,
|
|
137
|
+
nextErrors,
|
|
138
|
+
}: {
|
|
139
|
+
currentNativeState: FieldNativeState
|
|
140
|
+
errorPolicy: FieldErrorPolicy
|
|
141
|
+
event: FieldErrorPolicyArgs['event']
|
|
142
|
+
isErrorVisible: boolean
|
|
143
|
+
nextActiveErrors: ReadonlySet<FieldErrorKey>
|
|
144
|
+
nextErrors: readonly FieldErrorKey[]
|
|
145
|
+
}): FieldNativeState {
|
|
146
|
+
const eventNativeState: FieldNativeState = {
|
|
147
|
+
errors: nextErrors,
|
|
148
|
+
event,
|
|
149
|
+
field: {
|
|
150
|
+
wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
|
|
151
|
+
wasChanged:
|
|
152
|
+
currentNativeState.field.wasChanged ||
|
|
153
|
+
event === 'change' ||
|
|
154
|
+
event === 'input',
|
|
155
|
+
wasErrored: currentNativeState.field.wasErrored || isErrorVisible,
|
|
156
|
+
wasTouched:
|
|
157
|
+
currentNativeState.field.wasTouched ||
|
|
158
|
+
event === 'focus' ||
|
|
159
|
+
event === 'blur' ||
|
|
160
|
+
event === 'change' ||
|
|
161
|
+
event === 'input' ||
|
|
162
|
+
event === 'invalid',
|
|
163
|
+
},
|
|
164
|
+
isErrorVisible,
|
|
165
|
+
}
|
|
166
|
+
const nextVisibleErrors = resolveVisibleErrors({
|
|
167
|
+
activeErrors: nextActiveErrors,
|
|
168
|
+
errorPolicy,
|
|
169
|
+
form: defaultFormState,
|
|
170
|
+
nativeState: eventNativeState,
|
|
171
|
+
})
|
|
172
|
+
const nextIsErrorVisible = nextVisibleErrors.length > 0
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
...eventNativeState,
|
|
176
|
+
field: {
|
|
177
|
+
...eventNativeState.field,
|
|
178
|
+
wasErrored: eventNativeState.field.wasErrored || nextIsErrorVisible,
|
|
179
|
+
},
|
|
180
|
+
isErrorVisible: nextIsErrorVisible,
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isSameNativeState(
|
|
185
|
+
current: FieldNativeState,
|
|
186
|
+
next: FieldNativeState,
|
|
187
|
+
): boolean {
|
|
188
|
+
const fieldKeys = Object.keys(
|
|
189
|
+
current.field,
|
|
190
|
+
) as (keyof FieldErrorPolicyFieldState)[]
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
current.event === next.event &&
|
|
194
|
+
current.isErrorVisible === next.isErrorVisible &&
|
|
195
|
+
current.errors.length === next.errors.length &&
|
|
196
|
+
current.errors.every((key, index) => key === next.errors[index]) &&
|
|
197
|
+
fieldKeys.every((key) => current.field[key] === next.field[key])
|
|
198
|
+
)
|
|
145
199
|
}
|
|
146
200
|
|
|
147
201
|
function resolveVisibleErrors({
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ValidityErrorKey, ValidityReport } from './Field.types'
|
|
2
|
+
|
|
3
|
+
// Native ValidityState objects are live, so a radio checked after its
|
|
4
|
+
// sibling reported still clears that sibling's valueMissing.
|
|
5
|
+
export type ValidityReports = Map<ValidityReport['reporter'], ValidityState>
|
|
6
|
+
|
|
7
|
+
// Reported validity describes the whole control, so every report shares one key.
|
|
8
|
+
export const CONTROL_REPORTER = Symbol('control')
|
|
9
|
+
|
|
10
|
+
export const validityErrorKeys = [
|
|
11
|
+
'badInput',
|
|
12
|
+
'customError',
|
|
13
|
+
'patternMismatch',
|
|
14
|
+
'rangeOverflow',
|
|
15
|
+
'rangeUnderflow',
|
|
16
|
+
'stepMismatch',
|
|
17
|
+
'tooLong',
|
|
18
|
+
'tooShort',
|
|
19
|
+
'typeMismatch',
|
|
20
|
+
'valueMissing',
|
|
21
|
+
] as const satisfies readonly ValidityErrorKey[]
|
|
22
|
+
|
|
23
|
+
export function recordValidityReport(
|
|
24
|
+
reports: ValidityReports,
|
|
25
|
+
report: ValidityReport,
|
|
26
|
+
): void {
|
|
27
|
+
if (report.validity) reports.set(report.reporter, report.validity)
|
|
28
|
+
pruneDisconnectedReporters(reports)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function pruneDisconnectedReporters(reports: ValidityReports): boolean {
|
|
32
|
+
let removed = false
|
|
33
|
+
for (const reporter of reports.keys()) {
|
|
34
|
+
if (isDisconnected(reporter)) {
|
|
35
|
+
reports.delete(reporter)
|
|
36
|
+
removed = true
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return removed
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isDisconnected(reporter: ValidityReport['reporter']): boolean {
|
|
43
|
+
if (typeof reporter === 'symbol') return false
|
|
44
|
+
|
|
45
|
+
return 'isConnected' in reporter && reporter.isConnected === false
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getReportedErrorKeys(
|
|
49
|
+
reports: ValidityReports,
|
|
50
|
+
): readonly ValidityErrorKey[] {
|
|
51
|
+
const reportedKeys = new Set(
|
|
52
|
+
[...reports.values()].flatMap((validity) => getValidityErrorKeys(validity)),
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return validityErrorKeys.filter((key) => reportedKeys.has(key))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getValidityErrorKeys(
|
|
59
|
+
validity: ValidityState,
|
|
60
|
+
): readonly ValidityErrorKey[] {
|
|
61
|
+
return validity.valid ? [] : validityErrorKeys.filter((key) => validity[key])
|
|
62
|
+
}
|
package/css-files/index.ts
CHANGED
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
export type {
|
|
2
|
-
ControlProps,
|
|
3
2
|
DescriptionProps,
|
|
4
3
|
ErrorMessageProps,
|
|
4
|
+
FieldControlProps,
|
|
5
5
|
FieldRootProps,
|
|
6
6
|
LabelProps,
|
|
7
7
|
} from './Field'
|
|
8
8
|
export type {
|
|
9
|
-
ControlRenderArgs,
|
|
10
|
-
ControlRenderProps,
|
|
11
|
-
ControlRole,
|
|
12
9
|
FieldContextValue,
|
|
10
|
+
FieldControlRenderArgs,
|
|
11
|
+
FieldControlRenderProps,
|
|
13
12
|
FieldErrorKey,
|
|
14
13
|
FieldErrorPolicy,
|
|
15
14
|
FieldProviderProps,
|
|
16
15
|
UseDescriptionOptions,
|
|
17
16
|
UseDescriptionResult,
|
|
18
|
-
UseControlOptions,
|
|
19
|
-
UseControlResult,
|
|
20
17
|
UseErrorMessageOptions,
|
|
21
18
|
UseErrorMessageResult,
|
|
19
|
+
UseFieldControlOptions,
|
|
20
|
+
UseFieldControlResult,
|
|
22
21
|
UseFieldRootOptions,
|
|
23
22
|
UseFieldRootResult,
|
|
24
23
|
UseLabelOptions,
|
|
25
24
|
UseLabelResult,
|
|
26
25
|
ValidityErrorKey,
|
|
27
|
-
|
|
26
|
+
ValidityHandlerProps,
|
|
27
|
+
ValidityReport,
|
|
28
|
+
ValidityReporterProps,
|
|
28
29
|
} from './Field.types'
|
|
29
|
-
export {
|
|
30
|
+
export {
|
|
31
|
+
Description,
|
|
32
|
+
ErrorMessage,
|
|
33
|
+
FieldControl,
|
|
34
|
+
FieldRoot,
|
|
35
|
+
Label,
|
|
36
|
+
} from './Field'
|
|
30
37
|
export { FieldProvider } from './FieldContext'
|
|
31
|
-
export { useControl } from './useControl'
|
|
32
38
|
export { useDescription } from './useDescription'
|
|
33
39
|
export { useErrorMessage } from './useErrorMessage'
|
|
40
|
+
export { useFieldContext } from './useFieldContext'
|
|
41
|
+
export { useFieldControl } from './useFieldControl'
|
|
34
42
|
export { useFieldRoot } from './useFieldRoot'
|
|
35
43
|
export { useLabel } from './useLabel'
|
|
@@ -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,
|