@maratus-registry/choices-control 0.0.0-stage → 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/ChoicesControl.tsx +14 -0
- package/css-files/ChoicesControl.types.ts +145 -0
- package/css-files/getCheckboxItemsControl.ts +28 -0
- package/css-files/getCheckboxesControl.ts +27 -0
- package/css-files/getGroupControlProps.ts +37 -0
- package/css-files/getMultiListboxControl.ts +27 -0
- package/css-files/index.ts +19 -0
- package/css-files/isItemDisabled.ts +9 -0
- package/css-files/omitUnsupportedReadOnly.ts +13 -0
- package/css-files/useChoicesControl.ts +34 -0
- package/css-modules/ChoicesControl.tsx +14 -0
- package/css-modules/ChoicesControl.types.ts +145 -0
- package/css-modules/getCheckboxItemsControl.ts +28 -0
- package/css-modules/getCheckboxesControl.ts +27 -0
- package/css-modules/getGroupControlProps.ts +37 -0
- package/css-modules/getMultiListboxControl.ts +27 -0
- package/css-modules/index.ts +19 -0
- package/css-modules/isItemDisabled.ts +9 -0
- package/css-modules/omitUnsupportedReadOnly.ts +13 -0
- package/css-modules/useChoicesControl.ts +34 -0
- package/meta.json +5 -0
- package/package.json +18 -4
- package/tailwind-css/ChoicesControl.tsx +14 -0
- package/tailwind-css/ChoicesControl.types.ts +145 -0
- package/tailwind-css/getCheckboxItemsControl.ts +28 -0
- package/tailwind-css/getCheckboxesControl.ts +27 -0
- package/tailwind-css/getGroupControlProps.ts +37 -0
- package/tailwind-css/getMultiListboxControl.ts +27 -0
- package/tailwind-css/index.ts +19 -0
- package/tailwind-css/isItemDisabled.ts +9 -0
- package/tailwind-css/omitUnsupportedReadOnly.ts +13 -0
- package/tailwind-css/useChoicesControl.ts +34 -0
- package/README.md +0 -3
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoicesControlMode,
|
|
3
|
+
ChoicesControlProps,
|
|
4
|
+
} from './ChoicesControl.types'
|
|
5
|
+
import { useChoicesControl } from './useChoicesControl'
|
|
6
|
+
|
|
7
|
+
export function ChoicesControl<TMode extends ChoicesControlMode>(
|
|
8
|
+
props: ChoicesControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
const { children, ...hookProps } = props
|
|
11
|
+
const control = useChoicesControl<TMode>(hookProps)
|
|
12
|
+
|
|
13
|
+
return children(control)
|
|
14
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
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 ChoicesControlMode = keyof ChoicesControlModes
|
|
27
|
+
|
|
28
|
+
type ChoicesControlModes = {
|
|
29
|
+
'checkbox-items': {
|
|
30
|
+
options: UseFieldControlOptions
|
|
31
|
+
result: UseCheckboxItemsControlResult
|
|
32
|
+
}
|
|
33
|
+
checkboxes: {
|
|
34
|
+
options: UseFieldControlOptions<HTMLInputElement>
|
|
35
|
+
result: UseCheckboxesControlResult
|
|
36
|
+
}
|
|
37
|
+
listbox: {
|
|
38
|
+
options: UseFieldControlOptions
|
|
39
|
+
result: UseMultiListboxControlResult
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type UseChoicesControlOptions<
|
|
44
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
45
|
+
> = ChoicesControlModes[TMode]['options'] & { mode: TMode }
|
|
46
|
+
|
|
47
|
+
export type UseChoicesControlResult<
|
|
48
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
49
|
+
> = ChoicesControlModes[TMode]['result']
|
|
50
|
+
|
|
51
|
+
export type ChoicesControlBuilder<TMode extends ChoicesControlMode> = (input: {
|
|
52
|
+
field: FieldContextValue
|
|
53
|
+
fieldControl: UseFieldControlResult
|
|
54
|
+
}) => UseChoicesControlResult<TMode>
|
|
55
|
+
|
|
56
|
+
export type GroupControlRenderProps = Omit<
|
|
57
|
+
FieldControlRenderProps,
|
|
58
|
+
'aria-errormessage' | 'aria-invalid' | 'aria-readonly' | 'aria-required'
|
|
59
|
+
> & {
|
|
60
|
+
'aria-labelledby': string
|
|
61
|
+
role: 'group'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type CheckboxInputOptions = Omit<
|
|
65
|
+
ComponentPropsWithRef<'input'>,
|
|
66
|
+
| 'aria-readonly'
|
|
67
|
+
| 'disabled'
|
|
68
|
+
| 'readOnly'
|
|
69
|
+
| 'type'
|
|
70
|
+
| 'value'
|
|
71
|
+
| keyof ValidityHandlerProps
|
|
72
|
+
> & {
|
|
73
|
+
isDisabled?: boolean
|
|
74
|
+
value: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type CheckboxInputRenderProps = ComponentPropsWithRef<'input'> &
|
|
78
|
+
ValidityHandlerProps & {
|
|
79
|
+
'aria-readonly'?: true
|
|
80
|
+
disabled?: true
|
|
81
|
+
name: string
|
|
82
|
+
type: 'checkbox'
|
|
83
|
+
value: string
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type UseCheckboxesControlResult = {
|
|
87
|
+
choicesControlProps: GroupControlRenderProps
|
|
88
|
+
getChoicesInputProps: (
|
|
89
|
+
options: CheckboxInputOptions,
|
|
90
|
+
) => CheckboxInputRenderProps
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type CheckboxItemOptions = Omit<
|
|
94
|
+
CustomItemProps,
|
|
95
|
+
'aria-checked' | 'aria-disabled' | 'aria-readonly' | 'aria-required' | 'role'
|
|
96
|
+
> & {
|
|
97
|
+
isDisabled?: boolean
|
|
98
|
+
isSelected: boolean
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type CheckboxItemRenderProps = CustomItemRenderProps & {
|
|
102
|
+
'aria-checked': boolean
|
|
103
|
+
'aria-disabled'?: true
|
|
104
|
+
'aria-readonly'?: true
|
|
105
|
+
role: 'checkbox'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export type UseCheckboxItemsControlResult = {
|
|
109
|
+
choicesControlProps: GroupControlRenderProps & ValidityHandlerProps
|
|
110
|
+
getChoicesItemProps: (options: CheckboxItemOptions) => CheckboxItemRenderProps
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type MultiListboxControlRenderProps = FieldControlRenderProps &
|
|
114
|
+
ValidityHandlerProps & {
|
|
115
|
+
'aria-labelledby': string
|
|
116
|
+
'aria-multiselectable': true
|
|
117
|
+
role: 'listbox'
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type ListboxOptionOptions = Omit<
|
|
121
|
+
CustomItemProps,
|
|
122
|
+
'aria-disabled' | 'aria-selected' | 'role'
|
|
123
|
+
> & {
|
|
124
|
+
isDisabled?: boolean
|
|
125
|
+
isSelected: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type ListboxOptionRenderProps = CustomItemRenderProps & {
|
|
129
|
+
'aria-disabled'?: true
|
|
130
|
+
'aria-selected': boolean
|
|
131
|
+
role: 'option'
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export type UseMultiListboxControlResult = {
|
|
135
|
+
choicesControlProps: MultiListboxControlRenderProps
|
|
136
|
+
getChoicesItemProps: (
|
|
137
|
+
options: ListboxOptionOptions,
|
|
138
|
+
) => ListboxOptionRenderProps
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type ChoicesControlProps<
|
|
142
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
143
|
+
> = UseChoicesControlOptions<TMode> & {
|
|
144
|
+
children: (props: UseChoicesControlResult<TMode>) => ReactNode
|
|
145
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { getGroupControlProps } from './getGroupControlProps'
|
|
4
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
5
|
+
|
|
6
|
+
export const getCheckboxItemsControl: ChoicesControlBuilder<
|
|
7
|
+
'checkbox-items'
|
|
8
|
+
> = ({ field, fieldControl }) => ({
|
|
9
|
+
choicesControlProps: {
|
|
10
|
+
...suppressActivation(
|
|
11
|
+
getGroupControlProps(field, fieldControl.fieldControlProps),
|
|
12
|
+
field.isLoading,
|
|
13
|
+
),
|
|
14
|
+
...fieldControl.validityHandlerProps,
|
|
15
|
+
ref: mergeRefs(
|
|
16
|
+
fieldControl.fieldControlProps.ref,
|
|
17
|
+
fieldControl.validityHandlerProps.ref,
|
|
18
|
+
),
|
|
19
|
+
},
|
|
20
|
+
getChoicesItemProps: ({ isDisabled, isSelected, ref, ...itemProps }) => ({
|
|
21
|
+
...suppressActivation(itemProps, isItemDisabled(field, isDisabled)),
|
|
22
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
23
|
+
'aria-checked': isSelected,
|
|
24
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
25
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
26
|
+
role: 'checkbox',
|
|
27
|
+
}),
|
|
28
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
import { getGroupControlProps } from './getGroupControlProps'
|
|
4
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
5
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
6
|
+
|
|
7
|
+
export const getCheckboxesControl: ChoicesControlBuilder<'checkboxes'> = ({
|
|
8
|
+
field,
|
|
9
|
+
fieldControl,
|
|
10
|
+
}) => ({
|
|
11
|
+
choicesControlProps: getGroupControlProps(
|
|
12
|
+
field,
|
|
13
|
+
fieldControl.fieldControlProps,
|
|
14
|
+
),
|
|
15
|
+
getChoicesInputProps: ({ isDisabled, name, value, ...inputProps }) => {
|
|
16
|
+
return {
|
|
17
|
+
...omitUnsupportedReadOnly('ChoicesControl checkbox option', inputProps),
|
|
18
|
+
...fieldControl.validityHandlerProps,
|
|
19
|
+
ref: mergeRefs(inputProps.ref, fieldControl.validityHandlerProps.ref),
|
|
20
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
21
|
+
...(isItemDisabled(field, isDisabled) ? { disabled: true } : {}),
|
|
22
|
+
name: name ?? field.name,
|
|
23
|
+
type: 'checkbox',
|
|
24
|
+
value,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { GroupControlRenderProps } from './ChoicesControl.types'
|
|
2
|
+
import type {
|
|
3
|
+
FieldContextValue,
|
|
4
|
+
FieldControlRenderProps,
|
|
5
|
+
} from '@maratus-component/field'
|
|
6
|
+
|
|
7
|
+
// ARIA 1.2 deprecates aria-invalid and aria-errormessage on group, and axe
|
|
8
|
+
// rejects aria-required and aria-readonly there, so the error id moves into
|
|
9
|
+
// aria-describedby and the rest is dropped.
|
|
10
|
+
export function getGroupControlProps(
|
|
11
|
+
field: FieldContextValue,
|
|
12
|
+
fieldControlProps: FieldControlRenderProps,
|
|
13
|
+
): GroupControlRenderProps {
|
|
14
|
+
const groupProps = { ...fieldControlProps }
|
|
15
|
+
|
|
16
|
+
delete groupProps['aria-errormessage']
|
|
17
|
+
delete groupProps['aria-invalid']
|
|
18
|
+
delete groupProps['aria-readonly']
|
|
19
|
+
delete groupProps['aria-required']
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
...groupProps,
|
|
23
|
+
'aria-describedby': getGroupDescribedBy(field, fieldControlProps),
|
|
24
|
+
'aria-labelledby': field.labelId,
|
|
25
|
+
role: 'group',
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getGroupDescribedBy(
|
|
30
|
+
field: FieldContextValue,
|
|
31
|
+
fieldControlProps: FieldControlRenderProps,
|
|
32
|
+
): string | undefined {
|
|
33
|
+
const errorId = field.visibleErrors.length > 0 ? field.errorId : undefined
|
|
34
|
+
const ids = [fieldControlProps['aria-describedby'], errorId].filter(Boolean)
|
|
35
|
+
|
|
36
|
+
return ids.length > 0 ? ids.join(' ') : undefined
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getMultiListboxControl: ChoicesControlBuilder<'listbox'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choicesControlProps: {
|
|
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
|
+
'aria-multiselectable': true,
|
|
18
|
+
role: 'listbox',
|
|
19
|
+
},
|
|
20
|
+
getChoicesItemProps: ({ isDisabled, isSelected, ref, ...optionProps }) => ({
|
|
21
|
+
...suppressActivation(optionProps, isItemDisabled(field, isDisabled)),
|
|
22
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
23
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
24
|
+
'aria-selected': isSelected,
|
|
25
|
+
role: 'option',
|
|
26
|
+
}),
|
|
27
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
CheckboxInputOptions,
|
|
3
|
+
CheckboxInputRenderProps,
|
|
4
|
+
CheckboxItemOptions,
|
|
5
|
+
CheckboxItemRenderProps,
|
|
6
|
+
ChoicesControlMode,
|
|
7
|
+
ChoicesControlProps,
|
|
8
|
+
GroupControlRenderProps,
|
|
9
|
+
ListboxOptionOptions,
|
|
10
|
+
ListboxOptionRenderProps,
|
|
11
|
+
MultiListboxControlRenderProps,
|
|
12
|
+
UseCheckboxesControlResult,
|
|
13
|
+
UseCheckboxItemsControlResult,
|
|
14
|
+
UseChoicesControlOptions,
|
|
15
|
+
UseChoicesControlResult,
|
|
16
|
+
UseMultiListboxControlResult,
|
|
17
|
+
} from './ChoicesControl.types'
|
|
18
|
+
export { ChoicesControl } from './ChoicesControl'
|
|
19
|
+
export { useChoicesControl } from './useChoicesControl'
|
|
@@ -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,34 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoicesControlBuilder,
|
|
3
|
+
ChoicesControlMode,
|
|
4
|
+
UseChoicesControlOptions,
|
|
5
|
+
UseChoicesControlResult,
|
|
6
|
+
} from './ChoicesControl.types'
|
|
7
|
+
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
8
|
+
import { getCheckboxesControl } from './getCheckboxesControl'
|
|
9
|
+
import { getCheckboxItemsControl } from './getCheckboxItemsControl'
|
|
10
|
+
import { getMultiListboxControl } from './getMultiListboxControl'
|
|
11
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
12
|
+
|
|
13
|
+
export function useChoicesControl<TMode extends ChoicesControlMode>(
|
|
14
|
+
options: UseChoicesControlOptions<TMode>,
|
|
15
|
+
): UseChoicesControlResult<TMode> {
|
|
16
|
+
const { mode, ...fieldControlOptions } = omitUnsupportedReadOnly(
|
|
17
|
+
'ChoicesControl',
|
|
18
|
+
options,
|
|
19
|
+
)
|
|
20
|
+
const field = useFieldContext('ChoicesControl')
|
|
21
|
+
const fieldControl = useFieldControl(fieldControlOptions)
|
|
22
|
+
const buildChoicesControl: ChoicesControlBuilder<TMode> =
|
|
23
|
+
choicesControlBuilders[mode]
|
|
24
|
+
|
|
25
|
+
return buildChoicesControl({ field, fieldControl })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const choicesControlBuilders: {
|
|
29
|
+
[TMode in ChoicesControlMode]: ChoicesControlBuilder<TMode>
|
|
30
|
+
} = {
|
|
31
|
+
'checkbox-items': getCheckboxItemsControl,
|
|
32
|
+
checkboxes: getCheckboxesControl,
|
|
33
|
+
listbox: getMultiListboxControl,
|
|
34
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoicesControlMode,
|
|
3
|
+
ChoicesControlProps,
|
|
4
|
+
} from './ChoicesControl.types'
|
|
5
|
+
import { useChoicesControl } from './useChoicesControl'
|
|
6
|
+
|
|
7
|
+
export function ChoicesControl<TMode extends ChoicesControlMode>(
|
|
8
|
+
props: ChoicesControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
const { children, ...hookProps } = props
|
|
11
|
+
const control = useChoicesControl<TMode>(hookProps)
|
|
12
|
+
|
|
13
|
+
return children(control)
|
|
14
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
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 ChoicesControlMode = keyof ChoicesControlModes
|
|
27
|
+
|
|
28
|
+
type ChoicesControlModes = {
|
|
29
|
+
'checkbox-items': {
|
|
30
|
+
options: UseFieldControlOptions
|
|
31
|
+
result: UseCheckboxItemsControlResult
|
|
32
|
+
}
|
|
33
|
+
checkboxes: {
|
|
34
|
+
options: UseFieldControlOptions<HTMLInputElement>
|
|
35
|
+
result: UseCheckboxesControlResult
|
|
36
|
+
}
|
|
37
|
+
listbox: {
|
|
38
|
+
options: UseFieldControlOptions
|
|
39
|
+
result: UseMultiListboxControlResult
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type UseChoicesControlOptions<
|
|
44
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
45
|
+
> = ChoicesControlModes[TMode]['options'] & { mode: TMode }
|
|
46
|
+
|
|
47
|
+
export type UseChoicesControlResult<
|
|
48
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
49
|
+
> = ChoicesControlModes[TMode]['result']
|
|
50
|
+
|
|
51
|
+
export type ChoicesControlBuilder<TMode extends ChoicesControlMode> = (input: {
|
|
52
|
+
field: FieldContextValue
|
|
53
|
+
fieldControl: UseFieldControlResult
|
|
54
|
+
}) => UseChoicesControlResult<TMode>
|
|
55
|
+
|
|
56
|
+
export type GroupControlRenderProps = Omit<
|
|
57
|
+
FieldControlRenderProps,
|
|
58
|
+
'aria-errormessage' | 'aria-invalid' | 'aria-readonly' | 'aria-required'
|
|
59
|
+
> & {
|
|
60
|
+
'aria-labelledby': string
|
|
61
|
+
role: 'group'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type CheckboxInputOptions = Omit<
|
|
65
|
+
ComponentPropsWithRef<'input'>,
|
|
66
|
+
| 'aria-readonly'
|
|
67
|
+
| 'disabled'
|
|
68
|
+
| 'readOnly'
|
|
69
|
+
| 'type'
|
|
70
|
+
| 'value'
|
|
71
|
+
| keyof ValidityHandlerProps
|
|
72
|
+
> & {
|
|
73
|
+
isDisabled?: boolean
|
|
74
|
+
value: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type CheckboxInputRenderProps = ComponentPropsWithRef<'input'> &
|
|
78
|
+
ValidityHandlerProps & {
|
|
79
|
+
'aria-readonly'?: true
|
|
80
|
+
disabled?: true
|
|
81
|
+
name: string
|
|
82
|
+
type: 'checkbox'
|
|
83
|
+
value: string
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type UseCheckboxesControlResult = {
|
|
87
|
+
choicesControlProps: GroupControlRenderProps
|
|
88
|
+
getChoicesInputProps: (
|
|
89
|
+
options: CheckboxInputOptions,
|
|
90
|
+
) => CheckboxInputRenderProps
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type CheckboxItemOptions = Omit<
|
|
94
|
+
CustomItemProps,
|
|
95
|
+
'aria-checked' | 'aria-disabled' | 'aria-readonly' | 'aria-required' | 'role'
|
|
96
|
+
> & {
|
|
97
|
+
isDisabled?: boolean
|
|
98
|
+
isSelected: boolean
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type CheckboxItemRenderProps = CustomItemRenderProps & {
|
|
102
|
+
'aria-checked': boolean
|
|
103
|
+
'aria-disabled'?: true
|
|
104
|
+
'aria-readonly'?: true
|
|
105
|
+
role: 'checkbox'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export type UseCheckboxItemsControlResult = {
|
|
109
|
+
choicesControlProps: GroupControlRenderProps & ValidityHandlerProps
|
|
110
|
+
getChoicesItemProps: (options: CheckboxItemOptions) => CheckboxItemRenderProps
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type MultiListboxControlRenderProps = FieldControlRenderProps &
|
|
114
|
+
ValidityHandlerProps & {
|
|
115
|
+
'aria-labelledby': string
|
|
116
|
+
'aria-multiselectable': true
|
|
117
|
+
role: 'listbox'
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type ListboxOptionOptions = Omit<
|
|
121
|
+
CustomItemProps,
|
|
122
|
+
'aria-disabled' | 'aria-selected' | 'role'
|
|
123
|
+
> & {
|
|
124
|
+
isDisabled?: boolean
|
|
125
|
+
isSelected: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type ListboxOptionRenderProps = CustomItemRenderProps & {
|
|
129
|
+
'aria-disabled'?: true
|
|
130
|
+
'aria-selected': boolean
|
|
131
|
+
role: 'option'
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export type UseMultiListboxControlResult = {
|
|
135
|
+
choicesControlProps: MultiListboxControlRenderProps
|
|
136
|
+
getChoicesItemProps: (
|
|
137
|
+
options: ListboxOptionOptions,
|
|
138
|
+
) => ListboxOptionRenderProps
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type ChoicesControlProps<
|
|
142
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
143
|
+
> = UseChoicesControlOptions<TMode> & {
|
|
144
|
+
children: (props: UseChoicesControlResult<TMode>) => ReactNode
|
|
145
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { getGroupControlProps } from './getGroupControlProps'
|
|
4
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
5
|
+
|
|
6
|
+
export const getCheckboxItemsControl: ChoicesControlBuilder<
|
|
7
|
+
'checkbox-items'
|
|
8
|
+
> = ({ field, fieldControl }) => ({
|
|
9
|
+
choicesControlProps: {
|
|
10
|
+
...suppressActivation(
|
|
11
|
+
getGroupControlProps(field, fieldControl.fieldControlProps),
|
|
12
|
+
field.isLoading,
|
|
13
|
+
),
|
|
14
|
+
...fieldControl.validityHandlerProps,
|
|
15
|
+
ref: mergeRefs(
|
|
16
|
+
fieldControl.fieldControlProps.ref,
|
|
17
|
+
fieldControl.validityHandlerProps.ref,
|
|
18
|
+
),
|
|
19
|
+
},
|
|
20
|
+
getChoicesItemProps: ({ isDisabled, isSelected, ref, ...itemProps }) => ({
|
|
21
|
+
...suppressActivation(itemProps, isItemDisabled(field, isDisabled)),
|
|
22
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
23
|
+
'aria-checked': isSelected,
|
|
24
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
25
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
26
|
+
role: 'checkbox',
|
|
27
|
+
}),
|
|
28
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
import { getGroupControlProps } from './getGroupControlProps'
|
|
4
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
5
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
6
|
+
|
|
7
|
+
export const getCheckboxesControl: ChoicesControlBuilder<'checkboxes'> = ({
|
|
8
|
+
field,
|
|
9
|
+
fieldControl,
|
|
10
|
+
}) => ({
|
|
11
|
+
choicesControlProps: getGroupControlProps(
|
|
12
|
+
field,
|
|
13
|
+
fieldControl.fieldControlProps,
|
|
14
|
+
),
|
|
15
|
+
getChoicesInputProps: ({ isDisabled, name, value, ...inputProps }) => {
|
|
16
|
+
return {
|
|
17
|
+
...omitUnsupportedReadOnly('ChoicesControl checkbox option', inputProps),
|
|
18
|
+
...fieldControl.validityHandlerProps,
|
|
19
|
+
ref: mergeRefs(inputProps.ref, fieldControl.validityHandlerProps.ref),
|
|
20
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
21
|
+
...(isItemDisabled(field, isDisabled) ? { disabled: true } : {}),
|
|
22
|
+
name: name ?? field.name,
|
|
23
|
+
type: 'checkbox',
|
|
24
|
+
value,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { GroupControlRenderProps } from './ChoicesControl.types'
|
|
2
|
+
import type {
|
|
3
|
+
FieldContextValue,
|
|
4
|
+
FieldControlRenderProps,
|
|
5
|
+
} from '@maratus-component/field'
|
|
6
|
+
|
|
7
|
+
// ARIA 1.2 deprecates aria-invalid and aria-errormessage on group, and axe
|
|
8
|
+
// rejects aria-required and aria-readonly there, so the error id moves into
|
|
9
|
+
// aria-describedby and the rest is dropped.
|
|
10
|
+
export function getGroupControlProps(
|
|
11
|
+
field: FieldContextValue,
|
|
12
|
+
fieldControlProps: FieldControlRenderProps,
|
|
13
|
+
): GroupControlRenderProps {
|
|
14
|
+
const groupProps = { ...fieldControlProps }
|
|
15
|
+
|
|
16
|
+
delete groupProps['aria-errormessage']
|
|
17
|
+
delete groupProps['aria-invalid']
|
|
18
|
+
delete groupProps['aria-readonly']
|
|
19
|
+
delete groupProps['aria-required']
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
...groupProps,
|
|
23
|
+
'aria-describedby': getGroupDescribedBy(field, fieldControlProps),
|
|
24
|
+
'aria-labelledby': field.labelId,
|
|
25
|
+
role: 'group',
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getGroupDescribedBy(
|
|
30
|
+
field: FieldContextValue,
|
|
31
|
+
fieldControlProps: FieldControlRenderProps,
|
|
32
|
+
): string | undefined {
|
|
33
|
+
const errorId = field.visibleErrors.length > 0 ? field.errorId : undefined
|
|
34
|
+
const ids = [fieldControlProps['aria-describedby'], errorId].filter(Boolean)
|
|
35
|
+
|
|
36
|
+
return ids.length > 0 ? ids.join(' ') : undefined
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getMultiListboxControl: ChoicesControlBuilder<'listbox'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choicesControlProps: {
|
|
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
|
+
'aria-multiselectable': true,
|
|
18
|
+
role: 'listbox',
|
|
19
|
+
},
|
|
20
|
+
getChoicesItemProps: ({ isDisabled, isSelected, ref, ...optionProps }) => ({
|
|
21
|
+
...suppressActivation(optionProps, isItemDisabled(field, isDisabled)),
|
|
22
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
23
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
24
|
+
'aria-selected': isSelected,
|
|
25
|
+
role: 'option',
|
|
26
|
+
}),
|
|
27
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
CheckboxInputOptions,
|
|
3
|
+
CheckboxInputRenderProps,
|
|
4
|
+
CheckboxItemOptions,
|
|
5
|
+
CheckboxItemRenderProps,
|
|
6
|
+
ChoicesControlMode,
|
|
7
|
+
ChoicesControlProps,
|
|
8
|
+
GroupControlRenderProps,
|
|
9
|
+
ListboxOptionOptions,
|
|
10
|
+
ListboxOptionRenderProps,
|
|
11
|
+
MultiListboxControlRenderProps,
|
|
12
|
+
UseCheckboxesControlResult,
|
|
13
|
+
UseCheckboxItemsControlResult,
|
|
14
|
+
UseChoicesControlOptions,
|
|
15
|
+
UseChoicesControlResult,
|
|
16
|
+
UseMultiListboxControlResult,
|
|
17
|
+
} from './ChoicesControl.types'
|
|
18
|
+
export { ChoicesControl } from './ChoicesControl'
|
|
19
|
+
export { useChoicesControl } from './useChoicesControl'
|
|
@@ -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,34 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoicesControlBuilder,
|
|
3
|
+
ChoicesControlMode,
|
|
4
|
+
UseChoicesControlOptions,
|
|
5
|
+
UseChoicesControlResult,
|
|
6
|
+
} from './ChoicesControl.types'
|
|
7
|
+
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
8
|
+
import { getCheckboxesControl } from './getCheckboxesControl'
|
|
9
|
+
import { getCheckboxItemsControl } from './getCheckboxItemsControl'
|
|
10
|
+
import { getMultiListboxControl } from './getMultiListboxControl'
|
|
11
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
12
|
+
|
|
13
|
+
export function useChoicesControl<TMode extends ChoicesControlMode>(
|
|
14
|
+
options: UseChoicesControlOptions<TMode>,
|
|
15
|
+
): UseChoicesControlResult<TMode> {
|
|
16
|
+
const { mode, ...fieldControlOptions } = omitUnsupportedReadOnly(
|
|
17
|
+
'ChoicesControl',
|
|
18
|
+
options,
|
|
19
|
+
)
|
|
20
|
+
const field = useFieldContext('ChoicesControl')
|
|
21
|
+
const fieldControl = useFieldControl(fieldControlOptions)
|
|
22
|
+
const buildChoicesControl: ChoicesControlBuilder<TMode> =
|
|
23
|
+
choicesControlBuilders[mode]
|
|
24
|
+
|
|
25
|
+
return buildChoicesControl({ field, fieldControl })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const choicesControlBuilders: {
|
|
29
|
+
[TMode in ChoicesControlMode]: ChoicesControlBuilder<TMode>
|
|
30
|
+
} = {
|
|
31
|
+
'checkbox-items': getCheckboxItemsControl,
|
|
32
|
+
checkboxes: getCheckboxesControl,
|
|
33
|
+
listbox: getMultiListboxControl,
|
|
34
|
+
}
|
package/meta.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maratus-registry/choices-control",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
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.3.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
|
+
ChoicesControlMode,
|
|
3
|
+
ChoicesControlProps,
|
|
4
|
+
} from './ChoicesControl.types'
|
|
5
|
+
import { useChoicesControl } from './useChoicesControl'
|
|
6
|
+
|
|
7
|
+
export function ChoicesControl<TMode extends ChoicesControlMode>(
|
|
8
|
+
props: ChoicesControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
const { children, ...hookProps } = props
|
|
11
|
+
const control = useChoicesControl<TMode>(hookProps)
|
|
12
|
+
|
|
13
|
+
return children(control)
|
|
14
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
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 ChoicesControlMode = keyof ChoicesControlModes
|
|
27
|
+
|
|
28
|
+
type ChoicesControlModes = {
|
|
29
|
+
'checkbox-items': {
|
|
30
|
+
options: UseFieldControlOptions
|
|
31
|
+
result: UseCheckboxItemsControlResult
|
|
32
|
+
}
|
|
33
|
+
checkboxes: {
|
|
34
|
+
options: UseFieldControlOptions<HTMLInputElement>
|
|
35
|
+
result: UseCheckboxesControlResult
|
|
36
|
+
}
|
|
37
|
+
listbox: {
|
|
38
|
+
options: UseFieldControlOptions
|
|
39
|
+
result: UseMultiListboxControlResult
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type UseChoicesControlOptions<
|
|
44
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
45
|
+
> = ChoicesControlModes[TMode]['options'] & { mode: TMode }
|
|
46
|
+
|
|
47
|
+
export type UseChoicesControlResult<
|
|
48
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
49
|
+
> = ChoicesControlModes[TMode]['result']
|
|
50
|
+
|
|
51
|
+
export type ChoicesControlBuilder<TMode extends ChoicesControlMode> = (input: {
|
|
52
|
+
field: FieldContextValue
|
|
53
|
+
fieldControl: UseFieldControlResult
|
|
54
|
+
}) => UseChoicesControlResult<TMode>
|
|
55
|
+
|
|
56
|
+
export type GroupControlRenderProps = Omit<
|
|
57
|
+
FieldControlRenderProps,
|
|
58
|
+
'aria-errormessage' | 'aria-invalid' | 'aria-readonly' | 'aria-required'
|
|
59
|
+
> & {
|
|
60
|
+
'aria-labelledby': string
|
|
61
|
+
role: 'group'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type CheckboxInputOptions = Omit<
|
|
65
|
+
ComponentPropsWithRef<'input'>,
|
|
66
|
+
| 'aria-readonly'
|
|
67
|
+
| 'disabled'
|
|
68
|
+
| 'readOnly'
|
|
69
|
+
| 'type'
|
|
70
|
+
| 'value'
|
|
71
|
+
| keyof ValidityHandlerProps
|
|
72
|
+
> & {
|
|
73
|
+
isDisabled?: boolean
|
|
74
|
+
value: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type CheckboxInputRenderProps = ComponentPropsWithRef<'input'> &
|
|
78
|
+
ValidityHandlerProps & {
|
|
79
|
+
'aria-readonly'?: true
|
|
80
|
+
disabled?: true
|
|
81
|
+
name: string
|
|
82
|
+
type: 'checkbox'
|
|
83
|
+
value: string
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type UseCheckboxesControlResult = {
|
|
87
|
+
choicesControlProps: GroupControlRenderProps
|
|
88
|
+
getChoicesInputProps: (
|
|
89
|
+
options: CheckboxInputOptions,
|
|
90
|
+
) => CheckboxInputRenderProps
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type CheckboxItemOptions = Omit<
|
|
94
|
+
CustomItemProps,
|
|
95
|
+
'aria-checked' | 'aria-disabled' | 'aria-readonly' | 'aria-required' | 'role'
|
|
96
|
+
> & {
|
|
97
|
+
isDisabled?: boolean
|
|
98
|
+
isSelected: boolean
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type CheckboxItemRenderProps = CustomItemRenderProps & {
|
|
102
|
+
'aria-checked': boolean
|
|
103
|
+
'aria-disabled'?: true
|
|
104
|
+
'aria-readonly'?: true
|
|
105
|
+
role: 'checkbox'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export type UseCheckboxItemsControlResult = {
|
|
109
|
+
choicesControlProps: GroupControlRenderProps & ValidityHandlerProps
|
|
110
|
+
getChoicesItemProps: (options: CheckboxItemOptions) => CheckboxItemRenderProps
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type MultiListboxControlRenderProps = FieldControlRenderProps &
|
|
114
|
+
ValidityHandlerProps & {
|
|
115
|
+
'aria-labelledby': string
|
|
116
|
+
'aria-multiselectable': true
|
|
117
|
+
role: 'listbox'
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type ListboxOptionOptions = Omit<
|
|
121
|
+
CustomItemProps,
|
|
122
|
+
'aria-disabled' | 'aria-selected' | 'role'
|
|
123
|
+
> & {
|
|
124
|
+
isDisabled?: boolean
|
|
125
|
+
isSelected: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type ListboxOptionRenderProps = CustomItemRenderProps & {
|
|
129
|
+
'aria-disabled'?: true
|
|
130
|
+
'aria-selected': boolean
|
|
131
|
+
role: 'option'
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export type UseMultiListboxControlResult = {
|
|
135
|
+
choicesControlProps: MultiListboxControlRenderProps
|
|
136
|
+
getChoicesItemProps: (
|
|
137
|
+
options: ListboxOptionOptions,
|
|
138
|
+
) => ListboxOptionRenderProps
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type ChoicesControlProps<
|
|
142
|
+
TMode extends ChoicesControlMode = ChoicesControlMode,
|
|
143
|
+
> = UseChoicesControlOptions<TMode> & {
|
|
144
|
+
children: (props: UseChoicesControlResult<TMode>) => ReactNode
|
|
145
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { getGroupControlProps } from './getGroupControlProps'
|
|
4
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
5
|
+
|
|
6
|
+
export const getCheckboxItemsControl: ChoicesControlBuilder<
|
|
7
|
+
'checkbox-items'
|
|
8
|
+
> = ({ field, fieldControl }) => ({
|
|
9
|
+
choicesControlProps: {
|
|
10
|
+
...suppressActivation(
|
|
11
|
+
getGroupControlProps(field, fieldControl.fieldControlProps),
|
|
12
|
+
field.isLoading,
|
|
13
|
+
),
|
|
14
|
+
...fieldControl.validityHandlerProps,
|
|
15
|
+
ref: mergeRefs(
|
|
16
|
+
fieldControl.fieldControlProps.ref,
|
|
17
|
+
fieldControl.validityHandlerProps.ref,
|
|
18
|
+
),
|
|
19
|
+
},
|
|
20
|
+
getChoicesItemProps: ({ isDisabled, isSelected, ref, ...itemProps }) => ({
|
|
21
|
+
...suppressActivation(itemProps, isItemDisabled(field, isDisabled)),
|
|
22
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
23
|
+
'aria-checked': isSelected,
|
|
24
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
25
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
26
|
+
role: 'checkbox',
|
|
27
|
+
}),
|
|
28
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs } from '@maratus-lib/props'
|
|
3
|
+
import { getGroupControlProps } from './getGroupControlProps'
|
|
4
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
5
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
6
|
+
|
|
7
|
+
export const getCheckboxesControl: ChoicesControlBuilder<'checkboxes'> = ({
|
|
8
|
+
field,
|
|
9
|
+
fieldControl,
|
|
10
|
+
}) => ({
|
|
11
|
+
choicesControlProps: getGroupControlProps(
|
|
12
|
+
field,
|
|
13
|
+
fieldControl.fieldControlProps,
|
|
14
|
+
),
|
|
15
|
+
getChoicesInputProps: ({ isDisabled, name, value, ...inputProps }) => {
|
|
16
|
+
return {
|
|
17
|
+
...omitUnsupportedReadOnly('ChoicesControl checkbox option', inputProps),
|
|
18
|
+
...fieldControl.validityHandlerProps,
|
|
19
|
+
ref: mergeRefs(inputProps.ref, fieldControl.validityHandlerProps.ref),
|
|
20
|
+
...(field.isReadOnly ? { 'aria-readonly': true } : {}),
|
|
21
|
+
...(isItemDisabled(field, isDisabled) ? { disabled: true } : {}),
|
|
22
|
+
name: name ?? field.name,
|
|
23
|
+
type: 'checkbox',
|
|
24
|
+
value,
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { GroupControlRenderProps } from './ChoicesControl.types'
|
|
2
|
+
import type {
|
|
3
|
+
FieldContextValue,
|
|
4
|
+
FieldControlRenderProps,
|
|
5
|
+
} from '@maratus-component/field'
|
|
6
|
+
|
|
7
|
+
// ARIA 1.2 deprecates aria-invalid and aria-errormessage on group, and axe
|
|
8
|
+
// rejects aria-required and aria-readonly there, so the error id moves into
|
|
9
|
+
// aria-describedby and the rest is dropped.
|
|
10
|
+
export function getGroupControlProps(
|
|
11
|
+
field: FieldContextValue,
|
|
12
|
+
fieldControlProps: FieldControlRenderProps,
|
|
13
|
+
): GroupControlRenderProps {
|
|
14
|
+
const groupProps = { ...fieldControlProps }
|
|
15
|
+
|
|
16
|
+
delete groupProps['aria-errormessage']
|
|
17
|
+
delete groupProps['aria-invalid']
|
|
18
|
+
delete groupProps['aria-readonly']
|
|
19
|
+
delete groupProps['aria-required']
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
...groupProps,
|
|
23
|
+
'aria-describedby': getGroupDescribedBy(field, fieldControlProps),
|
|
24
|
+
'aria-labelledby': field.labelId,
|
|
25
|
+
role: 'group',
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getGroupDescribedBy(
|
|
30
|
+
field: FieldContextValue,
|
|
31
|
+
fieldControlProps: FieldControlRenderProps,
|
|
32
|
+
): string | undefined {
|
|
33
|
+
const errorId = field.visibleErrors.length > 0 ? field.errorId : undefined
|
|
34
|
+
const ids = [fieldControlProps['aria-describedby'], errorId].filter(Boolean)
|
|
35
|
+
|
|
36
|
+
return ids.length > 0 ? ids.join(' ') : undefined
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ChoicesControlBuilder } from './ChoicesControl.types'
|
|
2
|
+
import { mergeRefs, suppressActivation } from '@maratus-lib/props'
|
|
3
|
+
import { isItemDisabled } from './isItemDisabled'
|
|
4
|
+
|
|
5
|
+
export const getMultiListboxControl: ChoicesControlBuilder<'listbox'> = ({
|
|
6
|
+
field,
|
|
7
|
+
fieldControl,
|
|
8
|
+
}) => ({
|
|
9
|
+
choicesControlProps: {
|
|
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
|
+
'aria-multiselectable': true,
|
|
18
|
+
role: 'listbox',
|
|
19
|
+
},
|
|
20
|
+
getChoicesItemProps: ({ isDisabled, isSelected, ref, ...optionProps }) => ({
|
|
21
|
+
...suppressActivation(optionProps, isItemDisabled(field, isDisabled)),
|
|
22
|
+
...(ref ? { ref: mergeRefs(ref) } : {}),
|
|
23
|
+
...(isItemDisabled(field, isDisabled) ? { 'aria-disabled': true } : {}),
|
|
24
|
+
'aria-selected': isSelected,
|
|
25
|
+
role: 'option',
|
|
26
|
+
}),
|
|
27
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
CheckboxInputOptions,
|
|
3
|
+
CheckboxInputRenderProps,
|
|
4
|
+
CheckboxItemOptions,
|
|
5
|
+
CheckboxItemRenderProps,
|
|
6
|
+
ChoicesControlMode,
|
|
7
|
+
ChoicesControlProps,
|
|
8
|
+
GroupControlRenderProps,
|
|
9
|
+
ListboxOptionOptions,
|
|
10
|
+
ListboxOptionRenderProps,
|
|
11
|
+
MultiListboxControlRenderProps,
|
|
12
|
+
UseCheckboxesControlResult,
|
|
13
|
+
UseCheckboxItemsControlResult,
|
|
14
|
+
UseChoicesControlOptions,
|
|
15
|
+
UseChoicesControlResult,
|
|
16
|
+
UseMultiListboxControlResult,
|
|
17
|
+
} from './ChoicesControl.types'
|
|
18
|
+
export { ChoicesControl } from './ChoicesControl'
|
|
19
|
+
export { useChoicesControl } from './useChoicesControl'
|
|
@@ -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,34 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChoicesControlBuilder,
|
|
3
|
+
ChoicesControlMode,
|
|
4
|
+
UseChoicesControlOptions,
|
|
5
|
+
UseChoicesControlResult,
|
|
6
|
+
} from './ChoicesControl.types'
|
|
7
|
+
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
8
|
+
import { getCheckboxesControl } from './getCheckboxesControl'
|
|
9
|
+
import { getCheckboxItemsControl } from './getCheckboxItemsControl'
|
|
10
|
+
import { getMultiListboxControl } from './getMultiListboxControl'
|
|
11
|
+
import { omitUnsupportedReadOnly } from './omitUnsupportedReadOnly'
|
|
12
|
+
|
|
13
|
+
export function useChoicesControl<TMode extends ChoicesControlMode>(
|
|
14
|
+
options: UseChoicesControlOptions<TMode>,
|
|
15
|
+
): UseChoicesControlResult<TMode> {
|
|
16
|
+
const { mode, ...fieldControlOptions } = omitUnsupportedReadOnly(
|
|
17
|
+
'ChoicesControl',
|
|
18
|
+
options,
|
|
19
|
+
)
|
|
20
|
+
const field = useFieldContext('ChoicesControl')
|
|
21
|
+
const fieldControl = useFieldControl(fieldControlOptions)
|
|
22
|
+
const buildChoicesControl: ChoicesControlBuilder<TMode> =
|
|
23
|
+
choicesControlBuilders[mode]
|
|
24
|
+
|
|
25
|
+
return buildChoicesControl({ field, fieldControl })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const choicesControlBuilders: {
|
|
29
|
+
[TMode in ChoicesControlMode]: ChoicesControlBuilder<TMode>
|
|
30
|
+
} = {
|
|
31
|
+
'checkbox-items': getCheckboxItemsControl,
|
|
32
|
+
checkboxes: getCheckboxesControl,
|
|
33
|
+
listbox: getMultiListboxControl,
|
|
34
|
+
}
|
package/README.md
DELETED