@maratus-registry/choice-control 0.0.0-stage → 0.1.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.
- package/css-files/ChoiceControl.tsx +14 -0
- package/css-files/ChoiceControl.types.ts +163 -0
- package/css-files/getListboxControl.ts +26 -0
- package/css-files/getRadioItemsControl.ts +26 -0
- package/css-files/getRadiosControl.ts +27 -0
- package/css-files/getSelectControl.ts +26 -0
- package/css-files/index.ts +22 -0
- package/css-files/isItemDisabled.ts +9 -0
- package/css-files/omitUnsupportedReadOnly.ts +13 -0
- package/css-files/useChoiceControl.ts +36 -0
- package/css-modules/ChoiceControl.tsx +14 -0
- package/css-modules/ChoiceControl.types.ts +163 -0
- package/css-modules/getListboxControl.ts +26 -0
- package/css-modules/getRadioItemsControl.ts +26 -0
- package/css-modules/getRadiosControl.ts +27 -0
- package/css-modules/getSelectControl.ts +26 -0
- package/css-modules/index.ts +22 -0
- package/css-modules/isItemDisabled.ts +9 -0
- package/css-modules/omitUnsupportedReadOnly.ts +13 -0
- package/css-modules/useChoiceControl.ts +36 -0
- package/meta.json +5 -0
- package/package.json +18 -4
- package/tailwind-css/ChoiceControl.tsx +14 -0
- package/tailwind-css/ChoiceControl.types.ts +163 -0
- package/tailwind-css/getListboxControl.ts +26 -0
- package/tailwind-css/getRadioItemsControl.ts +26 -0
- package/tailwind-css/getRadiosControl.ts +27 -0
- package/tailwind-css/getSelectControl.ts +26 -0
- package/tailwind-css/index.ts +22 -0
- package/tailwind-css/isItemDisabled.ts +9 -0
- package/tailwind-css/omitUnsupportedReadOnly.ts +13 -0
- package/tailwind-css/useChoiceControl.ts +36 -0
- package/README.md +0 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoiceControlMode,
|
|
3
|
+
ChoiceControlProps,
|
|
4
|
+
} from './ChoiceControl.types'
|
|
5
|
+
import { useChoiceControl } from './useChoiceControl'
|
|
6
|
+
|
|
7
|
+
export function ChoiceControl<TMode extends ChoiceControlMode>(
|
|
8
|
+
props: ChoiceControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
const { children, ...hookProps } = props
|
|
11
|
+
const control = useChoiceControl<TMode>(hookProps)
|
|
12
|
+
|
|
13
|
+
return children(control)
|
|
14
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FieldContextValue,
|
|
3
|
+
FieldControlRenderProps,
|
|
4
|
+
UseFieldControlOptions,
|
|
5
|
+
UseFieldControlResult,
|
|
6
|
+
ValidityHandlerProps,
|
|
7
|
+
} from '@maratus-component/field'
|
|
8
|
+
import type {
|
|
9
|
+
ComponentPropsWithRef,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
ReactNode,
|
|
12
|
+
Ref,
|
|
13
|
+
RefCallback,
|
|
14
|
+
} from 'react'
|
|
15
|
+
|
|
16
|
+
// Custom items can be any element and pass a consumer ref through.
|
|
17
|
+
// The getter turns it into a callback that fits any element.
|
|
18
|
+
type CustomItemProps = HTMLAttributes<HTMLElement> & {
|
|
19
|
+
ref?: Ref<HTMLElement>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type CustomItemRenderProps = HTMLAttributes<HTMLElement> & {
|
|
23
|
+
ref?: RefCallback<HTMLElement>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ChoiceControlMode = keyof ChoiceControlModes
|
|
27
|
+
|
|
28
|
+
type ChoiceControlModes = {
|
|
29
|
+
listbox: {
|
|
30
|
+
options: UseFieldControlOptions
|
|
31
|
+
result: UseListboxControlResult
|
|
32
|
+
}
|
|
33
|
+
'radio-items': {
|
|
34
|
+
options: UseFieldControlOptions
|
|
35
|
+
result: UseRadioItemsControlResult
|
|
36
|
+
}
|
|
37
|
+
radios: {
|
|
38
|
+
options: UseFieldControlOptions<HTMLInputElement>
|
|
39
|
+
result: UseRadiosControlResult
|
|
40
|
+
}
|
|
41
|
+
select: {
|
|
42
|
+
options: UseFieldControlOptions<HTMLSelectElement>
|
|
43
|
+
result: UseSelectControlResult
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type UseChoiceControlOptions<
|
|
48
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
49
|
+
> = ChoiceControlModes[TMode]['options'] & { mode: TMode }
|
|
50
|
+
|
|
51
|
+
export type UseChoiceControlResult<
|
|
52
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
53
|
+
> = ChoiceControlModes[TMode]['result']
|
|
54
|
+
|
|
55
|
+
export type ChoiceControlBuilder<TMode extends ChoiceControlMode> = (input: {
|
|
56
|
+
field: FieldContextValue
|
|
57
|
+
fieldControl: UseFieldControlResult
|
|
58
|
+
}) => UseChoiceControlResult<TMode>
|
|
59
|
+
|
|
60
|
+
export type RadiosControlRenderProps = FieldControlRenderProps & {
|
|
61
|
+
'aria-labelledby': string
|
|
62
|
+
role: 'radiogroup'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type RadioInputOptions = Omit<
|
|
66
|
+
ComponentPropsWithRef<'input'>,
|
|
67
|
+
| 'disabled'
|
|
68
|
+
| 'name'
|
|
69
|
+
| 'readOnly'
|
|
70
|
+
| 'required'
|
|
71
|
+
| 'type'
|
|
72
|
+
| 'value'
|
|
73
|
+
| keyof ValidityHandlerProps
|
|
74
|
+
> & {
|
|
75
|
+
isDisabled?: boolean
|
|
76
|
+
value: string
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type RadioInputRenderProps = ComponentPropsWithRef<'input'> &
|
|
80
|
+
ValidityHandlerProps & {
|
|
81
|
+
disabled?: true
|
|
82
|
+
name: string
|
|
83
|
+
required?: true
|
|
84
|
+
type: 'radio'
|
|
85
|
+
value: string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type UseRadiosControlResult = {
|
|
89
|
+
choiceControlProps: RadiosControlRenderProps
|
|
90
|
+
getChoiceInputProps: (options: RadioInputOptions) => RadioInputRenderProps
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type RadioItemsControlRenderProps = FieldControlRenderProps &
|
|
94
|
+
ValidityHandlerProps & {
|
|
95
|
+
'aria-labelledby': string
|
|
96
|
+
role: 'radiogroup'
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type RadioItemOptions = Omit<
|
|
100
|
+
CustomItemProps,
|
|
101
|
+
'aria-checked' | 'aria-disabled' | 'aria-required' | 'role'
|
|
102
|
+
> & {
|
|
103
|
+
isDisabled?: boolean
|
|
104
|
+
isSelected: boolean
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export type RadioItemRenderProps = CustomItemRenderProps & {
|
|
108
|
+
'aria-checked': boolean
|
|
109
|
+
'aria-disabled'?: true
|
|
110
|
+
role: 'radio'
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type UseRadioItemsControlResult = {
|
|
114
|
+
choiceControlProps: RadioItemsControlRenderProps
|
|
115
|
+
getChoiceItemProps: (options: RadioItemOptions) => RadioItemRenderProps
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type SelectControlRenderProps = Omit<
|
|
119
|
+
FieldControlRenderProps,
|
|
120
|
+
'aria-disabled' | 'aria-required'
|
|
121
|
+
> &
|
|
122
|
+
ValidityHandlerProps & {
|
|
123
|
+
disabled?: true
|
|
124
|
+
name: string
|
|
125
|
+
required?: true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type UseSelectControlResult = {
|
|
129
|
+
choiceControlProps: SelectControlRenderProps
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type ListboxControlRenderProps = FieldControlRenderProps &
|
|
133
|
+
ValidityHandlerProps & {
|
|
134
|
+
'aria-labelledby': string
|
|
135
|
+
role: 'listbox'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type ListboxOptionOptions = Omit<
|
|
139
|
+
CustomItemProps,
|
|
140
|
+
'aria-disabled' | 'aria-selected' | 'role'
|
|
141
|
+
> & {
|
|
142
|
+
isDisabled?: boolean
|
|
143
|
+
isSelected: boolean
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type ListboxOptionRenderProps = CustomItemRenderProps & {
|
|
147
|
+
'aria-disabled'?: true
|
|
148
|
+
'aria-selected': boolean
|
|
149
|
+
role: 'option'
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export type UseListboxControlResult = {
|
|
153
|
+
choiceControlProps: ListboxControlRenderProps
|
|
154
|
+
getChoiceItemProps: (
|
|
155
|
+
options: ListboxOptionOptions,
|
|
156
|
+
) => ListboxOptionRenderProps
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export type ChoiceControlProps<
|
|
160
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
161
|
+
> = UseChoiceControlOptions<TMode> & {
|
|
162
|
+
children: (props: UseChoiceControlResult<TMode>) => ReactNode
|
|
163
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getListboxControl: ChoiceControlBuilder<'listbox'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choiceControlProps: {
|
|
10
|
+
...suppressActivation(fieldControl.fieldControlProps, field.isLoading),
|
|
11
|
+
...fieldControl.validityHandlerProps,
|
|
12
|
+
ref: mergeRefs(
|
|
13
|
+
fieldControl.fieldControlProps.ref,
|
|
14
|
+
fieldControl.validityHandlerProps.ref,
|
|
15
|
+
),
|
|
16
|
+
'aria-labelledby': field.labelId,
|
|
17
|
+
role: 'listbox',
|
|
18
|
+
},
|
|
19
|
+
getChoiceItemProps: ({ isDisabled, isSelected, ref, ...optionProps }) => ({
|
|
20
|
+
...suppressActivation(optionProps, isItemDisabled(field, isDisabled)),
|
|
21
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
22
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
23
|
+
'aria-selected': isSelected,
|
|
24
|
+
role: 'option',
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getRadioItemsControl: ChoiceControlBuilder<'radio-items'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choiceControlProps: {
|
|
10
|
+
...suppressActivation(fieldControl.fieldControlProps, field.isLoading),
|
|
11
|
+
...fieldControl.validityHandlerProps,
|
|
12
|
+
ref: mergeRefs(
|
|
13
|
+
fieldControl.fieldControlProps.ref,
|
|
14
|
+
fieldControl.validityHandlerProps.ref,
|
|
15
|
+
),
|
|
16
|
+
'aria-labelledby': field.labelId,
|
|
17
|
+
role: 'radiogroup',
|
|
18
|
+
},
|
|
19
|
+
getChoiceItemProps: ({ isDisabled, isSelected, ref, ...itemProps }) => ({
|
|
20
|
+
...suppressActivation(itemProps, isItemDisabled(field, isDisabled)),
|
|
21
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
22
|
+
'aria-checked': isSelected,
|
|
23
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
24
|
+
role: 'radio',
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
5
|
+
|
|
6
|
+
export const getRadiosControl: ChoiceControlBuilder<'radios'> = ({
|
|
7
|
+
field,
|
|
8
|
+
fieldControl,
|
|
9
|
+
}) => ({
|
|
10
|
+
choiceControlProps: {
|
|
11
|
+
...fieldControl.fieldControlProps,
|
|
12
|
+
'aria-labelledby': field.labelId,
|
|
13
|
+
role: 'radiogroup',
|
|
14
|
+
},
|
|
15
|
+
getChoiceInputProps: ({ isDisabled, value, ...inputProps }) => {
|
|
16
|
+
return {
|
|
17
|
+
...omitUnsupportedReadOnly('ChoiceControl radio option', inputProps),
|
|
18
|
+
...fieldControl.validityHandlerProps,
|
|
19
|
+
ref: mergeRefs(inputProps.ref, fieldControl.validityHandlerProps.ref),
|
|
20
|
+
...(isItemDisabled(field, isDisabled) ? { disabled: true } : {}),
|
|
21
|
+
name: field.name,
|
|
22
|
+
...(field.isRequired ? { required: true } : {}),
|
|
23
|
+
type: 'radio',
|
|
24
|
+
value,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
|
|
4
|
+
export const getSelectControl: ChoiceControlBuilder<'select'> = ({
|
|
5
|
+
field,
|
|
6
|
+
fieldControl,
|
|
7
|
+
}) => {
|
|
8
|
+
const selectFieldProps = { ...fieldControl.fieldControlProps }
|
|
9
|
+
|
|
10
|
+
delete selectFieldProps['aria-disabled']
|
|
11
|
+
delete selectFieldProps['aria-required']
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
choiceControlProps: {
|
|
15
|
+
...selectFieldProps,
|
|
16
|
+
...fieldControl.validityHandlerProps,
|
|
17
|
+
ref: mergeRefs(
|
|
18
|
+
fieldControl.fieldControlProps.ref,
|
|
19
|
+
fieldControl.validityHandlerProps.ref,
|
|
20
|
+
),
|
|
21
|
+
...(field.isLoading ? { disabled: true } : {}),
|
|
22
|
+
name: field.name,
|
|
23
|
+
...(field.isRequired ? { required: true } : {}),
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ChoiceControlMode,
|
|
3
|
+
ChoiceControlProps,
|
|
4
|
+
ListboxControlRenderProps,
|
|
5
|
+
ListboxOptionOptions,
|
|
6
|
+
ListboxOptionRenderProps,
|
|
7
|
+
RadioInputOptions,
|
|
8
|
+
RadioInputRenderProps,
|
|
9
|
+
RadioItemOptions,
|
|
10
|
+
RadioItemRenderProps,
|
|
11
|
+
RadioItemsControlRenderProps,
|
|
12
|
+
RadiosControlRenderProps,
|
|
13
|
+
SelectControlRenderProps,
|
|
14
|
+
UseChoiceControlOptions,
|
|
15
|
+
UseChoiceControlResult,
|
|
16
|
+
UseListboxControlResult,
|
|
17
|
+
UseRadioItemsControlResult,
|
|
18
|
+
UseRadiosControlResult,
|
|
19
|
+
UseSelectControlResult,
|
|
20
|
+
} from './ChoiceControl.types'
|
|
21
|
+
export { ChoiceControl } from './ChoiceControl'
|
|
22
|
+
export { useChoiceControl } from './useChoiceControl'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FieldContextValue } from '@maratus-component/field'
|
|
2
|
+
|
|
3
|
+
// Loading suppresses activation, so it disables every item.
|
|
4
|
+
export function isItemDisabled(
|
|
5
|
+
field: Pick<FieldContextValue, 'isLoading'>,
|
|
6
|
+
isDisabled: boolean | undefined,
|
|
7
|
+
): boolean {
|
|
8
|
+
return Boolean(isDisabled) || field.isLoading
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { omitPropWithWarning } from '@maratus-lib/props'
|
|
2
|
+
|
|
3
|
+
// HTML readonly does not apply to select, radio, or checkbox controls.
|
|
4
|
+
export function omitUnsupportedReadOnly<TProps extends object>(
|
|
5
|
+
control: string,
|
|
6
|
+
props: TProps,
|
|
7
|
+
): Omit<TProps, 'readOnly'> {
|
|
8
|
+
return omitPropWithWarning(
|
|
9
|
+
props,
|
|
10
|
+
'readOnly',
|
|
11
|
+
`${control}: HTML readonly does not apply to this control. Use FieldRoot isReadOnly instead.`,
|
|
12
|
+
)
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoiceControlBuilder,
|
|
3
|
+
ChoiceControlMode,
|
|
4
|
+
UseChoiceControlOptions,
|
|
5
|
+
UseChoiceControlResult,
|
|
6
|
+
} from './ChoiceControl.types'
|
|
7
|
+
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
8
|
+
import { getListboxControl } from './getListboxControl'
|
|
9
|
+
import { getRadioItemsControl } from './getRadioItemsControl'
|
|
10
|
+
import { getRadiosControl } from './getRadiosControl'
|
|
11
|
+
import { getSelectControl } from './getSelectControl'
|
|
12
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
13
|
+
|
|
14
|
+
export function useChoiceControl<TMode extends ChoiceControlMode>(
|
|
15
|
+
options: UseChoiceControlOptions<TMode>,
|
|
16
|
+
): UseChoiceControlResult<TMode> {
|
|
17
|
+
const { mode, ...fieldControlOptions } = omitUnsupportedReadOnly(
|
|
18
|
+
'ChoiceControl',
|
|
19
|
+
options,
|
|
20
|
+
)
|
|
21
|
+
const field = useFieldContext('ChoiceControl')
|
|
22
|
+
const fieldControl = useFieldControl(fieldControlOptions)
|
|
23
|
+
const buildChoiceControl: ChoiceControlBuilder<TMode> =
|
|
24
|
+
choiceControlBuilders[mode]
|
|
25
|
+
|
|
26
|
+
return buildChoiceControl({ field, fieldControl })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const choiceControlBuilders: {
|
|
30
|
+
[TMode in ChoiceControlMode]: ChoiceControlBuilder<TMode>
|
|
31
|
+
} = {
|
|
32
|
+
listbox: getListboxControl,
|
|
33
|
+
'radio-items': getRadioItemsControl,
|
|
34
|
+
radios: getRadiosControl,
|
|
35
|
+
select: getSelectControl,
|
|
36
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoiceControlMode,
|
|
3
|
+
ChoiceControlProps,
|
|
4
|
+
} from './ChoiceControl.types'
|
|
5
|
+
import { useChoiceControl } from './useChoiceControl'
|
|
6
|
+
|
|
7
|
+
export function ChoiceControl<TMode extends ChoiceControlMode>(
|
|
8
|
+
props: ChoiceControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
const { children, ...hookProps } = props
|
|
11
|
+
const control = useChoiceControl<TMode>(hookProps)
|
|
12
|
+
|
|
13
|
+
return children(control)
|
|
14
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FieldContextValue,
|
|
3
|
+
FieldControlRenderProps,
|
|
4
|
+
UseFieldControlOptions,
|
|
5
|
+
UseFieldControlResult,
|
|
6
|
+
ValidityHandlerProps,
|
|
7
|
+
} from '@maratus-component/field'
|
|
8
|
+
import type {
|
|
9
|
+
ComponentPropsWithRef,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
ReactNode,
|
|
12
|
+
Ref,
|
|
13
|
+
RefCallback,
|
|
14
|
+
} from 'react'
|
|
15
|
+
|
|
16
|
+
// Custom items can be any element and pass a consumer ref through.
|
|
17
|
+
// The getter turns it into a callback that fits any element.
|
|
18
|
+
type CustomItemProps = HTMLAttributes<HTMLElement> & {
|
|
19
|
+
ref?: Ref<HTMLElement>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type CustomItemRenderProps = HTMLAttributes<HTMLElement> & {
|
|
23
|
+
ref?: RefCallback<HTMLElement>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ChoiceControlMode = keyof ChoiceControlModes
|
|
27
|
+
|
|
28
|
+
type ChoiceControlModes = {
|
|
29
|
+
listbox: {
|
|
30
|
+
options: UseFieldControlOptions
|
|
31
|
+
result: UseListboxControlResult
|
|
32
|
+
}
|
|
33
|
+
'radio-items': {
|
|
34
|
+
options: UseFieldControlOptions
|
|
35
|
+
result: UseRadioItemsControlResult
|
|
36
|
+
}
|
|
37
|
+
radios: {
|
|
38
|
+
options: UseFieldControlOptions<HTMLInputElement>
|
|
39
|
+
result: UseRadiosControlResult
|
|
40
|
+
}
|
|
41
|
+
select: {
|
|
42
|
+
options: UseFieldControlOptions<HTMLSelectElement>
|
|
43
|
+
result: UseSelectControlResult
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type UseChoiceControlOptions<
|
|
48
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
49
|
+
> = ChoiceControlModes[TMode]['options'] & { mode: TMode }
|
|
50
|
+
|
|
51
|
+
export type UseChoiceControlResult<
|
|
52
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
53
|
+
> = ChoiceControlModes[TMode]['result']
|
|
54
|
+
|
|
55
|
+
export type ChoiceControlBuilder<TMode extends ChoiceControlMode> = (input: {
|
|
56
|
+
field: FieldContextValue
|
|
57
|
+
fieldControl: UseFieldControlResult
|
|
58
|
+
}) => UseChoiceControlResult<TMode>
|
|
59
|
+
|
|
60
|
+
export type RadiosControlRenderProps = FieldControlRenderProps & {
|
|
61
|
+
'aria-labelledby': string
|
|
62
|
+
role: 'radiogroup'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type RadioInputOptions = Omit<
|
|
66
|
+
ComponentPropsWithRef<'input'>,
|
|
67
|
+
| 'disabled'
|
|
68
|
+
| 'name'
|
|
69
|
+
| 'readOnly'
|
|
70
|
+
| 'required'
|
|
71
|
+
| 'type'
|
|
72
|
+
| 'value'
|
|
73
|
+
| keyof ValidityHandlerProps
|
|
74
|
+
> & {
|
|
75
|
+
isDisabled?: boolean
|
|
76
|
+
value: string
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type RadioInputRenderProps = ComponentPropsWithRef<'input'> &
|
|
80
|
+
ValidityHandlerProps & {
|
|
81
|
+
disabled?: true
|
|
82
|
+
name: string
|
|
83
|
+
required?: true
|
|
84
|
+
type: 'radio'
|
|
85
|
+
value: string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type UseRadiosControlResult = {
|
|
89
|
+
choiceControlProps: RadiosControlRenderProps
|
|
90
|
+
getChoiceInputProps: (options: RadioInputOptions) => RadioInputRenderProps
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type RadioItemsControlRenderProps = FieldControlRenderProps &
|
|
94
|
+
ValidityHandlerProps & {
|
|
95
|
+
'aria-labelledby': string
|
|
96
|
+
role: 'radiogroup'
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type RadioItemOptions = Omit<
|
|
100
|
+
CustomItemProps,
|
|
101
|
+
'aria-checked' | 'aria-disabled' | 'aria-required' | 'role'
|
|
102
|
+
> & {
|
|
103
|
+
isDisabled?: boolean
|
|
104
|
+
isSelected: boolean
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export type RadioItemRenderProps = CustomItemRenderProps & {
|
|
108
|
+
'aria-checked': boolean
|
|
109
|
+
'aria-disabled'?: true
|
|
110
|
+
role: 'radio'
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type UseRadioItemsControlResult = {
|
|
114
|
+
choiceControlProps: RadioItemsControlRenderProps
|
|
115
|
+
getChoiceItemProps: (options: RadioItemOptions) => RadioItemRenderProps
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type SelectControlRenderProps = Omit<
|
|
119
|
+
FieldControlRenderProps,
|
|
120
|
+
'aria-disabled' | 'aria-required'
|
|
121
|
+
> &
|
|
122
|
+
ValidityHandlerProps & {
|
|
123
|
+
disabled?: true
|
|
124
|
+
name: string
|
|
125
|
+
required?: true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type UseSelectControlResult = {
|
|
129
|
+
choiceControlProps: SelectControlRenderProps
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type ListboxControlRenderProps = FieldControlRenderProps &
|
|
133
|
+
ValidityHandlerProps & {
|
|
134
|
+
'aria-labelledby': string
|
|
135
|
+
role: 'listbox'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type ListboxOptionOptions = Omit<
|
|
139
|
+
CustomItemProps,
|
|
140
|
+
'aria-disabled' | 'aria-selected' | 'role'
|
|
141
|
+
> & {
|
|
142
|
+
isDisabled?: boolean
|
|
143
|
+
isSelected: boolean
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type ListboxOptionRenderProps = CustomItemRenderProps & {
|
|
147
|
+
'aria-disabled'?: true
|
|
148
|
+
'aria-selected': boolean
|
|
149
|
+
role: 'option'
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export type UseListboxControlResult = {
|
|
153
|
+
choiceControlProps: ListboxControlRenderProps
|
|
154
|
+
getChoiceItemProps: (
|
|
155
|
+
options: ListboxOptionOptions,
|
|
156
|
+
) => ListboxOptionRenderProps
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export type ChoiceControlProps<
|
|
160
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
161
|
+
> = UseChoiceControlOptions<TMode> & {
|
|
162
|
+
children: (props: UseChoiceControlResult<TMode>) => ReactNode
|
|
163
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getListboxControl: ChoiceControlBuilder<'listbox'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choiceControlProps: {
|
|
10
|
+
...suppressActivation(fieldControl.fieldControlProps, field.isLoading),
|
|
11
|
+
...fieldControl.validityHandlerProps,
|
|
12
|
+
ref: mergeRefs(
|
|
13
|
+
fieldControl.fieldControlProps.ref,
|
|
14
|
+
fieldControl.validityHandlerProps.ref,
|
|
15
|
+
),
|
|
16
|
+
'aria-labelledby': field.labelId,
|
|
17
|
+
role: 'listbox',
|
|
18
|
+
},
|
|
19
|
+
getChoiceItemProps: ({ isDisabled, isSelected, ref, ...optionProps }) => ({
|
|
20
|
+
...suppressActivation(optionProps, isItemDisabled(field, isDisabled)),
|
|
21
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
22
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
23
|
+
'aria-selected': isSelected,
|
|
24
|
+
role: 'option',
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getRadioItemsControl: ChoiceControlBuilder<'radio-items'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choiceControlProps: {
|
|
10
|
+
...suppressActivation(fieldControl.fieldControlProps, field.isLoading),
|
|
11
|
+
...fieldControl.validityHandlerProps,
|
|
12
|
+
ref: mergeRefs(
|
|
13
|
+
fieldControl.fieldControlProps.ref,
|
|
14
|
+
fieldControl.validityHandlerProps.ref,
|
|
15
|
+
),
|
|
16
|
+
'aria-labelledby': field.labelId,
|
|
17
|
+
role: 'radiogroup',
|
|
18
|
+
},
|
|
19
|
+
getChoiceItemProps: ({ isDisabled, isSelected, ref, ...itemProps }) => ({
|
|
20
|
+
...suppressActivation(itemProps, isItemDisabled(field, isDisabled)),
|
|
21
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
22
|
+
'aria-checked': isSelected,
|
|
23
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
24
|
+
role: 'radio',
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
5
|
+
|
|
6
|
+
export const getRadiosControl: ChoiceControlBuilder<'radios'> = ({
|
|
7
|
+
field,
|
|
8
|
+
fieldControl,
|
|
9
|
+
}) => ({
|
|
10
|
+
choiceControlProps: {
|
|
11
|
+
...fieldControl.fieldControlProps,
|
|
12
|
+
'aria-labelledby': field.labelId,
|
|
13
|
+
role: 'radiogroup',
|
|
14
|
+
},
|
|
15
|
+
getChoiceInputProps: ({ isDisabled, value, ...inputProps }) => {
|
|
16
|
+
return {
|
|
17
|
+
...omitUnsupportedReadOnly('ChoiceControl radio option', inputProps),
|
|
18
|
+
...fieldControl.validityHandlerProps,
|
|
19
|
+
ref: mergeRefs(inputProps.ref, fieldControl.validityHandlerProps.ref),
|
|
20
|
+
...(isItemDisabled(field, isDisabled) ? { disabled: true } : {}),
|
|
21
|
+
name: field.name,
|
|
22
|
+
...(field.isRequired ? { required: true } : {}),
|
|
23
|
+
type: 'radio',
|
|
24
|
+
value,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
|
|
4
|
+
export const getSelectControl: ChoiceControlBuilder<'select'> = ({
|
|
5
|
+
field,
|
|
6
|
+
fieldControl,
|
|
7
|
+
}) => {
|
|
8
|
+
const selectFieldProps = { ...fieldControl.fieldControlProps }
|
|
9
|
+
|
|
10
|
+
delete selectFieldProps['aria-disabled']
|
|
11
|
+
delete selectFieldProps['aria-required']
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
choiceControlProps: {
|
|
15
|
+
...selectFieldProps,
|
|
16
|
+
...fieldControl.validityHandlerProps,
|
|
17
|
+
ref: mergeRefs(
|
|
18
|
+
fieldControl.fieldControlProps.ref,
|
|
19
|
+
fieldControl.validityHandlerProps.ref,
|
|
20
|
+
),
|
|
21
|
+
...(field.isLoading ? { disabled: true } : {}),
|
|
22
|
+
name: field.name,
|
|
23
|
+
...(field.isRequired ? { required: true } : {}),
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ChoiceControlMode,
|
|
3
|
+
ChoiceControlProps,
|
|
4
|
+
ListboxControlRenderProps,
|
|
5
|
+
ListboxOptionOptions,
|
|
6
|
+
ListboxOptionRenderProps,
|
|
7
|
+
RadioInputOptions,
|
|
8
|
+
RadioInputRenderProps,
|
|
9
|
+
RadioItemOptions,
|
|
10
|
+
RadioItemRenderProps,
|
|
11
|
+
RadioItemsControlRenderProps,
|
|
12
|
+
RadiosControlRenderProps,
|
|
13
|
+
SelectControlRenderProps,
|
|
14
|
+
UseChoiceControlOptions,
|
|
15
|
+
UseChoiceControlResult,
|
|
16
|
+
UseListboxControlResult,
|
|
17
|
+
UseRadioItemsControlResult,
|
|
18
|
+
UseRadiosControlResult,
|
|
19
|
+
UseSelectControlResult,
|
|
20
|
+
} from './ChoiceControl.types'
|
|
21
|
+
export { ChoiceControl } from './ChoiceControl'
|
|
22
|
+
export { useChoiceControl } from './useChoiceControl'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FieldContextValue } from '@maratus-component/field'
|
|
2
|
+
|
|
3
|
+
// Loading suppresses activation, so it disables every item.
|
|
4
|
+
export function isItemDisabled(
|
|
5
|
+
field: Pick<FieldContextValue, 'isLoading'>,
|
|
6
|
+
isDisabled: boolean | undefined,
|
|
7
|
+
): boolean {
|
|
8
|
+
return Boolean(isDisabled) || field.isLoading
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { omitPropWithWarning } from '@maratus-lib/props'
|
|
2
|
+
|
|
3
|
+
// HTML readonly does not apply to select, radio, or checkbox controls.
|
|
4
|
+
export function omitUnsupportedReadOnly<TProps extends object>(
|
|
5
|
+
control: string,
|
|
6
|
+
props: TProps,
|
|
7
|
+
): Omit<TProps, 'readOnly'> {
|
|
8
|
+
return omitPropWithWarning(
|
|
9
|
+
props,
|
|
10
|
+
'readOnly',
|
|
11
|
+
`${control}: HTML readonly does not apply to this control. Use FieldRoot isReadOnly instead.`,
|
|
12
|
+
)
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoiceControlBuilder,
|
|
3
|
+
ChoiceControlMode,
|
|
4
|
+
UseChoiceControlOptions,
|
|
5
|
+
UseChoiceControlResult,
|
|
6
|
+
} from './ChoiceControl.types'
|
|
7
|
+
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
8
|
+
import { getListboxControl } from './getListboxControl'
|
|
9
|
+
import { getRadioItemsControl } from './getRadioItemsControl'
|
|
10
|
+
import { getRadiosControl } from './getRadiosControl'
|
|
11
|
+
import { getSelectControl } from './getSelectControl'
|
|
12
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
13
|
+
|
|
14
|
+
export function useChoiceControl<TMode extends ChoiceControlMode>(
|
|
15
|
+
options: UseChoiceControlOptions<TMode>,
|
|
16
|
+
): UseChoiceControlResult<TMode> {
|
|
17
|
+
const { mode, ...fieldControlOptions } = omitUnsupportedReadOnly(
|
|
18
|
+
'ChoiceControl',
|
|
19
|
+
options,
|
|
20
|
+
)
|
|
21
|
+
const field = useFieldContext('ChoiceControl')
|
|
22
|
+
const fieldControl = useFieldControl(fieldControlOptions)
|
|
23
|
+
const buildChoiceControl: ChoiceControlBuilder<TMode> =
|
|
24
|
+
choiceControlBuilders[mode]
|
|
25
|
+
|
|
26
|
+
return buildChoiceControl({ field, fieldControl })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const choiceControlBuilders: {
|
|
30
|
+
[TMode in ChoiceControlMode]: ChoiceControlBuilder<TMode>
|
|
31
|
+
} = {
|
|
32
|
+
listbox: getListboxControl,
|
|
33
|
+
'radio-items': getRadioItemsControl,
|
|
34
|
+
radios: getRadiosControl,
|
|
35
|
+
select: getSelectControl,
|
|
36
|
+
}
|
package/meta.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maratus-registry/choice-control",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"files": [
|
|
6
|
+
"css-files",
|
|
7
|
+
"css-modules",
|
|
8
|
+
"tailwind-css",
|
|
9
|
+
"meta.json"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@maratus-lib/props": "0.1.0",
|
|
14
|
+
"@maratus-registry/field": "0.4.0"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"react": "^19.0.0",
|
|
18
|
+
"react-dom": "^19.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoiceControlMode,
|
|
3
|
+
ChoiceControlProps,
|
|
4
|
+
} from './ChoiceControl.types'
|
|
5
|
+
import { useChoiceControl } from './useChoiceControl'
|
|
6
|
+
|
|
7
|
+
export function ChoiceControl<TMode extends ChoiceControlMode>(
|
|
8
|
+
props: ChoiceControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
const { children, ...hookProps } = props
|
|
11
|
+
const control = useChoiceControl<TMode>(hookProps)
|
|
12
|
+
|
|
13
|
+
return children(control)
|
|
14
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FieldContextValue,
|
|
3
|
+
FieldControlRenderProps,
|
|
4
|
+
UseFieldControlOptions,
|
|
5
|
+
UseFieldControlResult,
|
|
6
|
+
ValidityHandlerProps,
|
|
7
|
+
} from '@maratus-component/field'
|
|
8
|
+
import type {
|
|
9
|
+
ComponentPropsWithRef,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
ReactNode,
|
|
12
|
+
Ref,
|
|
13
|
+
RefCallback,
|
|
14
|
+
} from 'react'
|
|
15
|
+
|
|
16
|
+
// Custom items can be any element and pass a consumer ref through.
|
|
17
|
+
// The getter turns it into a callback that fits any element.
|
|
18
|
+
type CustomItemProps = HTMLAttributes<HTMLElement> & {
|
|
19
|
+
ref?: Ref<HTMLElement>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type CustomItemRenderProps = HTMLAttributes<HTMLElement> & {
|
|
23
|
+
ref?: RefCallback<HTMLElement>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ChoiceControlMode = keyof ChoiceControlModes
|
|
27
|
+
|
|
28
|
+
type ChoiceControlModes = {
|
|
29
|
+
listbox: {
|
|
30
|
+
options: UseFieldControlOptions
|
|
31
|
+
result: UseListboxControlResult
|
|
32
|
+
}
|
|
33
|
+
'radio-items': {
|
|
34
|
+
options: UseFieldControlOptions
|
|
35
|
+
result: UseRadioItemsControlResult
|
|
36
|
+
}
|
|
37
|
+
radios: {
|
|
38
|
+
options: UseFieldControlOptions<HTMLInputElement>
|
|
39
|
+
result: UseRadiosControlResult
|
|
40
|
+
}
|
|
41
|
+
select: {
|
|
42
|
+
options: UseFieldControlOptions<HTMLSelectElement>
|
|
43
|
+
result: UseSelectControlResult
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type UseChoiceControlOptions<
|
|
48
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
49
|
+
> = ChoiceControlModes[TMode]['options'] & { mode: TMode }
|
|
50
|
+
|
|
51
|
+
export type UseChoiceControlResult<
|
|
52
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
53
|
+
> = ChoiceControlModes[TMode]['result']
|
|
54
|
+
|
|
55
|
+
export type ChoiceControlBuilder<TMode extends ChoiceControlMode> = (input: {
|
|
56
|
+
field: FieldContextValue
|
|
57
|
+
fieldControl: UseFieldControlResult
|
|
58
|
+
}) => UseChoiceControlResult<TMode>
|
|
59
|
+
|
|
60
|
+
export type RadiosControlRenderProps = FieldControlRenderProps & {
|
|
61
|
+
'aria-labelledby': string
|
|
62
|
+
role: 'radiogroup'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type RadioInputOptions = Omit<
|
|
66
|
+
ComponentPropsWithRef<'input'>,
|
|
67
|
+
| 'disabled'
|
|
68
|
+
| 'name'
|
|
69
|
+
| 'readOnly'
|
|
70
|
+
| 'required'
|
|
71
|
+
| 'type'
|
|
72
|
+
| 'value'
|
|
73
|
+
| keyof ValidityHandlerProps
|
|
74
|
+
> & {
|
|
75
|
+
isDisabled?: boolean
|
|
76
|
+
value: string
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type RadioInputRenderProps = ComponentPropsWithRef<'input'> &
|
|
80
|
+
ValidityHandlerProps & {
|
|
81
|
+
disabled?: true
|
|
82
|
+
name: string
|
|
83
|
+
required?: true
|
|
84
|
+
type: 'radio'
|
|
85
|
+
value: string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type UseRadiosControlResult = {
|
|
89
|
+
choiceControlProps: RadiosControlRenderProps
|
|
90
|
+
getChoiceInputProps: (options: RadioInputOptions) => RadioInputRenderProps
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type RadioItemsControlRenderProps = FieldControlRenderProps &
|
|
94
|
+
ValidityHandlerProps & {
|
|
95
|
+
'aria-labelledby': string
|
|
96
|
+
role: 'radiogroup'
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export type RadioItemOptions = Omit<
|
|
100
|
+
CustomItemProps,
|
|
101
|
+
'aria-checked' | 'aria-disabled' | 'aria-required' | 'role'
|
|
102
|
+
> & {
|
|
103
|
+
isDisabled?: boolean
|
|
104
|
+
isSelected: boolean
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export type RadioItemRenderProps = CustomItemRenderProps & {
|
|
108
|
+
'aria-checked': boolean
|
|
109
|
+
'aria-disabled'?: true
|
|
110
|
+
role: 'radio'
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type UseRadioItemsControlResult = {
|
|
114
|
+
choiceControlProps: RadioItemsControlRenderProps
|
|
115
|
+
getChoiceItemProps: (options: RadioItemOptions) => RadioItemRenderProps
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type SelectControlRenderProps = Omit<
|
|
119
|
+
FieldControlRenderProps,
|
|
120
|
+
'aria-disabled' | 'aria-required'
|
|
121
|
+
> &
|
|
122
|
+
ValidityHandlerProps & {
|
|
123
|
+
disabled?: true
|
|
124
|
+
name: string
|
|
125
|
+
required?: true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type UseSelectControlResult = {
|
|
129
|
+
choiceControlProps: SelectControlRenderProps
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type ListboxControlRenderProps = FieldControlRenderProps &
|
|
133
|
+
ValidityHandlerProps & {
|
|
134
|
+
'aria-labelledby': string
|
|
135
|
+
role: 'listbox'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type ListboxOptionOptions = Omit<
|
|
139
|
+
CustomItemProps,
|
|
140
|
+
'aria-disabled' | 'aria-selected' | 'role'
|
|
141
|
+
> & {
|
|
142
|
+
isDisabled?: boolean
|
|
143
|
+
isSelected: boolean
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type ListboxOptionRenderProps = CustomItemRenderProps & {
|
|
147
|
+
'aria-disabled'?: true
|
|
148
|
+
'aria-selected': boolean
|
|
149
|
+
role: 'option'
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export type UseListboxControlResult = {
|
|
153
|
+
choiceControlProps: ListboxControlRenderProps
|
|
154
|
+
getChoiceItemProps: (
|
|
155
|
+
options: ListboxOptionOptions,
|
|
156
|
+
) => ListboxOptionRenderProps
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export type ChoiceControlProps<
|
|
160
|
+
TMode extends ChoiceControlMode = ChoiceControlMode,
|
|
161
|
+
> = UseChoiceControlOptions<TMode> & {
|
|
162
|
+
children: (props: UseChoiceControlResult<TMode>) => ReactNode
|
|
163
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getListboxControl: ChoiceControlBuilder<'listbox'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choiceControlProps: {
|
|
10
|
+
...suppressActivation(fieldControl.fieldControlProps, field.isLoading),
|
|
11
|
+
...fieldControl.validityHandlerProps,
|
|
12
|
+
ref: mergeRefs(
|
|
13
|
+
fieldControl.fieldControlProps.ref,
|
|
14
|
+
fieldControl.validityHandlerProps.ref,
|
|
15
|
+
),
|
|
16
|
+
'aria-labelledby': field.labelId,
|
|
17
|
+
role: 'listbox',
|
|
18
|
+
},
|
|
19
|
+
getChoiceItemProps: ({ isDisabled, isSelected, ref, ...optionProps }) => ({
|
|
20
|
+
...suppressActivation(optionProps, isItemDisabled(field, isDisabled)),
|
|
21
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
22
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
23
|
+
'aria-selected': isSelected,
|
|
24
|
+
role: 'option',
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getRadioItemsControl: ChoiceControlBuilder<'radio-items'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choiceControlProps: {
|
|
10
|
+
...suppressActivation(fieldControl.fieldControlProps, field.isLoading),
|
|
11
|
+
...fieldControl.validityHandlerProps,
|
|
12
|
+
ref: mergeRefs(
|
|
13
|
+
fieldControl.fieldControlProps.ref,
|
|
14
|
+
fieldControl.validityHandlerProps.ref,
|
|
15
|
+
),
|
|
16
|
+
'aria-labelledby': field.labelId,
|
|
17
|
+
role: 'radiogroup',
|
|
18
|
+
},
|
|
19
|
+
getChoiceItemProps: ({ isDisabled, isSelected, ref, ...itemProps }) => ({
|
|
20
|
+
...suppressActivation(itemProps, isItemDisabled(field, isDisabled)),
|
|
21
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
22
|
+
'aria-checked': isSelected,
|
|
23
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
24
|
+
role: 'radio',
|
|
25
|
+
}),
|
|
26
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
5
|
+
|
|
6
|
+
export const getRadiosControl: ChoiceControlBuilder<'radios'> = ({
|
|
7
|
+
field,
|
|
8
|
+
fieldControl,
|
|
9
|
+
}) => ({
|
|
10
|
+
choiceControlProps: {
|
|
11
|
+
...fieldControl.fieldControlProps,
|
|
12
|
+
'aria-labelledby': field.labelId,
|
|
13
|
+
role: 'radiogroup',
|
|
14
|
+
},
|
|
15
|
+
getChoiceInputProps: ({ isDisabled, value, ...inputProps }) => {
|
|
16
|
+
return {
|
|
17
|
+
...omitUnsupportedReadOnly('ChoiceControl radio option', inputProps),
|
|
18
|
+
...fieldControl.validityHandlerProps,
|
|
19
|
+
ref: mergeRefs(inputProps.ref, fieldControl.validityHandlerProps.ref),
|
|
20
|
+
...(isItemDisabled(field, isDisabled) ? { disabled: true } : {}),
|
|
21
|
+
name: field.name,
|
|
22
|
+
...(field.isRequired ? { required: true } : {}),
|
|
23
|
+
type: 'radio',
|
|
24
|
+
value,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ChoiceControlBuilder } from './ChoiceControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
|
|
4
|
+
export const getSelectControl: ChoiceControlBuilder<'select'> = ({
|
|
5
|
+
field,
|
|
6
|
+
fieldControl,
|
|
7
|
+
}) => {
|
|
8
|
+
const selectFieldProps = { ...fieldControl.fieldControlProps }
|
|
9
|
+
|
|
10
|
+
delete selectFieldProps['aria-disabled']
|
|
11
|
+
delete selectFieldProps['aria-required']
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
choiceControlProps: {
|
|
15
|
+
...selectFieldProps,
|
|
16
|
+
...fieldControl.validityHandlerProps,
|
|
17
|
+
ref: mergeRefs(
|
|
18
|
+
fieldControl.fieldControlProps.ref,
|
|
19
|
+
fieldControl.validityHandlerProps.ref,
|
|
20
|
+
),
|
|
21
|
+
...(field.isLoading ? { disabled: true } : {}),
|
|
22
|
+
name: field.name,
|
|
23
|
+
...(field.isRequired ? { required: true } : {}),
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ChoiceControlMode,
|
|
3
|
+
ChoiceControlProps,
|
|
4
|
+
ListboxControlRenderProps,
|
|
5
|
+
ListboxOptionOptions,
|
|
6
|
+
ListboxOptionRenderProps,
|
|
7
|
+
RadioInputOptions,
|
|
8
|
+
RadioInputRenderProps,
|
|
9
|
+
RadioItemOptions,
|
|
10
|
+
RadioItemRenderProps,
|
|
11
|
+
RadioItemsControlRenderProps,
|
|
12
|
+
RadiosControlRenderProps,
|
|
13
|
+
SelectControlRenderProps,
|
|
14
|
+
UseChoiceControlOptions,
|
|
15
|
+
UseChoiceControlResult,
|
|
16
|
+
UseListboxControlResult,
|
|
17
|
+
UseRadioItemsControlResult,
|
|
18
|
+
UseRadiosControlResult,
|
|
19
|
+
UseSelectControlResult,
|
|
20
|
+
} from './ChoiceControl.types'
|
|
21
|
+
export { ChoiceControl } from './ChoiceControl'
|
|
22
|
+
export { useChoiceControl } from './useChoiceControl'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FieldContextValue } from '@maratus-component/field'
|
|
2
|
+
|
|
3
|
+
// Loading suppresses activation, so it disables every item.
|
|
4
|
+
export function isItemDisabled(
|
|
5
|
+
field: Pick<FieldContextValue, 'isLoading'>,
|
|
6
|
+
isDisabled: boolean | undefined,
|
|
7
|
+
): boolean {
|
|
8
|
+
return Boolean(isDisabled) || field.isLoading
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { omitPropWithWarning } from '@maratus-lib/props'
|
|
2
|
+
|
|
3
|
+
// HTML readonly does not apply to select, radio, or checkbox controls.
|
|
4
|
+
export function omitUnsupportedReadOnly<TProps extends object>(
|
|
5
|
+
control: string,
|
|
6
|
+
props: TProps,
|
|
7
|
+
): Omit<TProps, 'readOnly'> {
|
|
8
|
+
return omitPropWithWarning(
|
|
9
|
+
props,
|
|
10
|
+
'readOnly',
|
|
11
|
+
`${control}: HTML readonly does not apply to this control. Use FieldRoot isReadOnly instead.`,
|
|
12
|
+
)
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoiceControlBuilder,
|
|
3
|
+
ChoiceControlMode,
|
|
4
|
+
UseChoiceControlOptions,
|
|
5
|
+
UseChoiceControlResult,
|
|
6
|
+
} from './ChoiceControl.types'
|
|
7
|
+
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
8
|
+
import { getListboxControl } from './getListboxControl'
|
|
9
|
+
import { getRadioItemsControl } from './getRadioItemsControl'
|
|
10
|
+
import { getRadiosControl } from './getRadiosControl'
|
|
11
|
+
import { getSelectControl } from './getSelectControl'
|
|
12
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
13
|
+
|
|
14
|
+
export function useChoiceControl<TMode extends ChoiceControlMode>(
|
|
15
|
+
options: UseChoiceControlOptions<TMode>,
|
|
16
|
+
): UseChoiceControlResult<TMode> {
|
|
17
|
+
const { mode, ...fieldControlOptions } = omitUnsupportedReadOnly(
|
|
18
|
+
'ChoiceControl',
|
|
19
|
+
options,
|
|
20
|
+
)
|
|
21
|
+
const field = useFieldContext('ChoiceControl')
|
|
22
|
+
const fieldControl = useFieldControl(fieldControlOptions)
|
|
23
|
+
const buildChoiceControl: ChoiceControlBuilder<TMode> =
|
|
24
|
+
choiceControlBuilders[mode]
|
|
25
|
+
|
|
26
|
+
return buildChoiceControl({ field, fieldControl })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const choiceControlBuilders: {
|
|
30
|
+
[TMode in ChoiceControlMode]: ChoiceControlBuilder<TMode>
|
|
31
|
+
} = {
|
|
32
|
+
listbox: getListboxControl,
|
|
33
|
+
'radio-items': getRadioItemsControl,
|
|
34
|
+
radios: getRadiosControl,
|
|
35
|
+
select: getSelectControl,
|
|
36
|
+
}
|
package/README.md
DELETED