@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.
Files changed (37) hide show
  1. package/css-files/Field.css +5 -5
  2. package/css-files/Field.tsx +13 -13
  3. package/css-files/Field.types.ts +81 -70
  4. package/css-files/FieldContext.tsx +111 -57
  5. package/css-files/fieldValidity.ts +62 -0
  6. package/css-files/index.ts +17 -9
  7. package/css-files/useDescription.ts +2 -2
  8. package/css-files/useErrorMessage.ts +2 -2
  9. package/css-files/useFieldControl.ts +80 -0
  10. package/css-files/useLabel.ts +3 -3
  11. package/css-files/validityReport.ts +41 -0
  12. package/css-modules/Field.module.css +5 -5
  13. package/css-modules/Field.tsx +13 -13
  14. package/css-modules/Field.types.ts +81 -70
  15. package/css-modules/FieldContext.tsx +111 -57
  16. package/css-modules/fieldValidity.ts +62 -0
  17. package/css-modules/index.ts +17 -9
  18. package/css-modules/useDescription.ts +2 -2
  19. package/css-modules/useErrorMessage.ts +2 -2
  20. package/css-modules/useFieldControl.ts +80 -0
  21. package/css-modules/useLabel.ts +3 -3
  22. package/css-modules/validityReport.ts +41 -0
  23. package/package.json +4 -1
  24. package/tailwind-css/Field.css +5 -5
  25. package/tailwind-css/Field.tsx +13 -13
  26. package/tailwind-css/Field.types.ts +81 -70
  27. package/tailwind-css/FieldContext.tsx +111 -57
  28. package/tailwind-css/fieldValidity.ts +62 -0
  29. package/tailwind-css/index.ts +17 -9
  30. package/tailwind-css/useDescription.ts +2 -2
  31. package/tailwind-css/useErrorMessage.ts +2 -2
  32. package/tailwind-css/useFieldControl.ts +80 -0
  33. package/tailwind-css/useLabel.ts +3 -3
  34. package/tailwind-css/validityReport.ts +41 -0
  35. package/css-files/useControl.ts +0 -161
  36. package/css-modules/useControl.ts +0 -161
  37. package/tailwind-css/useControl.ts +0 -161
@@ -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
+ }
@@ -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, htmlFor, id, ...labelRootProps } = options
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: htmlFor ?? field.controlId,
19
- id: id ?? field.labelId,
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
+ }
@@ -45,7 +45,7 @@
45
45
  color: var(--ara-field-label-fg--loading);
46
46
  }
47
47
 
48
- .control {
48
+ .fieldControl {
49
49
  background-color: var(--ara-field-control-bg);
50
50
  border-color: var(--ara-field-control-detail);
51
51
  border-style: var(--ara-field-control-border-style);
@@ -55,18 +55,18 @@
55
55
  padding-inline: var(--ara-field-control-padding-inline);
56
56
  }
57
57
 
58
- .control[aria-invalid='true'] {
58
+ .fieldControl[aria-invalid='true'] {
59
59
  border-color: var(--ara-field-control-detail--invalid);
60
60
  color: var(--ara-field-control-fg--invalid);
61
61
  }
62
62
 
63
- .control[data-loading] {
63
+ .fieldControl[data-loading] {
64
64
  border-color: var(--ara-field-control-detail--loading);
65
65
  color: var(--ara-field-control-fg--loading);
66
66
  }
67
67
 
68
- .control:read-only,
69
- .control[aria-readonly='true'] {
68
+ .fieldControl[readonly],
69
+ .fieldControl[aria-readonly='true'] {
70
70
  color: var(--ara-field-control-fg--readonly);
71
71
  }
72
72
 
@@ -1,15 +1,18 @@
1
1
  import type {
2
- ControlRenderArgs,
3
2
  ErrorMessageItemProps,
4
3
  FieldErrorKey,
4
+ FieldControlRenderArgs,
5
5
  FieldErrorPolicy,
6
- UseControlOptions,
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 = ComponentPropsWithRef<'label'>
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 ControlProps = UseControlOptions & {
74
- children: (props: ControlRenderArgs) => ReactNode
76
+ export type FieldControlProps = UseFieldControlOptions & {
77
+ children: (props: FieldControlRenderArgs) => ReactNode
75
78
  }
76
79
 
77
- export function Control(props: ControlProps) {
80
+ export function FieldControl(props: FieldControlProps) {
78
81
  const { children, ...hookProps } = props
79
- const control = useControl(hookProps)
82
+ const control = useFieldControl(hookProps)
80
83
 
81
84
  return children(control)
82
85
  }
83
86
 
84
- export type DescriptionProps = ComponentPropsWithRef<'div'> & {
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 = Omit<
96
- ComponentPropsWithRef<'div'>,
97
- 'children'
98
- > & {
98
+ export type ErrorMessageProps = UseErrorMessageOptions & {
99
99
  as?: ElementType
100
100
  renderChildren?: (props: ErrorMessageItemProps) => ReactNode
101
101
  }
@@ -1,12 +1,13 @@
1
1
  import type {
2
2
  AriaAttributes,
3
- ChangeEventHandler,
3
+ ChangeEvent,
4
4
  ComponentPropsWithRef,
5
- FocusEventHandler,
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
- evaluateNativeValidity(
50
+ updateValidityState(
50
51
  event: FieldErrorPolicyArgs['event'],
51
- control: { validity: ValidityState },
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
- export type UseLabelOptions = ComponentPropsWithRef<'label'>
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 ControlElementProps = Omit<
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 ControlRenderProps = ControlElementProps & {
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
- name?: string
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
- export type UseDescriptionOptions = ComponentPropsWithRef<'div'>
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 { createContext, useId, useState } from 'react'
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 nextIsErrorVisible = visibleErrors.length > 0
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
- evaluateNativeValidity: (event, control) => {
95
- const nextErrors = getValidityErrorKeys(control.validity)
96
-
97
- setNativeState((currentNativeState) => ({
98
- errors: nextErrors,
99
- event,
100
- field: {
101
- wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
102
- wasChanged:
103
- currentNativeState.field.wasChanged ||
104
- event === 'change' ||
105
- event === 'input',
106
- wasErrored: currentNativeState.field.wasErrored,
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
- }))
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
- const validityErrorKeys = [
129
- 'badInput',
130
- 'customError',
131
- 'patternMismatch',
132
- 'rangeOverflow',
133
- 'rangeUnderflow',
134
- 'stepMismatch',
135
- 'tooLong',
136
- 'tooShort',
137
- 'typeMismatch',
138
- 'valueMissing',
139
- ] as const satisfies readonly ValidityErrorKey[]
140
-
141
- function getValidityErrorKeys(
142
- validity: ValidityState,
143
- ): readonly ValidityErrorKey[] {
144
- return validity.valid ? [] : validityErrorKeys.filter((key) => validity[key])
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({