@maratus-registry/field 0.0.0 → 0.1.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 +5 -7
- package/css-files/index.ts +5 -7
- package/css-files/useControl.ts +131 -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 +5 -7
- package/css-modules/index.ts +5 -7
- package/css-modules/useControl.ts +131 -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 +5 -7
- package/tailwind-css/index.ts +5 -7
- package/tailwind-css/useControl.ts +131 -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
package/css-files/Field.tsx
CHANGED
|
@@ -12,13 +12,11 @@ import type {
|
|
|
12
12
|
ReactNode,
|
|
13
13
|
} from 'react'
|
|
14
14
|
import { FieldProvider } from './FieldContext'
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
useLabel,
|
|
21
|
-
} from './useField'
|
|
15
|
+
import { useControl } from './useControl'
|
|
16
|
+
import { useDescription } from './useDescription'
|
|
17
|
+
import { useErrorMessage } from './useErrorMessage'
|
|
18
|
+
import { useFieldRoot } from './useFieldRoot'
|
|
19
|
+
import { useLabel } from './useLabel'
|
|
22
20
|
|
|
23
21
|
export type FieldRootProps = HTMLAttributes<HTMLDivElement> & {
|
|
24
22
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
package/css-files/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,131 @@
|
|
|
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 { className, role, ...controlRootProps } = options
|
|
15
|
+
const field = useRequiredFieldContext('Control')
|
|
16
|
+
const relationshipProps = {
|
|
17
|
+
'aria-busy': field.isLoading ? true : undefined,
|
|
18
|
+
'aria-describedby': field.description ? field.descriptionId : undefined,
|
|
19
|
+
'aria-disabled': field.isLoading ? true : undefined,
|
|
20
|
+
'aria-errormessage':
|
|
21
|
+
field.visibleErrors.length > 0 ? field.errorId : undefined,
|
|
22
|
+
'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
|
|
23
|
+
className: clsx('maratus__Field__control', className),
|
|
24
|
+
'data-loading': field.isLoading ? '' : undefined,
|
|
25
|
+
id: field.controlId,
|
|
26
|
+
} satisfies Pick<
|
|
27
|
+
ControlRenderProps,
|
|
28
|
+
| 'aria-busy'
|
|
29
|
+
| 'aria-describedby'
|
|
30
|
+
| 'aria-disabled'
|
|
31
|
+
| 'aria-errormessage'
|
|
32
|
+
| 'aria-invalid'
|
|
33
|
+
| 'className'
|
|
34
|
+
| 'data-loading'
|
|
35
|
+
| 'id'
|
|
36
|
+
>
|
|
37
|
+
const validityHandlerProps = getValidityHandlerProps(
|
|
38
|
+
field.evaluateNativeValidity,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if (role) {
|
|
42
|
+
return {
|
|
43
|
+
controlProps: {
|
|
44
|
+
...controlRootProps,
|
|
45
|
+
...relationshipProps,
|
|
46
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
47
|
+
...(field.isRequired ? { 'aria-required': true } : {}),
|
|
48
|
+
...validityHandlerProps,
|
|
49
|
+
role,
|
|
50
|
+
},
|
|
51
|
+
withValidity,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
controlProps: {
|
|
57
|
+
...controlRootProps,
|
|
58
|
+
...relationshipProps,
|
|
59
|
+
...(field.isLoading ? { disabled: true } : {}),
|
|
60
|
+
name: field.name,
|
|
61
|
+
...validityHandlerProps,
|
|
62
|
+
...(field.isReadOnly ? { readOnly: true } : {}),
|
|
63
|
+
...(field.isRequired ? { required: true } : {}),
|
|
64
|
+
},
|
|
65
|
+
withValidity,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const withValidity: WithValidity = (event, validity) => {
|
|
70
|
+
const currentTarget = Object.create(
|
|
71
|
+
Object.getPrototypeOf(event.currentTarget),
|
|
72
|
+
) as typeof event.currentTarget & { validity: ValidityState }
|
|
73
|
+
|
|
74
|
+
Object.assign(currentTarget, event.currentTarget, {
|
|
75
|
+
validity: createValidityState(validity),
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
...event,
|
|
80
|
+
currentTarget,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getValidityHandlerProps(
|
|
85
|
+
evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
|
|
86
|
+
): Pick<
|
|
87
|
+
ControlRenderProps,
|
|
88
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
|
+
> {
|
|
90
|
+
return {
|
|
91
|
+
onBlur: (event) =>
|
|
92
|
+
evaluateNativeValidity('blur', getValidityControl(event)),
|
|
93
|
+
onChange: (event) =>
|
|
94
|
+
evaluateNativeValidity('change', getValidityControl(event)),
|
|
95
|
+
onFocus: (event) =>
|
|
96
|
+
evaluateNativeValidity('focus', getValidityControl(event)),
|
|
97
|
+
onInput: (event) =>
|
|
98
|
+
evaluateNativeValidity('input', getValidityControl(event)),
|
|
99
|
+
onInvalid: (event) =>
|
|
100
|
+
evaluateNativeValidity('invalid', getValidityControl(event)),
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getValidityControl(event: { currentTarget: ControlElement }) {
|
|
105
|
+
return event.currentTarget as ControlElement & { validity: ValidityState }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const validityStateKeys = [
|
|
109
|
+
'badInput',
|
|
110
|
+
'customError',
|
|
111
|
+
'patternMismatch',
|
|
112
|
+
'rangeOverflow',
|
|
113
|
+
'rangeUnderflow',
|
|
114
|
+
'stepMismatch',
|
|
115
|
+
'tooLong',
|
|
116
|
+
'tooShort',
|
|
117
|
+
'typeMismatch',
|
|
118
|
+
'valueMissing',
|
|
119
|
+
] as const satisfies readonly (keyof Omit<ValidityState, 'valid'>)[]
|
|
120
|
+
|
|
121
|
+
function createValidityState(validity: Partial<ValidityState>): ValidityState {
|
|
122
|
+
const errors = Object.fromEntries(
|
|
123
|
+
validityStateKeys.map((key) => [key, validity[key] ?? false]),
|
|
124
|
+
) as Record<(typeof validityStateKeys)[number], boolean>
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
...errors,
|
|
128
|
+
valid:
|
|
129
|
+
validity.valid ?? validityStateKeys.every((key) => errors[key] === false),
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { UseFieldRootOptions, UseFieldRootResult } from './Field.types'
|
|
2
|
+
import clsx from 'clsx'
|
|
3
|
+
import './Field.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('maratus__Field__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 './Field.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('maratus__Field__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/css-modules/Field.tsx
CHANGED
|
@@ -12,13 +12,11 @@ import type {
|
|
|
12
12
|
ReactNode,
|
|
13
13
|
} from 'react'
|
|
14
14
|
import { FieldProvider } from './FieldContext'
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
useLabel,
|
|
21
|
-
} from './useField'
|
|
15
|
+
import { useControl } from './useControl'
|
|
16
|
+
import { useDescription } from './useDescription'
|
|
17
|
+
import { useErrorMessage } from './useErrorMessage'
|
|
18
|
+
import { useFieldRoot } from './useFieldRoot'
|
|
19
|
+
import { useLabel } from './useLabel'
|
|
22
20
|
|
|
23
21
|
export type FieldRootProps = HTMLAttributes<HTMLDivElement> & {
|
|
24
22
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
package/css-modules/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,131 @@
|
|
|
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 { className, role, ...controlRootProps } = options
|
|
15
|
+
const field = useRequiredFieldContext('Control')
|
|
16
|
+
const relationshipProps = {
|
|
17
|
+
'aria-busy': field.isLoading ? true : undefined,
|
|
18
|
+
'aria-describedby': field.description ? field.descriptionId : undefined,
|
|
19
|
+
'aria-disabled': field.isLoading ? true : undefined,
|
|
20
|
+
'aria-errormessage':
|
|
21
|
+
field.visibleErrors.length > 0 ? field.errorId : undefined,
|
|
22
|
+
'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
|
|
23
|
+
className: clsx(styles.control, className),
|
|
24
|
+
'data-loading': field.isLoading ? '' : undefined,
|
|
25
|
+
id: field.controlId,
|
|
26
|
+
} satisfies Pick<
|
|
27
|
+
ControlRenderProps,
|
|
28
|
+
| 'aria-busy'
|
|
29
|
+
| 'aria-describedby'
|
|
30
|
+
| 'aria-disabled'
|
|
31
|
+
| 'aria-errormessage'
|
|
32
|
+
| 'aria-invalid'
|
|
33
|
+
| 'className'
|
|
34
|
+
| 'data-loading'
|
|
35
|
+
| 'id'
|
|
36
|
+
>
|
|
37
|
+
const validityHandlerProps = getValidityHandlerProps(
|
|
38
|
+
field.evaluateNativeValidity,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if (role) {
|
|
42
|
+
return {
|
|
43
|
+
controlProps: {
|
|
44
|
+
...controlRootProps,
|
|
45
|
+
...relationshipProps,
|
|
46
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
47
|
+
...(field.isRequired ? { 'aria-required': true } : {}),
|
|
48
|
+
...validityHandlerProps,
|
|
49
|
+
role,
|
|
50
|
+
},
|
|
51
|
+
withValidity,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
controlProps: {
|
|
57
|
+
...controlRootProps,
|
|
58
|
+
...relationshipProps,
|
|
59
|
+
...(field.isLoading ? { disabled: true } : {}),
|
|
60
|
+
name: field.name,
|
|
61
|
+
...validityHandlerProps,
|
|
62
|
+
...(field.isReadOnly ? { readOnly: true } : {}),
|
|
63
|
+
...(field.isRequired ? { required: true } : {}),
|
|
64
|
+
},
|
|
65
|
+
withValidity,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const withValidity: WithValidity = (event, validity) => {
|
|
70
|
+
const currentTarget = Object.create(
|
|
71
|
+
Object.getPrototypeOf(event.currentTarget),
|
|
72
|
+
) as typeof event.currentTarget & { validity: ValidityState }
|
|
73
|
+
|
|
74
|
+
Object.assign(currentTarget, event.currentTarget, {
|
|
75
|
+
validity: createValidityState(validity),
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
...event,
|
|
80
|
+
currentTarget,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getValidityHandlerProps(
|
|
85
|
+
evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
|
|
86
|
+
): Pick<
|
|
87
|
+
ControlRenderProps,
|
|
88
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
|
+
> {
|
|
90
|
+
return {
|
|
91
|
+
onBlur: (event) =>
|
|
92
|
+
evaluateNativeValidity('blur', getValidityControl(event)),
|
|
93
|
+
onChange: (event) =>
|
|
94
|
+
evaluateNativeValidity('change', getValidityControl(event)),
|
|
95
|
+
onFocus: (event) =>
|
|
96
|
+
evaluateNativeValidity('focus', getValidityControl(event)),
|
|
97
|
+
onInput: (event) =>
|
|
98
|
+
evaluateNativeValidity('input', getValidityControl(event)),
|
|
99
|
+
onInvalid: (event) =>
|
|
100
|
+
evaluateNativeValidity('invalid', getValidityControl(event)),
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getValidityControl(event: { currentTarget: ControlElement }) {
|
|
105
|
+
return event.currentTarget as ControlElement & { validity: ValidityState }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const validityStateKeys = [
|
|
109
|
+
'badInput',
|
|
110
|
+
'customError',
|
|
111
|
+
'patternMismatch',
|
|
112
|
+
'rangeOverflow',
|
|
113
|
+
'rangeUnderflow',
|
|
114
|
+
'stepMismatch',
|
|
115
|
+
'tooLong',
|
|
116
|
+
'tooShort',
|
|
117
|
+
'typeMismatch',
|
|
118
|
+
'valueMissing',
|
|
119
|
+
] as const satisfies readonly (keyof Omit<ValidityState, 'valid'>)[]
|
|
120
|
+
|
|
121
|
+
function createValidityState(validity: Partial<ValidityState>): ValidityState {
|
|
122
|
+
const errors = Object.fromEntries(
|
|
123
|
+
validityStateKeys.map((key) => [key, validity[key] ?? false]),
|
|
124
|
+
) as Record<(typeof validityStateKeys)[number], boolean>
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
...errors,
|
|
128
|
+
valid:
|
|
129
|
+
validity.valid ?? validityStateKeys.every((key) => errors[key] === false),
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -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
|
@@ -12,13 +12,11 @@ import type {
|
|
|
12
12
|
ReactNode,
|
|
13
13
|
} from 'react'
|
|
14
14
|
import { FieldProvider } from './FieldContext'
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
useLabel,
|
|
21
|
-
} from './useField'
|
|
15
|
+
import { useControl } from './useControl'
|
|
16
|
+
import { useDescription } from './useDescription'
|
|
17
|
+
import { useErrorMessage } from './useErrorMessage'
|
|
18
|
+
import { useFieldRoot } from './useFieldRoot'
|
|
19
|
+
import { useLabel } from './useLabel'
|
|
22
20
|
|
|
23
21
|
export type FieldRootProps = HTMLAttributes<HTMLDivElement> & {
|
|
24
22
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
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,131 @@
|
|
|
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 { className, role, ...controlRootProps } = options
|
|
15
|
+
const field = useRequiredFieldContext('Control')
|
|
16
|
+
const relationshipProps = {
|
|
17
|
+
'aria-busy': field.isLoading ? true : undefined,
|
|
18
|
+
'aria-describedby': field.description ? field.descriptionId : undefined,
|
|
19
|
+
'aria-disabled': field.isLoading ? true : undefined,
|
|
20
|
+
'aria-errormessage':
|
|
21
|
+
field.visibleErrors.length > 0 ? field.errorId : undefined,
|
|
22
|
+
'aria-invalid': field.visibleErrors.length > 0 ? true : undefined,
|
|
23
|
+
className: clsx('maratus__Field__control', className),
|
|
24
|
+
'data-loading': field.isLoading ? '' : undefined,
|
|
25
|
+
id: field.controlId,
|
|
26
|
+
} satisfies Pick<
|
|
27
|
+
ControlRenderProps,
|
|
28
|
+
| 'aria-busy'
|
|
29
|
+
| 'aria-describedby'
|
|
30
|
+
| 'aria-disabled'
|
|
31
|
+
| 'aria-errormessage'
|
|
32
|
+
| 'aria-invalid'
|
|
33
|
+
| 'className'
|
|
34
|
+
| 'data-loading'
|
|
35
|
+
| 'id'
|
|
36
|
+
>
|
|
37
|
+
const validityHandlerProps = getValidityHandlerProps(
|
|
38
|
+
field.evaluateNativeValidity,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if (role) {
|
|
42
|
+
return {
|
|
43
|
+
controlProps: {
|
|
44
|
+
...controlRootProps,
|
|
45
|
+
...relationshipProps,
|
|
46
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
47
|
+
...(field.isRequired ? { 'aria-required': true } : {}),
|
|
48
|
+
...validityHandlerProps,
|
|
49
|
+
role,
|
|
50
|
+
},
|
|
51
|
+
withValidity,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
controlProps: {
|
|
57
|
+
...controlRootProps,
|
|
58
|
+
...relationshipProps,
|
|
59
|
+
...(field.isLoading ? { disabled: true } : {}),
|
|
60
|
+
name: field.name,
|
|
61
|
+
...validityHandlerProps,
|
|
62
|
+
...(field.isReadOnly ? { readOnly: true } : {}),
|
|
63
|
+
...(field.isRequired ? { required: true } : {}),
|
|
64
|
+
},
|
|
65
|
+
withValidity,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const withValidity: WithValidity = (event, validity) => {
|
|
70
|
+
const currentTarget = Object.create(
|
|
71
|
+
Object.getPrototypeOf(event.currentTarget),
|
|
72
|
+
) as typeof event.currentTarget & { validity: ValidityState }
|
|
73
|
+
|
|
74
|
+
Object.assign(currentTarget, event.currentTarget, {
|
|
75
|
+
validity: createValidityState(validity),
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
...event,
|
|
80
|
+
currentTarget,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getValidityHandlerProps(
|
|
85
|
+
evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
|
|
86
|
+
): Pick<
|
|
87
|
+
ControlRenderProps,
|
|
88
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
|
+
> {
|
|
90
|
+
return {
|
|
91
|
+
onBlur: (event) =>
|
|
92
|
+
evaluateNativeValidity('blur', getValidityControl(event)),
|
|
93
|
+
onChange: (event) =>
|
|
94
|
+
evaluateNativeValidity('change', getValidityControl(event)),
|
|
95
|
+
onFocus: (event) =>
|
|
96
|
+
evaluateNativeValidity('focus', getValidityControl(event)),
|
|
97
|
+
onInput: (event) =>
|
|
98
|
+
evaluateNativeValidity('input', getValidityControl(event)),
|
|
99
|
+
onInvalid: (event) =>
|
|
100
|
+
evaluateNativeValidity('invalid', getValidityControl(event)),
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getValidityControl(event: { currentTarget: ControlElement }) {
|
|
105
|
+
return event.currentTarget as ControlElement & { validity: ValidityState }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const validityStateKeys = [
|
|
109
|
+
'badInput',
|
|
110
|
+
'customError',
|
|
111
|
+
'patternMismatch',
|
|
112
|
+
'rangeOverflow',
|
|
113
|
+
'rangeUnderflow',
|
|
114
|
+
'stepMismatch',
|
|
115
|
+
'tooLong',
|
|
116
|
+
'tooShort',
|
|
117
|
+
'typeMismatch',
|
|
118
|
+
'valueMissing',
|
|
119
|
+
] as const satisfies readonly (keyof Omit<ValidityState, 'valid'>)[]
|
|
120
|
+
|
|
121
|
+
function createValidityState(validity: Partial<ValidityState>): ValidityState {
|
|
122
|
+
const errors = Object.fromEntries(
|
|
123
|
+
validityStateKeys.map((key) => [key, validity[key] ?? false]),
|
|
124
|
+
) as Record<(typeof validityStateKeys)[number], boolean>
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
...errors,
|
|
128
|
+
valid:
|
|
129
|
+
validity.valid ?? validityStateKeys.every((key) => errors[key] === false),
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { UseFieldRootOptions, UseFieldRootResult } from './Field.types'
|
|
2
|
+
import clsx from 'clsx'
|
|
3
|
+
import './Field.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('maratus__Field__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 './Field.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('maratus__Field__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/css-files/useField.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { useControl } from './useControl'
|
|
2
|
-
export { useDescription } from './useDescription'
|
|
3
|
-
export { useErrorMessage } from './useErrorMessage'
|
|
4
|
-
export { useFieldContext } from './useFieldContext'
|
|
5
|
-
export { useFieldRoot } from './useFieldRoot'
|
|
6
|
-
export { useLabel } from './useLabel'
|
package/css-modules/useField.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { useControl } from './useControl'
|
|
2
|
-
export { useDescription } from './useDescription'
|
|
3
|
-
export { useErrorMessage } from './useErrorMessage'
|
|
4
|
-
export { useFieldContext } from './useFieldContext'
|
|
5
|
-
export { useFieldRoot } from './useFieldRoot'
|
|
6
|
-
export { useLabel } from './useLabel'
|
package/tailwind-css/useField.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { useControl } from './useControl'
|
|
2
|
-
export { useDescription } from './useDescription'
|
|
3
|
-
export { useErrorMessage } from './useErrorMessage'
|
|
4
|
-
export { useFieldContext } from './useFieldContext'
|
|
5
|
-
export { useFieldRoot } from './useFieldRoot'
|
|
6
|
-
export { useLabel } from './useLabel'
|