@maratus-registry/field 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,9 @@ import type {
4
4
  FieldErrorKey,
5
5
  FieldErrorPolicy,
6
6
  UseControlOptions,
7
+ UseDescriptionOptions,
8
+ UseErrorMessageOptions,
9
+ UseLabelOptions,
7
10
  } from './Field.types'
8
11
  import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
9
12
  import { FieldProvider } from './FieldContext'
@@ -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)
@@ -81,7 +84,7 @@ export function Control(props: ControlProps) {
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
  }
@@ -46,7 +46,7 @@ export type FieldContextValue = {
46
46
  descriptionId: string
47
47
  errorId: string
48
48
  errorMap?: ReadonlyMap<FieldErrorKey, ReactNode>
49
- evaluateNativeValidity(
49
+ updateValidityState(
50
50
  event: FieldErrorPolicyArgs['event'],
51
51
  control: { validity: ValidityState },
52
52
  ): void
@@ -85,7 +85,12 @@ export type UseFieldRootResult = {
85
85
  fieldRootProps: FieldRootRenderProps
86
86
  }
87
87
 
88
- export type UseLabelOptions = ComponentPropsWithRef<'label'>
88
+ // GPRD-006, REQ-002 and REQ-004: Field owns label relationship wiring so
89
+ // consumers cannot override the generated label id or control association.
90
+ export type UseLabelOptions = Omit<
91
+ ComponentPropsWithRef<'label'>,
92
+ 'htmlFor' | 'id'
93
+ >
89
94
 
90
95
  export type LabelRootProps = ComponentPropsWithRef<'label'> & {
91
96
  'data-loading'?: ''
@@ -179,15 +184,19 @@ export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
179
184
  }
180
185
  }
181
186
 
182
- export type UseDescriptionOptions = ComponentPropsWithRef<'div'>
187
+ // GPRD-006 and REQ-005: Field owns the description id because Control uses it
188
+ // for aria-describedby.
189
+ export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
183
190
 
184
191
  export type UseDescriptionResult = {
185
192
  descriptionProps: ComponentPropsWithRef<'div'>
186
193
  }
187
194
 
195
+ // GPRD-006 and REQ-007: Field owns the error id because Control uses it for
196
+ // aria-errormessage when errors are visible.
188
197
  export type UseErrorMessageOptions = Omit<
189
198
  ComponentPropsWithRef<'div'>,
190
- 'children'
199
+ 'children' | 'id'
191
200
  >
192
201
 
193
202
  export type ErrorMessageItemProps = {
@@ -68,21 +68,7 @@ function useFieldProviderValue({
68
68
  form: defaultFormState,
69
69
  nativeState,
70
70
  })
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
- }
71
+ const isErrorVisible = visibleErrors.length > 0
86
72
 
87
73
  return {
88
74
  activeErrors,
@@ -91,29 +77,20 @@ function useFieldProviderValue({
91
77
  descriptionId: `${generatedId}-description`,
92
78
  errorId: `${generatedId}-error`,
93
79
  errorMap,
94
- evaluateNativeValidity: (event, control) => {
80
+ updateValidityState: (event, control) => {
95
81
  const nextErrors = getValidityErrorKeys(control.validity)
82
+ const nextActiveErrors = activeErrors ?? new Set(nextErrors)
96
83
 
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
- }))
84
+ setNativeState((currentNativeState) =>
85
+ getNextNativeState({
86
+ currentNativeState,
87
+ errorPolicy,
88
+ event,
89
+ isErrorVisible,
90
+ nextActiveErrors,
91
+ nextErrors,
92
+ }),
93
+ )
117
94
  },
118
95
  isLoading,
119
96
  isReadOnly,
@@ -125,6 +102,59 @@ function useFieldProviderValue({
125
102
  }
126
103
  }
127
104
 
105
+ function getNextNativeState({
106
+ currentNativeState,
107
+ errorPolicy,
108
+ event,
109
+ isErrorVisible,
110
+ nextActiveErrors,
111
+ nextErrors,
112
+ }: {
113
+ currentNativeState: FieldNativeState
114
+ errorPolicy: FieldErrorPolicy
115
+ event: FieldErrorPolicyArgs['event']
116
+ isErrorVisible: boolean
117
+ nextActiveErrors: ReadonlySet<FieldErrorKey>
118
+ nextErrors: readonly FieldErrorKey[]
119
+ }): FieldNativeState {
120
+ const eventNativeState: FieldNativeState = {
121
+ errors: nextErrors,
122
+ event,
123
+ field: {
124
+ wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
125
+ wasChanged:
126
+ currentNativeState.field.wasChanged ||
127
+ event === 'change' ||
128
+ event === 'input',
129
+ wasErrored: currentNativeState.field.wasErrored || isErrorVisible,
130
+ wasTouched:
131
+ currentNativeState.field.wasTouched ||
132
+ event === 'focus' ||
133
+ event === 'blur' ||
134
+ event === 'change' ||
135
+ event === 'input' ||
136
+ event === 'invalid',
137
+ },
138
+ isErrorVisible,
139
+ }
140
+ const nextVisibleErrors = resolveVisibleErrors({
141
+ activeErrors: nextActiveErrors,
142
+ errorPolicy,
143
+ form: defaultFormState,
144
+ nativeState: eventNativeState,
145
+ })
146
+ const nextIsErrorVisible = nextVisibleErrors.length > 0
147
+
148
+ return {
149
+ ...eventNativeState,
150
+ field: {
151
+ ...eventNativeState.field,
152
+ wasErrored: eventNativeState.field.wasErrored || nextIsErrorVisible,
153
+ },
154
+ isErrorVisible: nextIsErrorVisible,
155
+ }
156
+ }
157
+
128
158
  const validityErrorKeys = [
129
159
  'badInput',
130
160
  'customError',
@@ -51,7 +51,7 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
51
51
  onInput,
52
52
  onInvalid,
53
53
  },
54
- field.evaluateNativeValidity,
54
+ field.updateValidityState,
55
55
  )
56
56
 
57
57
  if (role) {
@@ -102,7 +102,7 @@ function composeValidityHandlerProps(
102
102
  UseControlOptions,
103
103
  'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
104
104
  >,
105
- evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
105
+ updateValidityState: FieldContextValue['updateValidityState'],
106
106
  ): Pick<
107
107
  ControlRenderProps,
108
108
  'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
@@ -110,23 +110,23 @@ function composeValidityHandlerProps(
110
110
  return {
111
111
  onBlur: (event) => {
112
112
  userHandlers.onBlur?.(event)
113
- evaluateNativeValidity('blur', getValidityControl(event))
113
+ updateValidityState('blur', getValidityControl(event))
114
114
  },
115
115
  onChange: (event) => {
116
116
  userHandlers.onChange?.(event)
117
- evaluateNativeValidity('change', getValidityControl(event))
117
+ updateValidityState('change', getValidityControl(event))
118
118
  },
119
119
  onFocus: (event) => {
120
120
  userHandlers.onFocus?.(event)
121
- evaluateNativeValidity('focus', getValidityControl(event))
121
+ updateValidityState('focus', getValidityControl(event))
122
122
  },
123
123
  onInput: (event) => {
124
124
  userHandlers.onInput?.(event)
125
- evaluateNativeValidity('input', getValidityControl(event))
125
+ updateValidityState('input', getValidityControl(event))
126
126
  },
127
127
  onInvalid: (event) => {
128
128
  userHandlers.onInvalid?.(event)
129
- evaluateNativeValidity('invalid', getValidityControl(event))
129
+ updateValidityState('invalid', getValidityControl(event))
130
130
  },
131
131
  }
132
132
  }
@@ -6,7 +6,7 @@ import './Field.css'
6
6
  export function useDescription(
7
7
  options: UseDescriptionOptions,
8
8
  ): UseDescriptionResult {
9
- const { children, className, id, ...descriptionRootProps } = options
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: id ?? field.descriptionId,
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, id, ...errorMessageRootProps } = options
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: id ?? field.errorId,
35
+ id: field.errorId,
36
36
  role: field.visibleErrors.length > 0 ? 'alert' : undefined,
37
37
  },
38
38
  items,
@@ -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
  }
