@maratus-registry/field 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/css-files/Field.css +8 -6
- package/css-files/Field.tsx +7 -7
- package/css-files/Field.types.ts +74 -72
- package/css-files/FieldContext.tsx +49 -25
- package/css-files/fieldValidity.ts +62 -0
- package/css-files/index.ts +17 -9
- package/css-files/useErrorMessage.ts +6 -8
- package/css-files/useFieldControl.ts +80 -0
- package/css-files/validityReport.ts +41 -0
- package/css-modules/Field.module.css +5 -6
- package/css-modules/Field.tsx +7 -7
- package/css-modules/Field.types.ts +74 -72
- package/css-modules/FieldContext.tsx +49 -25
- package/css-modules/fieldValidity.ts +62 -0
- package/css-modules/index.ts +17 -9
- package/css-modules/useErrorMessage.ts +6 -8
- package/css-modules/useFieldControl.ts +80 -0
- package/css-modules/validityReport.ts +41 -0
- package/meta.json +2 -2
- package/package.json +4 -1
- package/tailwind-css/Field.css +8 -6
- package/tailwind-css/Field.tsx +7 -7
- package/tailwind-css/Field.types.ts +74 -72
- package/tailwind-css/FieldContext.tsx +49 -25
- package/tailwind-css/fieldValidity.ts +62 -0
- package/tailwind-css/index.ts +17 -9
- package/tailwind-css/useErrorMessage.ts +6 -8
- package/tailwind-css/useFieldControl.ts +80 -0
- 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
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
--ara-field-control-fg: var(--ara-color-control-fg);
|
|
14
14
|
--ara-field-control-fg--invalid: var(--ara-color-feedback-fg);
|
|
15
15
|
--ara-field-control-fg--loading: var(--ara-color-control-fg--loading);
|
|
16
|
-
--ara-field-control-fg--readonly: var(--ara-color-control-fg--
|
|
16
|
+
--ara-field-control-fg--readonly: var(--ara-color-control-fg--readonly);
|
|
17
17
|
--ara-field-control-padding-block: var(--ara-spacing-x1);
|
|
18
18
|
--ara-field-control-padding-inline: var(--ara-spacing-x1);
|
|
19
19
|
--ara-field-description-fg: var(--ara-color-content-detail);
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
color: var(--ara-field-label-fg--loading);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
.
|
|
51
|
+
:where(.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,20 @@
|
|
|
58
58
|
padding-inline: var(--ara-field-control-padding-inline);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
.
|
|
61
|
+
:where(.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
|
+
:where(.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
|
+
:where(
|
|
72
|
+
.maratus__Field__fieldControl[readonly],
|
|
73
|
+
.maratus__Field__fieldControl[aria-readonly='true']
|
|
74
|
+
) {
|
|
73
75
|
color: var(--ara-field-control-fg--readonly);
|
|
74
76
|
}
|
|
75
77
|
|
package/css-files/Field.tsx
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
ControlRenderArgs,
|
|
3
2
|
ErrorMessageItemProps,
|
|
4
3
|
FieldErrorKey,
|
|
4
|
+
FieldControlRenderArgs,
|
|
5
5
|
FieldErrorPolicy,
|
|
6
|
-
UseControlOptions,
|
|
7
6
|
UseDescriptionOptions,
|
|
8
7
|
UseErrorMessageOptions,
|
|
8
|
+
UseFieldControlOptions,
|
|
9
9
|
UseLabelOptions,
|
|
10
10
|
} from './Field.types'
|
|
11
11
|
import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
|
|
12
12
|
import { FieldProvider } from './FieldContext'
|
|
13
|
-
import { useControl } from './useControl'
|
|
14
13
|
import { useDescription } from './useDescription'
|
|
15
14
|
import { useErrorMessage } from './useErrorMessage'
|
|
15
|
+
import { useFieldControl } from './useFieldControl'
|
|
16
16
|
import { useFieldRoot } from './useFieldRoot'
|
|
17
17
|
import { useLabel } from './useLabel'
|
|
18
18
|
|
|
@@ -73,13 +73,13 @@ export function Label(props: LabelProps) {
|
|
|
73
73
|
return <label {...labelProps} />
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
export type
|
|
77
|
-
children: (props:
|
|
76
|
+
export type FieldControlProps = UseFieldControlOptions & {
|
|
77
|
+
children: (props: FieldControlRenderArgs) => ReactNode
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
export function
|
|
80
|
+
export function FieldControl(props: FieldControlProps) {
|
|
81
81
|
const { children, ...hookProps } = props
|
|
82
|
-
const control =
|
|
82
|
+
const control = useFieldControl(hookProps)
|
|
83
83
|
|
|
84
84
|
return children(control)
|
|
85
85
|
}
|
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'>
|
|
@@ -48,7 +49,7 @@ export type FieldContextValue = {
|
|
|
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,8 +87,8 @@ export type UseFieldRootResult = {
|
|
|
85
87
|
fieldRootProps: FieldRootRenderProps
|
|
86
88
|
}
|
|
87
89
|
|
|
88
|
-
//
|
|
89
|
-
//
|
|
90
|
+
// Field owns label relationship wiring so consumers cannot override the
|
|
91
|
+
// generated label id or control association.
|
|
90
92
|
export type UseLabelOptions = Omit<
|
|
91
93
|
ComponentPropsWithRef<'label'>,
|
|
92
94
|
'htmlFor' | 'id'
|
|
@@ -108,12 +110,12 @@ export type ControlElement =
|
|
|
108
110
|
| HTMLSelectElement
|
|
109
111
|
| HTMLTextAreaElement
|
|
110
112
|
|
|
111
|
-
type
|
|
113
|
+
type FieldControlElementProps = Omit<
|
|
112
114
|
HTMLAttributes<ControlElement>,
|
|
113
|
-
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
115
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid' | 'role'
|
|
114
116
|
>
|
|
115
117
|
|
|
116
|
-
export type
|
|
118
|
+
export type FieldControlRenderProps = FieldControlElementProps & {
|
|
117
119
|
'aria-busy'?: AriaAttributes['aria-busy']
|
|
118
120
|
'aria-describedby'?: AriaAttributes['aria-describedby']
|
|
119
121
|
'aria-disabled'?: AriaAttributes['aria-disabled']
|
|
@@ -122,78 +124,78 @@ export type ControlRenderProps = ControlElementProps & {
|
|
|
122
124
|
'aria-readonly'?: AriaAttributes['aria-readonly']
|
|
123
125
|
'aria-required'?: AriaAttributes['aria-required']
|
|
124
126
|
'data-loading'?: ''
|
|
125
|
-
disabled?: boolean
|
|
126
127
|
id: string
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
| 'searchbox'
|
|
149
|
-
| 'spinbutton'
|
|
150
|
-
| 'textbox'
|
|
151
|
-
|
|
152
|
-
export type UseControlOptions = Omit<
|
|
153
|
-
ControlRenderProps,
|
|
154
|
-
| 'aria-describedby'
|
|
155
|
-
| 'aria-busy'
|
|
156
|
-
| 'aria-disabled'
|
|
157
|
-
| 'aria-errormessage'
|
|
158
|
-
| 'aria-invalid'
|
|
159
|
-
| 'aria-readonly'
|
|
160
|
-
| 'aria-required'
|
|
161
|
-
| 'children'
|
|
162
|
-
| 'data-loading'
|
|
163
|
-
| 'disabled'
|
|
164
|
-
| 'id'
|
|
165
|
-
| 'name'
|
|
166
|
-
| 'readOnly'
|
|
167
|
-
| 'required'
|
|
168
|
-
| 'role'
|
|
169
|
-
> & {
|
|
170
|
-
role?: ControlRole
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export type UseControlResult = {
|
|
174
|
-
controlProps: ControlRenderProps
|
|
175
|
-
withValidity: WithValidity
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
|
|
179
|
-
event: TEvent,
|
|
180
|
-
validity: Partial<ValidityState>,
|
|
181
|
-
) => TEvent & {
|
|
182
|
-
currentTarget: TEvent['currentTarget'] & {
|
|
183
|
-
validity: ValidityState
|
|
184
|
-
}
|
|
128
|
+
ref?: RefCallback<ControlElement>
|
|
129
|
+
}
|
|
130
|
+
|
|
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
|
|
185
149
|
}
|
|
186
150
|
|
|
187
|
-
//
|
|
188
|
-
//
|
|
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.
|
|
189
191
|
export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
|
|
190
192
|
|
|
191
193
|
export type UseDescriptionResult = {
|
|
192
194
|
descriptionProps: ComponentPropsWithRef<'div'>
|
|
193
195
|
}
|
|
194
196
|
|
|
195
|
-
//
|
|
196
|
-
// aria-errormessage when errors are visible.
|
|
197
|
+
// Field owns the error id because FieldControl uses it
|
|
198
|
+
// for aria-errormessage when errors are visible.
|
|
197
199
|
export type UseErrorMessageOptions = Omit<
|
|
198
200
|
ComponentPropsWithRef<'div'>,
|
|
199
201
|
'children' | 'id'
|
|
@@ -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)
|
|
@@ -77,20 +97,25 @@ function useFieldProviderValue({
|
|
|
77
97
|
descriptionId: `${generatedId}-description`,
|
|
78
98
|
errorId: `${generatedId}-error`,
|
|
79
99
|
errorMap,
|
|
80
|
-
updateValidityState: (event,
|
|
81
|
-
|
|
100
|
+
updateValidityState: (event, report) => {
|
|
101
|
+
recordValidityReport(validityReports.current, report)
|
|
102
|
+
const nextErrors = getReportedErrorKeys(validityReports.current)
|
|
82
103
|
const nextActiveErrors = activeErrors ?? new Set(nextErrors)
|
|
83
104
|
|
|
84
|
-
setNativeState((currentNativeState) =>
|
|
85
|
-
getNextNativeState({
|
|
105
|
+
setNativeState((currentNativeState) => {
|
|
106
|
+
const nextNativeState = getNextNativeState({
|
|
86
107
|
currentNativeState,
|
|
87
108
|
errorPolicy,
|
|
88
109
|
event,
|
|
89
110
|
isErrorVisible,
|
|
90
111
|
nextActiveErrors,
|
|
91
112
|
nextErrors,
|
|
92
|
-
})
|
|
93
|
-
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
return isSameNativeState(currentNativeState, nextNativeState)
|
|
116
|
+
? currentNativeState
|
|
117
|
+
: nextNativeState
|
|
118
|
+
})
|
|
94
119
|
},
|
|
95
120
|
isLoading,
|
|
96
121
|
isReadOnly,
|
|
@@ -98,6 +123,7 @@ function useFieldProviderValue({
|
|
|
98
123
|
label,
|
|
99
124
|
labelId: `${generatedId}-label`,
|
|
100
125
|
name,
|
|
126
|
+
validityReporterRef,
|
|
101
127
|
visibleErrors,
|
|
102
128
|
}
|
|
103
129
|
}
|
|
@@ -155,23 +181,21 @@ function getNextNativeState({
|
|
|
155
181
|
}
|
|
156
182
|
}
|
|
157
183
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
): readonly ValidityErrorKey[] {
|
|
174
|
-
return validity.valid ? [] : validityErrorKeys.filter((key) => validity[key])
|
|
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
|
+
)
|
|
175
199
|
}
|
|
176
200
|
|
|
177
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'
|
|
@@ -19,14 +19,12 @@ export function useErrorMessage(
|
|
|
19
19
|
.filter(
|
|
20
20
|
(entry): entry is readonly [FieldErrorKey, ReactNode] => entry[1] != null,
|
|
21
21
|
)
|
|
22
|
-
.map(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}),
|
|
29
|
-
)
|
|
22
|
+
.map(([errorKey, message]): ErrorMessageItemProps => ({
|
|
23
|
+
children: message,
|
|
24
|
+
className: 'maratus__Field__errorMessage',
|
|
25
|
+
errorKey,
|
|
26
|
+
key: errorKey,
|
|
27
|
+
}))
|
|
30
28
|
|
|
31
29
|
return {
|
|
32
30
|
errorMessageProps: {
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FieldContextValue,
|
|
3
|
+
UseFieldControlOptions,
|
|
4
|
+
UseFieldControlResult,
|
|
5
|
+
ValidityHandlerProps,
|
|
6
|
+
} from './Field.types'
|
|
7
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
8
|
+
import clsx from 'clsx'
|
|
9
|
+
import { useFieldContext } from './useFieldContext'
|
|
10
|
+
import { getValidityReport } from './validityReport'
|
|
11
|
+
import './Field.css'
|
|
12
|
+
|
|
13
|
+
export function useFieldControl(
|
|
14
|
+
options: UseFieldControlOptions = {},
|
|
15
|
+
): UseFieldControlResult {
|
|
16
|
+
const {
|
|
17
|
+
className,
|
|
18
|
+
onBlur,
|
|
19
|
+
onChange,
|
|
20
|
+
onFocus,
|
|
21
|
+
onInput,
|
|
22
|
+
onInvalid,
|
|
23
|
+
ref,
|
|
24
|
+
...fieldControlRootProps
|
|
25
|
+
} = options
|
|
26
|
+
const field = useFieldContext('FieldControl')
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
fieldControlProps: {
|
|
30
|
+
...fieldControlRootProps,
|
|
31
|
+
'aria-busy': field.isLoading ? true : undefined,
|
|
32
|
+
'aria-describedby': field.description ? field.descriptionId : undefined,
|
|
33
|
+
'aria-disabled': field.isLoading ? true : undefined,
|
|
34
|
+
'aria-errormessage':
|
|
35
|
+
field.visibleErrors.length > 0 ? field.errorId : undefined,
|
|
36
|
+
'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
|
|
37
|
+
className: clsx('maratus__Field__fieldControl', className),
|
|
38
|
+
'data-loading': field.isLoading ? '' : undefined,
|
|
39
|
+
id: field.controlId,
|
|
40
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
41
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
42
|
+
...(field.isRequired ? { 'aria-required': true } : {}),
|
|
43
|
+
},
|
|
44
|
+
validityHandlerProps: {
|
|
45
|
+
...composeValidityHandlerProps(
|
|
46
|
+
{ onBlur, onChange, onFocus, onInput, onInvalid },
|
|
47
|
+
field.updateValidityState,
|
|
48
|
+
),
|
|
49
|
+
ref: field.validityReporterRef,
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function composeValidityHandlerProps(
|
|
55
|
+
userHandlers: Partial<ValidityHandlerProps>,
|
|
56
|
+
updateValidityState: FieldContextValue['updateValidityState'],
|
|
57
|
+
): ValidityHandlerProps {
|
|
58
|
+
return {
|
|
59
|
+
onBlur: (event, validity) => {
|
|
60
|
+
userHandlers.onBlur?.(event)
|
|
61
|
+
updateValidityState('blur', getValidityReport(event, validity))
|
|
62
|
+
},
|
|
63
|
+
onChange: (event, validity) => {
|
|
64
|
+
userHandlers.onChange?.(event)
|
|
65
|
+
updateValidityState('change', getValidityReport(event, validity))
|
|
66
|
+
},
|
|
67
|
+
onFocus: (event, validity) => {
|
|
68
|
+
userHandlers.onFocus?.(event)
|
|
69
|
+
updateValidityState('focus', getValidityReport(event, validity))
|
|
70
|
+
},
|
|
71
|
+
onInput: (event, validity) => {
|
|
72
|
+
userHandlers.onInput?.(event)
|
|
73
|
+
updateValidityState('input', getValidityReport(event, validity))
|
|
74
|
+
},
|
|
75
|
+
onInvalid: (event, validity) => {
|
|
76
|
+
userHandlers.onInvalid?.(event)
|
|
77
|
+
updateValidityState('invalid', getValidityReport(event, validity))
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ValidityReport } from './Field.types'
|
|
2
|
+
import { CONTROL_REPORTER, validityErrorKeys } from './fieldValidity'
|
|
3
|
+
|
|
4
|
+
// Reported validity wins; otherwise the event's element reports its own
|
|
5
|
+
// ValidityState, or only field history when it has none.
|
|
6
|
+
export function getValidityReport(
|
|
7
|
+
event: { currentTarget: EventTarget },
|
|
8
|
+
validity: Partial<ValidityState> | undefined,
|
|
9
|
+
): ValidityReport {
|
|
10
|
+
if (validity) {
|
|
11
|
+
return {
|
|
12
|
+
reporter: CONTROL_REPORTER,
|
|
13
|
+
validity: createValidityState(validity),
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
reporter: event.currentTarget,
|
|
19
|
+
validity: hasValidityState(event.currentTarget)
|
|
20
|
+
? event.currentTarget.validity
|
|
21
|
+
: null,
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function hasValidityState(
|
|
26
|
+
target: EventTarget,
|
|
27
|
+
): target is EventTarget & { validity: ValidityState } {
|
|
28
|
+
return 'validity' in target
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function createValidityState(validity: Partial<ValidityState>): ValidityState {
|
|
32
|
+
const errors = Object.fromEntries(
|
|
33
|
+
validityErrorKeys.map((key) => [key, validity[key] ?? false]),
|
|
34
|
+
) as Record<(typeof validityErrorKeys)[number], boolean>
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
...errors,
|
|
38
|
+
valid:
|
|
39
|
+
validity.valid ?? validityErrorKeys.every((key) => errors[key] === false),
|
|
40
|
+
}
|
|
41
|
+
}
|