@maratus-registry/field 0.0.0 → 0.2.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.tsx +10 -17
- package/css-files/Field.types.ts +11 -9
- package/css-files/index.ts +5 -7
- package/css-files/useControl.ts +161 -0
- package/css-files/useDescription.ts +20 -0
- package/css-files/useErrorMessage.ts +40 -0
- package/css-files/useFieldContext.ts +15 -0
- package/css-files/useFieldRoot.ts +21 -0
- package/css-files/useLabel.ts +22 -0
- package/css-modules/Field.tsx +10 -17
- package/css-modules/Field.types.ts +11 -9
- package/css-modules/index.ts +5 -7
- package/css-modules/useControl.ts +161 -0
- package/css-modules/useDescription.ts +20 -0
- package/css-modules/useErrorMessage.ts +40 -0
- package/css-modules/useFieldContext.ts +15 -0
- package/css-modules/useFieldRoot.ts +21 -0
- package/css-modules/useLabel.ts +22 -0
- package/package.json +1 -1
- package/tailwind-css/Field.tsx +10 -17
- package/tailwind-css/Field.types.ts +11 -9
- package/tailwind-css/index.ts +5 -7
- package/tailwind-css/useControl.ts +161 -0
- package/tailwind-css/useDescription.ts +20 -0
- package/tailwind-css/useErrorMessage.ts +40 -0
- package/tailwind-css/useFieldContext.ts +15 -0
- package/tailwind-css/useFieldRoot.ts +21 -0
- package/tailwind-css/useLabel.ts +22 -0
- package/css-files/useField.ts +0 -6
- package/css-modules/useField.ts +0 -6
- package/tailwind-css/useField.ts +0 -6
|
@@ -0,0 +1,161 @@
|
|
|
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 styles from './Field.module.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(styles.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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { UseDescriptionOptions, UseDescriptionResult } from './Field.types'
|
|
2
|
+
import clsx from 'clsx'
|
|
3
|
+
import { useFieldContext } from './useFieldContext'
|
|
4
|
+
import styles from './Field.module.css'
|
|
5
|
+
|
|
6
|
+
export function useDescription(
|
|
7
|
+
options: UseDescriptionOptions,
|
|
8
|
+
): UseDescriptionResult {
|
|
9
|
+
const { children, className, id, ...descriptionRootProps } = options
|
|
10
|
+
const field = useFieldContext('Description')
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
descriptionProps: {
|
|
14
|
+
...descriptionRootProps,
|
|
15
|
+
children: children ?? field.description,
|
|
16
|
+
className: clsx(styles.description, className),
|
|
17
|
+
id: id ?? field.descriptionId,
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ErrorMessageItemProps,
|
|
3
|
+
FieldErrorKey,
|
|
4
|
+
UseErrorMessageOptions,
|
|
5
|
+
UseErrorMessageResult,
|
|
6
|
+
} from './Field.types'
|
|
7
|
+
import type { ReactNode } from 'react'
|
|
8
|
+
import clsx from 'clsx'
|
|
9
|
+
import { useFieldContext } from './useFieldContext'
|
|
10
|
+
import styles from './Field.module.css'
|
|
11
|
+
|
|
12
|
+
export function useErrorMessage(
|
|
13
|
+
options: UseErrorMessageOptions,
|
|
14
|
+
): UseErrorMessageResult {
|
|
15
|
+
const { className, id, ...errorMessageRootProps } = options
|
|
16
|
+
const field = useFieldContext('ErrorMessage')
|
|
17
|
+
const items = field.visibleErrors
|
|
18
|
+
.map((errorKey) => [errorKey, field.errorMap?.get(errorKey)] as const)
|
|
19
|
+
.filter(
|
|
20
|
+
(entry): entry is readonly [FieldErrorKey, ReactNode] => entry[1] != null,
|
|
21
|
+
)
|
|
22
|
+
.map(
|
|
23
|
+
([errorKey, message]): ErrorMessageItemProps => ({
|
|
24
|
+
children: message,
|
|
25
|
+
className: styles.errorMessage,
|
|
26
|
+
errorKey,
|
|
27
|
+
key: errorKey,
|
|
28
|
+
}),
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
errorMessageProps: {
|
|
33
|
+
...errorMessageRootProps,
|
|
34
|
+
className: clsx(styles.errorMessageRoot, className),
|
|
35
|
+
id: id ?? field.errorId,
|
|
36
|
+
role: field.visibleErrors.length > 0 ? 'alert' : undefined,
|
|
37
|
+
},
|
|
38
|
+
items,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FieldContextValue } from './Field.types'
|
|
2
|
+
import { useContext } from 'react'
|
|
3
|
+
import { FieldContext } from './FieldContext'
|
|
4
|
+
|
|
5
|
+
export function useFieldContext(componentName: string): FieldContextValue {
|
|
6
|
+
const context = useContext(FieldContext)
|
|
7
|
+
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new globalThis.Error(
|
|
10
|
+
`${componentName} must be rendered inside FieldRoot.`,
|
|
11
|
+
)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return context
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { UseFieldRootOptions, UseFieldRootResult } from './Field.types'
|
|
2
|
+
import clsx from 'clsx'
|
|
3
|
+
import styles from './Field.module.css'
|
|
4
|
+
|
|
5
|
+
export function useFieldRoot(options: UseFieldRootOptions): UseFieldRootResult {
|
|
6
|
+
const {
|
|
7
|
+
'aria-busy': ariaBusy,
|
|
8
|
+
className,
|
|
9
|
+
isLoading = false,
|
|
10
|
+
...rootProps
|
|
11
|
+
} = options
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
fieldRootProps: {
|
|
15
|
+
...rootProps,
|
|
16
|
+
'aria-busy': ariaBusy ?? (isLoading ? true : undefined),
|
|
17
|
+
className: clsx(styles.field, className),
|
|
18
|
+
'data-loading': isLoading ? '' : undefined,
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { UseLabelOptions, UseLabelResult } from './Field.types'
|
|
2
|
+
import clsx from 'clsx'
|
|
3
|
+
import { useFieldContext } from './useFieldContext'
|
|
4
|
+
import styles from './Field.module.css'
|
|
5
|
+
|
|
6
|
+
export function useLabel(options: UseLabelOptions): UseLabelResult {
|
|
7
|
+
const { children, className, htmlFor, id, ...labelRootProps } = options
|
|
8
|
+
const field = useFieldContext('Label')
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
labelProps: {
|
|
12
|
+
...labelRootProps,
|
|
13
|
+
children: children ?? field.label,
|
|
14
|
+
className: clsx(styles.label, className),
|
|
15
|
+
'data-loading': field.isLoading ? '' : undefined,
|
|
16
|
+
'data-readonly': field.isReadOnly ? '' : undefined,
|
|
17
|
+
'data-required': field.isRequired ? '' : undefined,
|
|
18
|
+
htmlFor: htmlFor ?? field.controlId,
|
|
19
|
+
id: id ?? field.labelId,
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
}
|
package/package.json
CHANGED
package/tailwind-css/Field.tsx
CHANGED
|
@@ -5,22 +5,15 @@ import type {
|
|
|
5
5
|
FieldErrorPolicy,
|
|
6
6
|
UseControlOptions,
|
|
7
7
|
} from './Field.types'
|
|
8
|
-
import type {
|
|
9
|
-
ElementType,
|
|
10
|
-
HTMLAttributes,
|
|
11
|
-
LabelHTMLAttributes,
|
|
12
|
-
ReactNode,
|
|
13
|
-
} from 'react'
|
|
8
|
+
import type { ComponentPropsWithRef, ElementType, ReactNode } from 'react'
|
|
14
9
|
import { FieldProvider } from './FieldContext'
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
useLabel,
|
|
21
|
-
} from './useField'
|
|
10
|
+
import { useControl } from './useControl'
|
|
11
|
+
import { useDescription } from './useDescription'
|
|
12
|
+
import { useErrorMessage } from './useErrorMessage'
|
|
13
|
+
import { useFieldRoot } from './useFieldRoot'
|
|
14
|
+
import { useLabel } from './useLabel'
|
|
22
15
|
|
|
23
|
-
export type FieldRootProps =
|
|
16
|
+
export type FieldRootProps = ComponentPropsWithRef<'div'> & {
|
|
24
17
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
|
25
18
|
as?: ElementType
|
|
26
19
|
controlId?: string
|
|
@@ -69,7 +62,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
69
62
|
)
|
|
70
63
|
}
|
|
71
64
|
|
|
72
|
-
export type LabelProps =
|
|
65
|
+
export type LabelProps = ComponentPropsWithRef<'label'>
|
|
73
66
|
|
|
74
67
|
export function Label(props: LabelProps) {
|
|
75
68
|
const { labelProps } = useLabel(props)
|
|
@@ -88,7 +81,7 @@ export function Control(props: ControlProps) {
|
|
|
88
81
|
return children(control)
|
|
89
82
|
}
|
|
90
83
|
|
|
91
|
-
export type DescriptionProps =
|
|
84
|
+
export type DescriptionProps = ComponentPropsWithRef<'div'> & {
|
|
92
85
|
as?: ElementType
|
|
93
86
|
}
|
|
94
87
|
|
|
@@ -100,7 +93,7 @@ export function Description(props: DescriptionProps) {
|
|
|
100
93
|
}
|
|
101
94
|
|
|
102
95
|
export type ErrorMessageProps = Omit<
|
|
103
|
-
|
|
96
|
+
ComponentPropsWithRef<'div'>,
|
|
104
97
|
'children'
|
|
105
98
|
> & {
|
|
106
99
|
as?: ElementType
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AriaAttributes,
|
|
3
3
|
ChangeEventHandler,
|
|
4
|
+
ComponentPropsWithRef,
|
|
4
5
|
FocusEventHandler,
|
|
5
6
|
HTMLAttributes,
|
|
6
|
-
LabelHTMLAttributes,
|
|
7
7
|
ReactEventHandler,
|
|
8
8
|
ReactNode,
|
|
9
|
+
Ref,
|
|
9
10
|
} from 'react'
|
|
10
11
|
|
|
11
12
|
export type ValidityErrorKey = Exclude<keyof ValidityState, 'valid'>
|
|
@@ -72,11 +73,11 @@ export type FieldProviderProps = {
|
|
|
72
73
|
name: string
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
export type UseFieldRootOptions =
|
|
76
|
+
export type UseFieldRootOptions = ComponentPropsWithRef<'div'> & {
|
|
76
77
|
isLoading?: boolean
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
export type FieldRootRenderProps =
|
|
80
|
+
export type FieldRootRenderProps = ComponentPropsWithRef<'div'> & {
|
|
80
81
|
'data-loading'?: ''
|
|
81
82
|
}
|
|
82
83
|
|
|
@@ -84,9 +85,9 @@ export type UseFieldRootResult = {
|
|
|
84
85
|
fieldRootProps: FieldRootRenderProps
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
export type UseLabelOptions =
|
|
88
|
+
export type UseLabelOptions = ComponentPropsWithRef<'label'>
|
|
88
89
|
|
|
89
|
-
export type LabelRootProps =
|
|
90
|
+
export type LabelRootProps = ComponentPropsWithRef<'label'> & {
|
|
90
91
|
'data-loading'?: ''
|
|
91
92
|
'data-readonly'?: ''
|
|
92
93
|
'data-required'?: ''
|
|
@@ -125,6 +126,7 @@ export type ControlRenderProps = ControlElementProps & {
|
|
|
125
126
|
onInput?: ReactEventHandler<ControlElement>
|
|
126
127
|
onInvalid?: ReactEventHandler<ControlElement>
|
|
127
128
|
readOnly?: boolean
|
|
129
|
+
ref?: Ref<any>
|
|
128
130
|
required?: boolean
|
|
129
131
|
role?: ControlRole
|
|
130
132
|
}
|
|
@@ -177,14 +179,14 @@ export type WithValidity = <TEvent extends { currentTarget: EventTarget }>(
|
|
|
177
179
|
}
|
|
178
180
|
}
|
|
179
181
|
|
|
180
|
-
export type UseDescriptionOptions =
|
|
182
|
+
export type UseDescriptionOptions = ComponentPropsWithRef<'div'>
|
|
181
183
|
|
|
182
184
|
export type UseDescriptionResult = {
|
|
183
|
-
descriptionProps:
|
|
185
|
+
descriptionProps: ComponentPropsWithRef<'div'>
|
|
184
186
|
}
|
|
185
187
|
|
|
186
188
|
export type UseErrorMessageOptions = Omit<
|
|
187
|
-
|
|
189
|
+
ComponentPropsWithRef<'div'>,
|
|
188
190
|
'children'
|
|
189
191
|
>
|
|
190
192
|
|
|
@@ -196,6 +198,6 @@ export type ErrorMessageItemProps = {
|
|
|
196
198
|
}
|
|
197
199
|
|
|
198
200
|
export type UseErrorMessageResult = {
|
|
199
|
-
errorMessageProps:
|
|
201
|
+
errorMessageProps: ComponentPropsWithRef<'div'>
|
|
200
202
|
items: readonly ErrorMessageItemProps[]
|
|
201
203
|
}
|
package/tailwind-css/index.ts
CHANGED
|
@@ -28,10 +28,8 @@ export type {
|
|
|
28
28
|
} from './Field.types'
|
|
29
29
|
export { Control, Description, ErrorMessage, FieldRoot, Label } from './Field'
|
|
30
30
|
export { FieldProvider } from './FieldContext'
|
|
31
|
-
export {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
useLabel,
|
|
37
|
-
} from './useField'
|
|
31
|
+
export { useControl } from './useControl'
|
|
32
|
+
export { useDescription } from './useDescription'
|
|
33
|
+
export { useErrorMessage } from './useErrorMessage'
|
|
34
|
+
export { useFieldRoot } from './useFieldRoot'
|
|
35
|
+
export { useLabel } from './useLabel'
|
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { UseDescriptionOptions, UseDescriptionResult } from './Field.types'
|
|
2
|
+
import clsx from 'clsx'
|
|
3
|
+
import { useFieldContext } from './useFieldContext'
|
|
4
|
+
import './Field.css'
|
|
5
|
+
|
|
6
|
+
export function useDescription(
|
|
7
|
+
options: UseDescriptionOptions,
|
|
8
|
+
): UseDescriptionResult {
|
|
9
|
+
const { children, className, id, ...descriptionRootProps } = options
|
|
10
|
+
const field = useFieldContext('Description')
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
descriptionProps: {
|
|
14
|
+
...descriptionRootProps,
|
|
15
|
+
children: children ?? field.description,
|
|
16
|
+
className: clsx('maratus__Field__description', className),
|
|
17
|
+
id: id ?? field.descriptionId,
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ErrorMessageItemProps,
|
|
3
|
+
FieldErrorKey,
|
|
4
|
+
UseErrorMessageOptions,
|
|
5
|
+
UseErrorMessageResult,
|
|
6
|
+
} from './Field.types'
|
|
7
|
+
import type { ReactNode } from 'react'
|
|
8
|
+
import clsx from 'clsx'
|
|
9
|
+
import { useFieldContext } from './useFieldContext'
|
|
10
|
+
import './Field.css'
|
|
11
|
+
|
|
12
|
+
export function useErrorMessage(
|
|
13
|
+
options: UseErrorMessageOptions,
|
|
14
|
+
): UseErrorMessageResult {
|
|
15
|
+
const { className, id, ...errorMessageRootProps } = options
|
|
16
|
+
const field = useFieldContext('ErrorMessage')
|
|
17
|
+
const items = field.visibleErrors
|
|
18
|
+
.map((errorKey) => [errorKey, field.errorMap?.get(errorKey)] as const)
|
|
19
|
+
.filter(
|
|
20
|
+
(entry): entry is readonly [FieldErrorKey, ReactNode] => entry[1] != null,
|
|
21
|
+
)
|
|
22
|
+
.map(
|
|
23
|
+
([errorKey, message]): ErrorMessageItemProps => ({
|
|
24
|
+
children: message,
|
|
25
|
+
className: 'maratus__Field__errorMessage',
|
|
26
|
+
errorKey,
|
|
27
|
+
key: errorKey,
|
|
28
|
+
}),
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
errorMessageProps: {
|
|
33
|
+
...errorMessageRootProps,
|
|
34
|
+
className: clsx('maratus__Field__errorMessageRoot', className),
|
|
35
|
+
id: id ?? field.errorId,
|
|
36
|
+
role: field.visibleErrors.length > 0 ? 'alert' : undefined,
|
|
37
|
+
},
|
|
38
|
+
items,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FieldContextValue } from './Field.types'
|
|
2
|
+
import { useContext } from 'react'
|
|
3
|
+
import { FieldContext } from './FieldContext'
|
|
4
|
+
|
|
5
|
+
export function useFieldContext(componentName: string): FieldContextValue {
|
|
6
|
+
const context = useContext(FieldContext)
|
|
7
|
+
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new globalThis.Error(
|
|
10
|
+
`${componentName} must be rendered inside FieldRoot.`,
|
|
11
|
+
)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return context
|
|
15
|
+
}
|