@@ -4,6 +4,9 @@ import type {
4
4
  FieldErrorKey,
5
5
  FieldErrorPolicy,
6
6
  UseControlOptions,
7
+ UseDescriptionOptions,
8
+ UseErrorMessageOptions,
9
+ UseLabelOptions,
7
10
  } from './Field.types'
8
11
  import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
9
12
  import { FieldProvider } from './FieldContext'
@@ -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)
@@ -81,7 +84,7 @@ export function Control(props: ControlProps) {
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
  }
@@ -46,7 +46,7 @@ export type FieldContextValue = {
46
46
  descriptionId: string
47
47
  errorId: string
48
48
  errorMap?: ReadonlyMap<FieldErrorKey, ReactNode>
49
- evaluateNativeValidity(
49
+ updateValidityState(
50
50
  event: FieldErrorPolicyArgs['event'],
51
51
  control: { validity: ValidityState },
52
52
  ): void
@@ -85,7 +85,12 @@ export type UseFieldRootResult = {
85
85
  fieldRootProps: FieldRootRenderProps
86
86
  }
87
87
 
88
- export type UseLabelOptions = ComponentPropsWithRef<'label'>
88
+ // GPRD-006, REQ-002 and REQ-004: Field owns label relationship wiring so
89
+ // consumers cannot override the generated label id or control association.
90
+ export type UseLabelOptions = Omit<
91
+ ComponentPropsWithRef<'label'>,
92
+ 'htmlFor' | 'id'
93
+ >
89
94
 
90
95
  export type LabelRootProps = ComponentPropsWithRef<'label'> & {
91
96
  'data-loading'?: ''
@@ -179,15 +184,19 @@ export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
179
184
  }
180
185
  }
181
186
 
182
- export type UseDescriptionOptions = ComponentPropsWithRef<'div'>
187
+ // GPRD-006 and REQ-005: Field owns the description id because Control uses it
188
+ // for aria-describedby.
189
+ export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
183
190
 
184
191
  export type UseDescriptionResult = {
185
192
  descriptionProps: ComponentPropsWithRef<'div'>
186
193
  }
187
194
 
195
+ // GPRD-006 and REQ-007: Field owns the error id because Control uses it for
196
+ // aria-errormessage when errors are visible.
188
197
  export type UseErrorMessageOptions = Omit<
189
198
  ComponentPropsWithRef<'div'>,
190
- 'children'
199
+ 'children' | 'id'
191
200
  >
192
201
 
193
202
  export type ErrorMessageItemProps = {
@@ -68,21 +68,7 @@ function useFieldProviderValue({
68
68
  form: defaultFormState,
69
69
  nativeState,
70
70
  })
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
- }
71
+ const isErrorVisible = visibleErrors.length > 0
86
72
 
87
73
  return {
88
74
  activeErrors,
@@ -91,29 +77,20 @@ function useFieldProviderValue({
91
77
  descriptionId: `${generatedId}-description`,
92
78
  errorId: `${generatedId}-error`,
93
79
  errorMap,
94
- evaluateNativeValidity: (event, control) => {
80
+ updateValidityState: (event, control) => {
95
81
  const nextErrors = getValidityErrorKeys(control.validity)
82
+ const nextActiveErrors = activeErrors ?? new Set(nextErrors)
96
83
 
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
- }))
84
+ setNativeState((currentNativeState) =>
85
+ getNextNativeState({
86
+ currentNativeState,
87
+ errorPolicy,
88
+ event,
89
+ isErrorVisible,
90
+ nextActiveErrors,
91
+ nextErrors,
92
+ }),
93
+ )
117
94
  },
118
95
  isLoading,
119
96
  isReadOnly,
@@ -125,6 +102,59 @@ function useFieldProviderValue({
125
102
  }
126
103
  }
127
104
 
105
+ function getNextNativeState({
106
+ currentNativeState,
107
+ errorPolicy,
108
+ event,
109
+ isErrorVisible,
110
+ nextActiveErrors,
111
+ nextErrors,
112
+ }: {
113
+ currentNativeState: FieldNativeState
114
+ errorPolicy: FieldErrorPolicy
115
+ event: FieldErrorPolicyArgs['event']
116
+ isErrorVisible: boolean
117
+ nextActiveErrors: ReadonlySet<FieldErrorKey>
118
+ nextErrors: readonly FieldErrorKey[]
119
+ }): FieldNativeState {
120
+ const eventNativeState: FieldNativeState = {
121
+ errors: nextErrors,
122
+ event,
123
+ field: {
124
+ wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
125
+ wasChanged:
126
+ currentNativeState.field.wasChanged ||
127
+ event === 'change' ||
128
+ event === 'input',
129
+ wasErrored: currentNativeState.field.wasErrored || isErrorVisible,
130
+ wasTouched:
131
+ currentNativeState.field.wasTouched ||
132
+ event === 'focus' ||
133
+ event === 'blur' ||
134
+ event === 'change' ||
135
+ event === 'input' ||
136
+ event === 'invalid',
137
+ },
138
+ isErrorVisible,
139
+ }
140
+ const nextVisibleErrors = resolveVisibleErrors({
141
+ activeErrors: nextActiveErrors,
142
+ errorPolicy,
143
+ form: defaultFormState,
144
+ nativeState: eventNativeState,
145
+ })
146
+ const nextIsErrorVisible = nextVisibleErrors.length > 0
147
+
148
+ return {
149
+ ...eventNativeState,
150
+ field: {
151
+ ...eventNativeState.field,
152
+ wasErrored: eventNativeState.field.wasErrored || nextIsErrorVisible,
153
+ },
154
+ isErrorVisible: nextIsErrorVisible,
155
+ }
156
+ }
157
+
128
158
  const validityErrorKeys = [
129
159
  'badInput',
130
160
  'customError',
@@ -51,7 +51,7 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
51
51
  onInput,
52
52
  onInvalid,
53
53
  },
54
- field.evaluateNativeValidity,
54
+ field.updateValidityState,
55
55
  )
56
56
 
57
57
  if (role) {
@@ -102,7 +102,7 @@ function composeValidityHandlerProps(
102
102
  UseControlOptions,
103
103
  'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
104
104
  >,
105
- evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
105
+ updateValidityState: FieldContextValue['updateValidityState'],
106
106
  ): Pick<
107
107
  ControlRenderProps,
108
108
  'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
@@ -110,23 +110,23 @@ function composeValidityHandlerProps(
110
110
  return {
111
111
  onBlur: (event) => {
112
112
  userHandlers.onBlur?.(event)
113
- evaluateNativeValidity('blur', getValidityControl(event))
113
+ updateValidityState('blur', getValidityControl(event))
114
114
  },
115
115
  onChange: (event) => {
116
116
  userHandlers.onChange?.(event)
117
- evaluateNativeValidity('change', getValidityControl(event))
117
+ updateValidityState('change', getValidityControl(event))
118
118
  },
119
119
  onFocus: (event) => {
120
120
  userHandlers.onFocus?.(event)
121
- evaluateNativeValidity('focus', getValidityControl(event))
121
+ updateValidityState('focus', getValidityControl(event))
122
122
  },
123
123
  onInput: (event) => {
124
124
  userHandlers.onInput?.(event)
125
- evaluateNativeValidity('input', getValidityControl(event))
125
+ updateValidityState('input', getValidityControl(event))
126
126
  },
127
127
  onInvalid: (event) => {
128
128
  userHandlers.onInvalid?.(event)
129
- evaluateNativeValidity('invalid', getValidityControl(event))
129
+ updateValidityState('invalid', getValidityControl(event))
130
130
  },
131
131
  }
132
132
  }
@@ -6,7 +6,7 @@ import styles from './Field.module.css'
6
6
  export function useDescription(
7
7
  options: UseDescriptionOptions,
8
8
  ): UseDescriptionResult {
9
- const { children, className, id, ...descriptionRootProps } = options
9
+ const { children, className, ...descriptionRootProps } = options
10
10
  const field = useFieldContext('Description')
11
11
 
12
12
  return {
@@ -14,7 +14,7 @@ export function useDescription(
14
14
  ...descriptionRootProps,
15
15
  children: children ?? field.description,
16
16
  className: clsx(styles.description, className),
17
- id: id ?? field.descriptionId,
17
+ id: field.descriptionId,
18
18
  },
19
19
  }
20
20
  }
@@ -12,7 +12,7 @@ import styles from './Field.module.css'
12
12
  export function useErrorMessage(
13
13
  options: UseErrorMessageOptions,
14
14
  ): UseErrorMessageResult {
15
- const { className, id, ...errorMessageRootProps } = options
15
+ const { className, ...errorMessageRootProps } = options
16
16
  const field = useFieldContext('ErrorMessage')
17
17
  const items = field.visibleErrors
18
18
  .map((errorKey) => [errorKey, field.errorMap?.get(errorKey)] as const)
@@ -32,7 +32,7 @@ export function useErrorMessage(
32
32
  errorMessageProps: {
33
33
  ...errorMessageRootProps,
34
34
  className: clsx(styles.errorMessageRoot, className),
35
- id: id ?? field.errorId,
35
+ id: field.errorId,
36
36
  role: field.visibleErrors.length > 0 ? 'alert' : undefined,
37
37
  },
38
38
  items,
@@ -4,7 +4,7 @@ import { useFieldContext } from './useFieldContext'
4
4
  import styles from './Field.module.css'
5
5
 
6
6
  export function useLabel(options: UseLabelOptions): UseLabelResult {
7
- const { children, className, 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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maratus-registry/field",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "private": false,
5
5
  "files": [
6
6
  "css-files",
@@ -4,6 +4,9 @@ import type {
4
4
  FieldErrorKey,
5
5
  FieldErrorPolicy,
6
6
  UseControlOptions,
7
+ UseDescriptionOptions,
8
+ UseErrorMessageOptions,
9
+ UseLabelOptions,
7
10
  } from './Field.types'
8
11
  import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
9
12
  import { FieldProvider } from './FieldContext'
@@ -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)
@@ -81,7 +84,7 @@ export function Control(props: ControlProps) {
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
  }
@@ -46,7 +46,7 @@ export type FieldContextValue = {
46
46
  descriptionId: string
47
47
  errorId: string
48
48
  errorMap?: ReadonlyMap<FieldErrorKey, ReactNode>
49
- evaluateNativeValidity(
49
+ updateValidityState(
50
50
  event: FieldErrorPolicyArgs['event'],
51
51
  control: { validity: ValidityState },
52
52
  ): void
@@ -85,7 +85,12 @@ export type UseFieldRootResult = {
85
85
  fieldRootProps: FieldRootRenderProps
86
86
  }
87
87
 
88
- export type UseLabelOptions = ComponentPropsWithRef<'label'>
88
+ // GPRD-006, REQ-002 and REQ-004: Field owns label relationship wiring so
89
+ // consumers cannot override the generated label id or control association.
90
+ export type UseLabelOptions = Omit<
91
+ ComponentPropsWithRef<'label'>,
92
+ 'htmlFor' | 'id'
93
+ >
89
94
 
90
95
  export type LabelRootProps = ComponentPropsWithRef<'label'> & {
91
96
  'data-loading'?: ''
@@ -179,15 +184,19 @@ export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
179
184
  }
180
185
  }
181
186
 
182
- export type UseDescriptionOptions = ComponentPropsWithRef<'div'>
187
+ // GPRD-006 and REQ-005: Field owns the description id because Control uses it
188
+ // for aria-describedby.
189
+ export type UseDescriptionOptions = Omit<ComponentPropsWithRef<'div'>, 'id'>
183
190
 
184
191
  export type UseDescriptionResult = {
185
192
  descriptionProps: ComponentPropsWithRef<'div'>
186
193
  }
187
194
 
195
+ // GPRD-006 and REQ-007: Field owns the error id because Control uses it for
196
+ // aria-errormessage when errors are visible.
188
197
  export type UseErrorMessageOptions = Omit<
189
198
  ComponentPropsWithRef<'div'>,
190
- 'children'
199
+ 'children' | 'id'
191
200
  >
192
201
 
193
202
  export type ErrorMessageItemProps = {
@@ -68,21 +68,7 @@ function useFieldProviderValue({
68
68
  form: defaultFormState,
69
69
  nativeState,
70
70
  })
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
- }
71
+ const isErrorVisible = visibleErrors.length > 0
86
72
 
87
73
  return {
88
74
  activeErrors,
@@ -91,29 +77,20 @@ function useFieldProviderValue({
91
77
  descriptionId: `${generatedId}-description`,
92
78
  errorId: `${generatedId}-error`,
93
79
  errorMap,
94
- evaluateNativeValidity: (event, control) => {
80
+ updateValidityState: (event, control) => {
95
81
  const nextErrors = getValidityErrorKeys(control.validity)
82
+ const nextActiveErrors = activeErrors ?? new Set(nextErrors)
96
83
 
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
- }))
84
+ setNativeState((currentNativeState) =>
85
+ getNextNativeState({
86
+ currentNativeState,
87
+ errorPolicy,
88
+ event,
89
+ isErrorVisible,
90
+ nextActiveErrors,
91
+ nextErrors,
92
+ }),
93
+ )
117
94
  },
118
95
  isLoading,
119
96
  isReadOnly,
@@ -125,6 +102,59 @@ function useFieldProviderValue({
125
102
  }
126
103
  }
127
104
 
105
+ function getNextNativeState({
106
+ currentNativeState,
107
+ errorPolicy,
108
+ event,
109
+ isErrorVisible,
110
+ nextActiveErrors,
111
+ nextErrors,
112
+ }: {
113
+ currentNativeState: FieldNativeState
114
+ errorPolicy: FieldErrorPolicy
115
+ event: FieldErrorPolicyArgs['event']
116
+ isErrorVisible: boolean
117
+ nextActiveErrors: ReadonlySet<FieldErrorKey>
118
+ nextErrors: readonly FieldErrorKey[]
119
+ }): FieldNativeState {
120
+ const eventNativeState: FieldNativeState = {
121
+ errors: nextErrors,
122
+ event,
123
+ field: {
124
+ wasBlurred: currentNativeState.field.wasBlurred || event === 'blur',
125
+ wasChanged:
126
+ currentNativeState.field.wasChanged ||
127
+ event === 'change' ||
128
+ event === 'input',
129
+ wasErrored: currentNativeState.field.wasErrored || isErrorVisible,
130
+ wasTouched:
131
+ currentNativeState.field.wasTouched ||
132
+ event === 'focus' ||
133
+ event === 'blur' ||
134
+ event === 'change' ||
135
+ event === 'input' ||
136
+ event === 'invalid',
137
+ },
138
+ isErrorVisible,
139
+ }
140
+ const nextVisibleErrors = resolveVisibleErrors({
141
+ activeErrors: nextActiveErrors,
142
+ errorPolicy,
143
+ form: defaultFormState,
144
+ nativeState: eventNativeState,
145
+ })
146
+ const nextIsErrorVisible = nextVisibleErrors.length > 0
147
+
148
+ return {
149
+ ...eventNativeState,
150
+ field: {
151
+ ...eventNativeState.field,
152
+ wasErrored: eventNativeState.field.wasErrored || nextIsErrorVisible,
153
+ },
154
+ isErrorVisible: nextIsErrorVisible,
155
+ }
156
+ }
157
+
128
158
  const validityErrorKeys = [
129
159
  'badInput',
130
160
  'customError',
@@ -51,7 +51,7 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
51
51
  onInput,
52
52
  onInvalid,
53
53
  },
54
- field.evaluateNativeValidity,
54
+ field.updateValidityState,
55
55
  )
56
56
 
57
57
  if (role) {
@@ -102,7 +102,7 @@ function composeValidityHandlerProps(
102
102
  UseControlOptions,
103
103
  'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
104
104
  >,
105
- evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
105
+ updateValidityState: FieldContextValue['updateValidityState'],
106
106
  ): Pick<
107
107
  ControlRenderProps,
108
108
  'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
@@ -110,23 +110,23 @@ function composeValidityHandlerProps(
110
110
  return {
111
111
  onBlur: (event) => {
112
112
  userHandlers.onBlur?.(event)
113
- evaluateNativeValidity('blur', getValidityControl(event))
113
+ updateValidityState('blur', getValidityControl(event))
114
114
  },
115
115
  onChange: (event) => {
116
116
  userHandlers.onChange?.(event)
117
- evaluateNativeValidity('change', getValidityControl(event))
117
+ updateValidityState('change', getValidityControl(event))
118
118
  },
119
119
  onFocus: (event) => {
120
120
  userHandlers.onFocus?.(event)
121
- evaluateNativeValidity('focus', getValidityControl(event))
121
+ updateValidityState('focus', getValidityControl(event))
122
122
  },
123
123
  onInput: (event) => {
124
124
  userHandlers.onInput?.(event)
125
- evaluateNativeValidity('input', getValidityControl(event))
125
+ updateValidityState('input', getValidityControl(event))
126
126
  },
127
127
  onInvalid: (event) => {
128
128
  userHandlers.onInvalid?.(event)
129
- evaluateNativeValidity('invalid', getValidityControl(event))
129
+ updateValidityState('invalid', getValidityControl(event))
130
130
  },
131
131
  }
132
132
  }
@@ -6,7 +6,7 @@ import './Field.css'
6
6
  export function useDescription(
7
7
  options: UseDescriptionOptions,
8
8
  ): UseDescriptionResult {
9
- const { children, className, id, ...descriptionRootProps } = options
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: id ?? field.descriptionId,
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, id, ...errorMessageRootProps } = options
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: id ?? field.errorId,
35
+ id: field.errorId,
36
36
  role: field.visibleErrors.length > 0 ? 'alert' : undefined,
37
37
  },
38
38
  items,
@@ -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
  }