@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
|
@@ -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/tailwind-css/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,
|
|
@@ -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
|
+
}
|
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
|
}
|
|
@@ -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
|
+
}
|
package/css-files/useControl.ts
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ControlElement,
|
|
3
|
-
ControlRenderProps,
|
|
4
|
-
FieldContextValue,
|
|
5
|
-
UseControlOptions,
|
|
6
|
-
UseControlResult,
|
|
7
|
-
WithValidity,
|
|
8
|
-
} from './Field.types'
|
|
9
|
-
import clsx from 'clsx'
|
|
10
|
-
import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
|
|
11
|
-
import './Field.css'
|
|
12
|
-
|
|
13
|
-
export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
14
|
-
const {
|
|
15
|
-
className,
|
|
16
|
-
onBlur,
|
|
17
|
-
onChange,
|
|
18
|
-
onFocus,
|
|
19
|
-
onInput,
|
|
20
|
-
onInvalid,
|
|
21
|
-
role,
|
|
22
|
-
...controlRootProps
|
|
23
|
-
} = options
|
|
24
|
-
const field = useRequiredFieldContext('Control')
|
|
25
|
-
const relationshipProps = {
|
|
26
|
-
'aria-busy': field.isLoading ? true : undefined,
|
|
27
|
-
'aria-describedby': field.description ? field.descriptionId : undefined,
|
|
28
|
-
'aria-disabled': field.isLoading ? true : undefined,
|
|
29
|
-
'aria-errormessage':
|
|
30
|
-
field.visibleErrors.length > 0 ? field.errorId : undefined,
|
|
31
|
-
'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
|
|
32
|
-
className: clsx('maratus__Field__control', className),
|
|
33
|
-
'data-loading': field.isLoading ? '' : undefined,
|
|
34
|
-
id: field.controlId,
|
|
35
|
-
} satisfies Pick<
|
|
36
|
-
ControlRenderProps,
|
|
37
|
-
| 'aria-busy'
|
|
38
|
-
| 'aria-describedby'
|
|
39
|
-
| 'aria-disabled'
|
|
40
|
-
| 'aria-errormessage'
|
|
41
|
-
| 'aria-invalid'
|
|
42
|
-
| 'className'
|
|
43
|
-
| 'data-loading'
|
|
44
|
-
| 'id'
|
|
45
|
-
>
|
|
46
|
-
const validityHandlerProps = composeValidityHandlerProps(
|
|
47
|
-
{
|
|
48
|
-
onBlur,
|
|
49
|
-
onChange,
|
|
50
|
-
onFocus,
|
|
51
|
-
onInput,
|
|
52
|
-
onInvalid,
|
|
53
|
-
},
|
|
54
|
-
field.evaluateNativeValidity,
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
if (role) {
|
|
58
|
-
return {
|
|
59
|
-
controlProps: {
|
|
60
|
-
...controlRootProps,
|
|
61
|
-
...relationshipProps,
|
|
62
|
-
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
63
|
-
...(field.isRequired ? { 'aria-required': true } : {}),
|
|
64
|
-
...validityHandlerProps,
|
|
65
|
-
role,
|
|
66
|
-
},
|
|
67
|
-
withValidity,
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
controlProps: {
|
|
73
|
-
...controlRootProps,
|
|
74
|
-
...relationshipProps,
|
|
75
|
-
...(field.isLoading ? { disabled: true } : {}),
|
|
76
|
-
name: field.name,
|
|
77
|
-
...validityHandlerProps,
|
|
78
|
-
...(field.isReadOnly ? { readOnly: true } : {}),
|
|
79
|
-
...(field.isRequired ? { required: true } : {}),
|
|
80
|
-
},
|
|
81
|
-
withValidity,
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const withValidity: WithValidity = (event, validity) => {
|
|
86
|
-
const currentTarget = Object.create(
|
|
87
|
-
Object.getPrototypeOf(event.currentTarget),
|
|
88
|
-
) as typeof event.currentTarget & { validity: ValidityState }
|
|
89
|
-
|
|
90
|
-
Object.assign(currentTarget, event.currentTarget, {
|
|
91
|
-
validity: createValidityState(validity),
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
...event,
|
|
96
|
-
currentTarget,
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function composeValidityHandlerProps(
|
|
101
|
-
userHandlers: Pick<
|
|
102
|
-
UseControlOptions,
|
|
103
|
-
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
104
|
-
>,
|
|
105
|
-
evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
|
|
106
|
-
): Pick<
|
|
107
|
-
ControlRenderProps,
|
|
108
|
-
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
109
|
-
> {
|
|
110
|
-
return {
|
|
111
|
-
onBlur: (event) => {
|
|
112
|
-
userHandlers.onBlur?.(event)
|
|
113
|
-
evaluateNativeValidity('blur', getValidityControl(event))
|
|
114
|
-
},
|
|
115
|
-
onChange: (event) => {
|
|
116
|
-
userHandlers.onChange?.(event)
|
|
117
|
-
evaluateNativeValidity('change', getValidityControl(event))
|
|
118
|
-
},
|
|
119
|
-
onFocus: (event) => {
|
|
120
|
-
userHandlers.onFocus?.(event)
|
|
121
|
-
evaluateNativeValidity('focus', getValidityControl(event))
|
|
122
|
-
},
|
|
123
|
-
onInput: (event) => {
|
|
124
|
-
userHandlers.onInput?.(event)
|
|
125
|
-
evaluateNativeValidity('input', getValidityControl(event))
|
|
126
|
-
},
|
|
127
|
-
onInvalid: (event) => {
|
|
128
|
-
userHandlers.onInvalid?.(event)
|
|
129
|
-
evaluateNativeValidity('invalid', getValidityControl(event))
|
|
130
|
-
},
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function getValidityControl(event: { currentTarget: ControlElement }) {
|
|
135
|
-
return event.currentTarget as ControlElement & { validity: ValidityState }
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const validityStateKeys = [
|
|
139
|
-
'badInput',
|
|
140
|
-
'customError',
|
|
141
|
-
'patternMismatch',
|
|
142
|
-
'rangeOverflow',
|
|
143
|
-
'rangeUnderflow',
|
|
144
|
-
'stepMismatch',
|
|
145
|
-
'tooLong',
|
|
146
|
-
'tooShort',
|
|
147
|
-
'typeMismatch',
|
|
148
|
-
'valueMissing',
|
|
149
|
-
] as const satisfies readonly (keyof Omit<ValidityState, 'valid'>)[]
|
|
150
|
-
|
|
151
|
-
function createValidityState(validity: Partial<ValidityState>): ValidityState {
|
|
152
|
-
const errors = Object.fromEntries(
|
|
153
|
-
validityStateKeys.map((key) => [key, validity[key] ?? false]),
|
|
154
|
-
) as Record<(typeof validityStateKeys)[number], boolean>
|
|
155
|
-
|
|
156
|
-
return {
|
|
157
|
-
...errors,
|
|
158
|
-
valid:
|
|
159
|
-
validity.valid ?? validityStateKeys.every((key) => errors[key] === false),
|
|
160
|
-
}
|
|
161
|
-
}
|