@maratus-registry/field 0.1.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 +5 -10
- package/css-files/Field.types.ts +11 -9
- package/css-files/useControl.ts +43 -13
- package/css-modules/Field.tsx +5 -10
- package/css-modules/Field.types.ts +11 -9
- package/css-modules/useControl.ts +43 -13
- package/package.json +1 -1
- package/tailwind-css/Field.tsx +5 -10
- package/tailwind-css/Field.types.ts +11 -9
- package/tailwind-css/useControl.ts +43 -13
package/css-files/Field.tsx
CHANGED
|
@@ -5,12 +5,7 @@ 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
10
|
import { useControl } from './useControl'
|
|
16
11
|
import { useDescription } from './useDescription'
|
|
@@ -18,7 +13,7 @@ import { useErrorMessage } from './useErrorMessage'
|
|
|
18
13
|
import { useFieldRoot } from './useFieldRoot'
|
|
19
14
|
import { useLabel } from './useLabel'
|
|
20
15
|
|
|
21
|
-
export type FieldRootProps =
|
|
16
|
+
export type FieldRootProps = ComponentPropsWithRef<'div'> & {
|
|
22
17
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
|
23
18
|
as?: ElementType
|
|
24
19
|
controlId?: string
|
|
@@ -67,7 +62,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
67
62
|
)
|
|
68
63
|
}
|
|
69
64
|
|
|
70
|
-
export type LabelProps =
|
|
65
|
+
export type LabelProps = ComponentPropsWithRef<'label'>
|
|
71
66
|
|
|
72
67
|
export function Label(props: LabelProps) {
|
|
73
68
|
const { labelProps } = useLabel(props)
|
|
@@ -86,7 +81,7 @@ export function Control(props: ControlProps) {
|
|
|
86
81
|
return children(control)
|
|
87
82
|
}
|
|
88
83
|
|
|
89
|
-
export type DescriptionProps =
|
|
84
|
+
export type DescriptionProps = ComponentPropsWithRef<'div'> & {
|
|
90
85
|
as?: ElementType
|
|
91
86
|
}
|
|
92
87
|
|
|
@@ -98,7 +93,7 @@ export function Description(props: DescriptionProps) {
|
|
|
98
93
|
}
|
|
99
94
|
|
|
100
95
|
export type ErrorMessageProps = Omit<
|
|
101
|
-
|
|
96
|
+
ComponentPropsWithRef<'div'>,
|
|
102
97
|
'children'
|
|
103
98
|
> & {
|
|
104
99
|
as?: ElementType
|
package/css-files/Field.types.ts
CHANGED
|
@@ -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/css-files/useControl.ts
CHANGED
|
@@ -11,7 +11,16 @@ import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
|
|
|
11
11
|
import './Field.css'
|
|
12
12
|
|
|
13
13
|
export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
className,
|
|
16
|
+
onBlur,
|
|
17
|
+
onChange,
|
|
18
|
+
onFocus,
|
|
19
|
+
onInput,
|
|
20
|
+
onInvalid,
|
|
21
|
+
role,
|
|
22
|
+
...controlRootProps
|
|
23
|
+
} = options
|
|
15
24
|
const field = useRequiredFieldContext('Control')
|
|
16
25
|
const relationshipProps = {
|
|
17
26
|
'aria-busy': field.isLoading ? true : undefined,
|
|
@@ -34,7 +43,14 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
|
34
43
|
| 'data-loading'
|
|
35
44
|
| 'id'
|
|
36
45
|
>
|
|
37
|
-
const validityHandlerProps =
|
|
46
|
+
const validityHandlerProps = composeValidityHandlerProps(
|
|
47
|
+
{
|
|
48
|
+
onBlur,
|
|
49
|
+
onChange,
|
|
50
|
+
onFocus,
|
|
51
|
+
onInput,
|
|
52
|
+
onInvalid,
|
|
53
|
+
},
|
|
38
54
|
field.evaluateNativeValidity,
|
|
39
55
|
)
|
|
40
56
|
|
|
@@ -81,23 +97,37 @@ const withValidity: WithValidity = (event, validity) => {
|
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
function
|
|
100
|
+
function composeValidityHandlerProps(
|
|
101
|
+
userHandlers: Pick<
|
|
102
|
+
UseControlOptions,
|
|
103
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
104
|
+
>,
|
|
85
105
|
evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
|
|
86
106
|
): Pick<
|
|
87
107
|
ControlRenderProps,
|
|
88
108
|
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
109
|
> {
|
|
90
110
|
return {
|
|
91
|
-
onBlur: (event) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
+
},
|
|
101
131
|
}
|
|
102
132
|
}
|
|
103
133
|
|
package/css-modules/Field.tsx
CHANGED
|
@@ -5,12 +5,7 @@ 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
10
|
import { useControl } from './useControl'
|
|
16
11
|
import { useDescription } from './useDescription'
|
|
@@ -18,7 +13,7 @@ import { useErrorMessage } from './useErrorMessage'
|
|
|
18
13
|
import { useFieldRoot } from './useFieldRoot'
|
|
19
14
|
import { useLabel } from './useLabel'
|
|
20
15
|
|
|
21
|
-
export type FieldRootProps =
|
|
16
|
+
export type FieldRootProps = ComponentPropsWithRef<'div'> & {
|
|
22
17
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
|
23
18
|
as?: ElementType
|
|
24
19
|
controlId?: string
|
|
@@ -67,7 +62,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
67
62
|
)
|
|
68
63
|
}
|
|
69
64
|
|
|
70
|
-
export type LabelProps =
|
|
65
|
+
export type LabelProps = ComponentPropsWithRef<'label'>
|
|
71
66
|
|
|
72
67
|
export function Label(props: LabelProps) {
|
|
73
68
|
const { labelProps } = useLabel(props)
|
|
@@ -86,7 +81,7 @@ export function Control(props: ControlProps) {
|
|
|
86
81
|
return children(control)
|
|
87
82
|
}
|
|
88
83
|
|
|
89
|
-
export type DescriptionProps =
|
|
84
|
+
export type DescriptionProps = ComponentPropsWithRef<'div'> & {
|
|
90
85
|
as?: ElementType
|
|
91
86
|
}
|
|
92
87
|
|
|
@@ -98,7 +93,7 @@ export function Description(props: DescriptionProps) {
|
|
|
98
93
|
}
|
|
99
94
|
|
|
100
95
|
export type ErrorMessageProps = Omit<
|
|
101
|
-
|
|
96
|
+
ComponentPropsWithRef<'div'>,
|
|
102
97
|
'children'
|
|
103
98
|
> & {
|
|
104
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
|
}
|
|
@@ -11,7 +11,16 @@ import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
|
|
|
11
11
|
import styles from './Field.module.css'
|
|
12
12
|
|
|
13
13
|
export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
className,
|
|
16
|
+
onBlur,
|
|
17
|
+
onChange,
|
|
18
|
+
onFocus,
|
|
19
|
+
onInput,
|
|
20
|
+
onInvalid,
|
|
21
|
+
role,
|
|
22
|
+
...controlRootProps
|
|
23
|
+
} = options
|
|
15
24
|
const field = useRequiredFieldContext('Control')
|
|
16
25
|
const relationshipProps = {
|
|
17
26
|
'aria-busy': field.isLoading ? true : undefined,
|
|
@@ -34,7 +43,14 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
|
34
43
|
| 'data-loading'
|
|
35
44
|
| 'id'
|
|
36
45
|
>
|
|
37
|
-
const validityHandlerProps =
|
|
46
|
+
const validityHandlerProps = composeValidityHandlerProps(
|
|
47
|
+
{
|
|
48
|
+
onBlur,
|
|
49
|
+
onChange,
|
|
50
|
+
onFocus,
|
|
51
|
+
onInput,
|
|
52
|
+
onInvalid,
|
|
53
|
+
},
|
|
38
54
|
field.evaluateNativeValidity,
|
|
39
55
|
)
|
|
40
56
|
|
|
@@ -81,23 +97,37 @@ const withValidity: WithValidity = (event, validity) => {
|
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
function
|
|
100
|
+
function composeValidityHandlerProps(
|
|
101
|
+
userHandlers: Pick<
|
|
102
|
+
UseControlOptions,
|
|
103
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
104
|
+
>,
|
|
85
105
|
evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
|
|
86
106
|
): Pick<
|
|
87
107
|
ControlRenderProps,
|
|
88
108
|
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
109
|
> {
|
|
90
110
|
return {
|
|
91
|
-
onBlur: (event) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
+
},
|
|
101
131
|
}
|
|
102
132
|
}
|
|
103
133
|
|
package/package.json
CHANGED
package/tailwind-css/Field.tsx
CHANGED
|
@@ -5,12 +5,7 @@ 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
10
|
import { useControl } from './useControl'
|
|
16
11
|
import { useDescription } from './useDescription'
|
|
@@ -18,7 +13,7 @@ import { useErrorMessage } from './useErrorMessage'
|
|
|
18
13
|
import { useFieldRoot } from './useFieldRoot'
|
|
19
14
|
import { useLabel } from './useLabel'
|
|
20
15
|
|
|
21
|
-
export type FieldRootProps =
|
|
16
|
+
export type FieldRootProps = ComponentPropsWithRef<'div'> & {
|
|
22
17
|
activeErrors?: ReadonlySet<FieldErrorKey>
|
|
23
18
|
as?: ElementType
|
|
24
19
|
controlId?: string
|
|
@@ -67,7 +62,7 @@ export function FieldRoot(props: FieldRootProps) {
|
|
|
67
62
|
)
|
|
68
63
|
}
|
|
69
64
|
|
|
70
|
-
export type LabelProps =
|
|
65
|
+
export type LabelProps = ComponentPropsWithRef<'label'>
|
|
71
66
|
|
|
72
67
|
export function Label(props: LabelProps) {
|
|
73
68
|
const { labelProps } = useLabel(props)
|
|
@@ -86,7 +81,7 @@ export function Control(props: ControlProps) {
|
|
|
86
81
|
return children(control)
|
|
87
82
|
}
|
|
88
83
|
|
|
89
|
-
export type DescriptionProps =
|
|
84
|
+
export type DescriptionProps = ComponentPropsWithRef<'div'> & {
|
|
90
85
|
as?: ElementType
|
|
91
86
|
}
|
|
92
87
|
|
|
@@ -98,7 +93,7 @@ export function Description(props: DescriptionProps) {
|
|
|
98
93
|
}
|
|
99
94
|
|
|
100
95
|
export type ErrorMessageProps = Omit<
|
|
101
|
-
|
|
96
|
+
ComponentPropsWithRef<'div'>,
|
|
102
97
|
'children'
|
|
103
98
|
> & {
|
|
104
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
|
}
|
|
@@ -11,7 +11,16 @@ import { useFieldContext as useRequiredFieldContext } from './useFieldContext'
|
|
|
11
11
|
import './Field.css'
|
|
12
12
|
|
|
13
13
|
export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
className,
|
|
16
|
+
onBlur,
|
|
17
|
+
onChange,
|
|
18
|
+
onFocus,
|
|
19
|
+
onInput,
|
|
20
|
+
onInvalid,
|
|
21
|
+
role,
|
|
22
|
+
...controlRootProps
|
|
23
|
+
} = options
|
|
15
24
|
const field = useRequiredFieldContext('Control')
|
|
16
25
|
const relationshipProps = {
|
|
17
26
|
'aria-busy': field.isLoading ? true : undefined,
|
|
@@ -34,7 +43,14 @@ export function useControl(options: UseControlOptions = {}): UseControlResult {
|
|
|
34
43
|
| 'data-loading'
|
|
35
44
|
| 'id'
|
|
36
45
|
>
|
|
37
|
-
const validityHandlerProps =
|
|
46
|
+
const validityHandlerProps = composeValidityHandlerProps(
|
|
47
|
+
{
|
|
48
|
+
onBlur,
|
|
49
|
+
onChange,
|
|
50
|
+
onFocus,
|
|
51
|
+
onInput,
|
|
52
|
+
onInvalid,
|
|
53
|
+
},
|
|
38
54
|
field.evaluateNativeValidity,
|
|
39
55
|
)
|
|
40
56
|
|
|
@@ -81,23 +97,37 @@ const withValidity: WithValidity = (event, validity) => {
|
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
function
|
|
100
|
+
function composeValidityHandlerProps(
|
|
101
|
+
userHandlers: Pick<
|
|
102
|
+
UseControlOptions,
|
|
103
|
+
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
104
|
+
>,
|
|
85
105
|
evaluateNativeValidity: FieldContextValue['evaluateNativeValidity'],
|
|
86
106
|
): Pick<
|
|
87
107
|
ControlRenderProps,
|
|
88
108
|
'onBlur' | 'onChange' | 'onFocus' | 'onInput' | 'onInvalid'
|
|
89
109
|
> {
|
|
90
110
|
return {
|
|
91
|
-
onBlur: (event) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
+
},
|
|
101
131
|
}
|
|
102
132
|
}
|
|
103
133
|
|