@barqjs/aria 0.0.0-stage → 0.2.0-next.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/LICENSE +21 -0
- package/README.md +293 -2
- package/package.json +347 -4
- package/src/breadcrumbs.tsx +296 -0
- package/src/button.tsx +342 -0
- package/src/calendar.tsx +1389 -0
- package/src/checkbox.tsx +570 -0
- package/src/collections.ts +976 -0
- package/src/color.ts +562 -0
- package/src/colorpicker.tsx +1478 -0
- package/src/combobox.tsx +2481 -0
- package/src/command.tsx +1184 -0
- package/src/date.ts +652 -0
- package/src/datefield.tsx +951 -0
- package/src/datepicker.tsx +556 -0
- package/src/dialog.tsx +890 -0
- package/src/disclosure.tsx +659 -0
- package/src/dom.ts +706 -0
- package/src/drawer.ts +674 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +563 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1270 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +218 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +105 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +416 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +193 -0
- package/src/interactions/press.ts +898 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +128 -0
- package/src/link.tsx +381 -0
- package/src/listbox.tsx +721 -0
- package/src/live.ts +204 -0
- package/src/menu.tsx +2290 -0
- package/src/menubar.tsx +575 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +838 -0
- package/src/otp.ts +430 -0
- package/src/overlays.ts +1247 -0
- package/src/platform.ts +74 -0
- package/src/presence.ts +164 -0
- package/src/questionnaire.tsx +1712 -0
- package/src/radio.tsx +567 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1665 -0
- package/src/selection.ts +1415 -0
- package/src/slider.tsx +1042 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +186 -0
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +1018 -0
- package/src/tag.tsx +452 -0
- package/src/textfield.tsx +417 -0
- package/src/toast.ts +658 -0
- package/src/toggle-input.tsx +48 -0
- package/src/toggle.ts +321 -0
- package/src/togglegroup.ts +365 -0
- package/src/toolbar.tsx +179 -0
- package/src/tooltip.tsx +657 -0
- package/src/utils.ts +758 -0
- package/src/validation.ts +447 -0
- package/src/virtualizer.tsx +674 -0
package/src/checkbox.tsx
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checkboxes, on their own and in a group.
|
|
3
|
+
*
|
|
4
|
+
* The indeterminate state is the interesting part: it is a PROPERTY, settable
|
|
5
|
+
* only from script, and it is presentational — the box shows a dash, but the
|
|
6
|
+
* checkbox is still either checked or not, and pressing it commits whichever
|
|
7
|
+
* the value already was. A group's "select all" is the usual reason for one.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
type Accessor,
|
|
12
|
+
type Child,
|
|
13
|
+
createContext,
|
|
14
|
+
createEffect,
|
|
15
|
+
getContext,
|
|
16
|
+
getOwner,
|
|
17
|
+
type Incoming,
|
|
18
|
+
install,
|
|
19
|
+
} from "@barqjs/core";
|
|
20
|
+
import { ref as makeRef, type RefTarget } from "@barqjs/primitives/refs";
|
|
21
|
+
import { focusRing } from "./focus.ts";
|
|
22
|
+
import { hover } from "./interactions/hover.ts";
|
|
23
|
+
import type { PressEvent } from "./interactions/press.ts";
|
|
24
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
25
|
+
import { field, type FieldOptions } from "./label.ts";
|
|
26
|
+
import {
|
|
27
|
+
toggle,
|
|
28
|
+
toggleState,
|
|
29
|
+
toggleElementType,
|
|
30
|
+
type ToggleElementType,
|
|
31
|
+
type ToggleOptions,
|
|
32
|
+
type ToggleResult,
|
|
33
|
+
} from "./toggle.ts";
|
|
34
|
+
import { toggleInput } from "./toggle-input.tsx";
|
|
35
|
+
import type { ToggleState } from "./button.tsx";
|
|
36
|
+
import {
|
|
37
|
+
fieldValidation,
|
|
38
|
+
type FormValidationState,
|
|
39
|
+
type ValidateFunction,
|
|
40
|
+
type ValidationBehavior,
|
|
41
|
+
} from "./validation.ts";
|
|
42
|
+
import {
|
|
43
|
+
access,
|
|
44
|
+
callback,
|
|
45
|
+
controllable,
|
|
46
|
+
type DOMProps,
|
|
47
|
+
filterDOMProps,
|
|
48
|
+
fromProps,
|
|
49
|
+
type MaybeAccessor,
|
|
50
|
+
mergeProps,
|
|
51
|
+
type StyleProps,
|
|
52
|
+
styleProps,
|
|
53
|
+
} from "./utils.ts";
|
|
54
|
+
|
|
55
|
+
export interface CheckboxOptions extends ToggleOptions {
|
|
56
|
+
/**
|
|
57
|
+
* Show the mixed state.
|
|
58
|
+
*
|
|
59
|
+
* Presentational: the value is still checked or unchecked underneath, and
|
|
60
|
+
* pressing the checkbox commits that value rather than a third one.
|
|
61
|
+
*/
|
|
62
|
+
isIndeterminate?: MaybeAccessor<boolean | undefined>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type CheckboxResult = ToggleResult;
|
|
66
|
+
|
|
67
|
+
export function checkbox(
|
|
68
|
+
options: CheckboxOptions,
|
|
69
|
+
state: ToggleState,
|
|
70
|
+
ref: ElementRef<HTMLInputElement>,
|
|
71
|
+
): CheckboxResult {
|
|
72
|
+
const result = toggle(options, state, ref);
|
|
73
|
+
|
|
74
|
+
// `indeterminate` has no attribute; it exists only as a property.
|
|
75
|
+
createEffect(
|
|
76
|
+
() =>
|
|
77
|
+
[access(ref) as HTMLInputElement | null, access(options.isIndeterminate) === true] as const,
|
|
78
|
+
([input, indeterminate]) => {
|
|
79
|
+
if (input === null) return;
|
|
80
|
+
input.indeterminate = indeterminate;
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
...result,
|
|
86
|
+
inputProps: mergeProps(result.inputProps, {
|
|
87
|
+
// The box shows a dash, and a screen reader must say "mixed".
|
|
88
|
+
"aria-checked": () => (access(options.isIndeterminate) === true ? "mixed" : undefined),
|
|
89
|
+
}),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// Group
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
export interface CheckboxGroupStateOptions {
|
|
98
|
+
value?: MaybeAccessor<string[] | undefined>;
|
|
99
|
+
defaultValue?: MaybeAccessor<string[] | undefined>;
|
|
100
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
101
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
102
|
+
isRequired?: MaybeAccessor<boolean | undefined>;
|
|
103
|
+
isInvalid?: MaybeAccessor<boolean | undefined>;
|
|
104
|
+
/** What the page thinks of the whole selection, checked as it changes. */
|
|
105
|
+
validate?: ValidateFunction<string[]>;
|
|
106
|
+
/** @default "aria" */
|
|
107
|
+
validationBehavior?: MaybeAccessor<ValidationBehavior | undefined>;
|
|
108
|
+
/** The caller's own message, which wins over anything validation found. */
|
|
109
|
+
errorMessage?: MaybeAccessor<unknown>;
|
|
110
|
+
/** The form field name, for matching a server error to this group. */
|
|
111
|
+
name?: MaybeAccessor<string | undefined>;
|
|
112
|
+
onChange?: (value: string[]) => void;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface CheckboxGroupState {
|
|
116
|
+
value: Accessor<string[]>;
|
|
117
|
+
setValue(value: string[]): void;
|
|
118
|
+
isSelected(value: string): boolean;
|
|
119
|
+
addValue(value: string): void;
|
|
120
|
+
removeValue(value: string): void;
|
|
121
|
+
toggleValue(value: string): void;
|
|
122
|
+
isDisabled: Accessor<boolean>;
|
|
123
|
+
isReadOnly: Accessor<boolean>;
|
|
124
|
+
isRequired: Accessor<boolean>;
|
|
125
|
+
isInvalid: Accessor<boolean>;
|
|
126
|
+
/**
|
|
127
|
+
* The validation lives on the STATE rather than on `checkboxGroup`, because
|
|
128
|
+
* a member checkbox reads it too: a group that is invalid marks every box in
|
|
129
|
+
* it, and the box has no way back to the hook.
|
|
130
|
+
*/
|
|
131
|
+
validation: FormValidationState;
|
|
132
|
+
errors: Accessor<string[]>;
|
|
133
|
+
/** For `field`, so `aria-describedby` points at whatever is being shown. */
|
|
134
|
+
errorMessage: Accessor<unknown>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function checkboxGroupState(options: CheckboxGroupStateOptions): CheckboxGroupState {
|
|
138
|
+
const [value, setValue] = controllable<string[]>(
|
|
139
|
+
() => access(options.value),
|
|
140
|
+
() => access(options.defaultValue) ?? [],
|
|
141
|
+
options.onChange,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const isReadOnly = (): boolean => access(options.isReadOnly) === true;
|
|
145
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
146
|
+
|
|
147
|
+
const add = (next: string): void => {
|
|
148
|
+
if (isReadOnly() || isDisabled()) return;
|
|
149
|
+
if (value().includes(next)) return;
|
|
150
|
+
commit([...value(), next]);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const remove = (next: string): void => {
|
|
154
|
+
if (isReadOnly() || isDisabled()) return;
|
|
155
|
+
if (!value().includes(next)) return;
|
|
156
|
+
commit(value().filter((entry) => entry !== next));
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const {
|
|
160
|
+
state: validation,
|
|
161
|
+
isInvalid,
|
|
162
|
+
errors,
|
|
163
|
+
errorMessage,
|
|
164
|
+
} = fieldValidation<string[]>({
|
|
165
|
+
value,
|
|
166
|
+
validate: options.validate,
|
|
167
|
+
validationBehavior: options.validationBehavior,
|
|
168
|
+
isInvalid: options.isInvalid,
|
|
169
|
+
errorMessage: options.errorMessage,
|
|
170
|
+
name: options.name,
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const commit = (next: string[]): void => {
|
|
174
|
+
setValue(next);
|
|
175
|
+
validation.commitValidation();
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
value,
|
|
180
|
+
setValue: (next) => {
|
|
181
|
+
if (isReadOnly() || isDisabled()) return;
|
|
182
|
+
commit(next);
|
|
183
|
+
},
|
|
184
|
+
isSelected: (entry) => value().includes(entry),
|
|
185
|
+
addValue: add,
|
|
186
|
+
removeValue: remove,
|
|
187
|
+
toggleValue: (entry) => (value().includes(entry) ? remove(entry) : add(entry)),
|
|
188
|
+
isDisabled,
|
|
189
|
+
isReadOnly,
|
|
190
|
+
isRequired: () => access(options.isRequired) === true,
|
|
191
|
+
isInvalid,
|
|
192
|
+
validation,
|
|
193
|
+
errors,
|
|
194
|
+
errorMessage,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface CheckboxGroupOptions extends FieldOptions {
|
|
199
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
200
|
+
isRequired?: MaybeAccessor<boolean | undefined>;
|
|
201
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
202
|
+
name?: MaybeAccessor<string | undefined>;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface CheckboxGroupResult {
|
|
206
|
+
groupProps: DOMProps;
|
|
207
|
+
labelProps: DOMProps;
|
|
208
|
+
descriptionProps: DOMProps;
|
|
209
|
+
errorMessageProps: DOMProps;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A set of related checkboxes.
|
|
214
|
+
*
|
|
215
|
+
* `role="group"`, not `radiogroup` and not `list`: the members are
|
|
216
|
+
* independently checkable, and the group exists only so the shared label is
|
|
217
|
+
* announced with each of them. A `<fieldset>` with a `<legend>` would do the
|
|
218
|
+
* same, and this is what to use when the markup cannot be one.
|
|
219
|
+
*/
|
|
220
|
+
export function checkboxGroup(
|
|
221
|
+
options: CheckboxGroupOptions,
|
|
222
|
+
state: CheckboxGroupState,
|
|
223
|
+
): CheckboxGroupResult {
|
|
224
|
+
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = field({
|
|
225
|
+
...options,
|
|
226
|
+
isInvalid: state.isInvalid,
|
|
227
|
+
errorMessage: state.errorMessage,
|
|
228
|
+
// A group is not a labelable element, so `<label for>` would do nothing.
|
|
229
|
+
labelElementType: "span",
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
groupProps: mergeProps(filterDOMProps(options, { labelable: true }), fieldProps, {
|
|
234
|
+
role: "group",
|
|
235
|
+
"aria-disabled": () => access(options.isDisabled) === true || undefined,
|
|
236
|
+
"aria-invalid": () => state.isInvalid() || undefined,
|
|
237
|
+
"aria-required": () => access(options.isRequired) === true || undefined,
|
|
238
|
+
}),
|
|
239
|
+
labelProps,
|
|
240
|
+
descriptionProps,
|
|
241
|
+
errorMessageProps,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ---------------------------------------------------------------------------
|
|
246
|
+
// Components
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
|
|
249
|
+
export interface CheckboxComponentProps extends StyleProps {
|
|
250
|
+
children?: Child;
|
|
251
|
+
/**
|
|
252
|
+
* `"label"` when the children are the checkbox's name, `"span"` when they
|
|
253
|
+
* are its drawing and the name comes from somewhere else. Defaults to a
|
|
254
|
+
* label with children and a span without.
|
|
255
|
+
*/
|
|
256
|
+
elementType?: ToggleElementType;
|
|
257
|
+
isSelected?: boolean;
|
|
258
|
+
defaultSelected?: boolean;
|
|
259
|
+
isIndeterminate?: boolean;
|
|
260
|
+
isDisabled?: boolean;
|
|
261
|
+
isReadOnly?: boolean;
|
|
262
|
+
isRequired?: boolean;
|
|
263
|
+
isInvalid?: boolean;
|
|
264
|
+
/** What the page thinks of the value, checked as it changes. */
|
|
265
|
+
validate?: ValidateFunction<boolean>;
|
|
266
|
+
/** @default "aria" */
|
|
267
|
+
validationBehavior?: ValidationBehavior;
|
|
268
|
+
value?: string;
|
|
269
|
+
name?: string;
|
|
270
|
+
form?: string;
|
|
271
|
+
autoFocus?: boolean;
|
|
272
|
+
excludeFromTabOrder?: boolean;
|
|
273
|
+
"aria-label"?: string;
|
|
274
|
+
"aria-labelledby"?: string;
|
|
275
|
+
"aria-describedby"?: string;
|
|
276
|
+
ref?: RefTarget<HTMLInputElement>;
|
|
277
|
+
onChange?: (isSelected: boolean) => void;
|
|
278
|
+
onFocus?: (event: FocusEvent) => void;
|
|
279
|
+
onBlur?: (event: FocusEvent) => void;
|
|
280
|
+
onPress?: (event: PressEvent) => void;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** What a part drawn inside a checkbox reads. */
|
|
284
|
+
export interface CheckboxContextValue {
|
|
285
|
+
isSelected: Accessor<boolean>;
|
|
286
|
+
isIndeterminate: Accessor<boolean>;
|
|
287
|
+
isDisabled: Accessor<boolean>;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const CheckboxContext = createContext<CheckboxContextValue | null>(null);
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The checkbox a part is drawn inside: the tick or the dash of a design
|
|
294
|
+
* system's box, which appears with a state it does not own.
|
|
295
|
+
*/
|
|
296
|
+
export function useCheckbox(): CheckboxContextValue {
|
|
297
|
+
const value = getContext(CheckboxContext);
|
|
298
|
+
if (value === null || value === undefined) {
|
|
299
|
+
throw new Error("This must be rendered inside a Checkbox.");
|
|
300
|
+
}
|
|
301
|
+
return value;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function checkedState(value: CheckboxContextValue): "checked" | "unchecked" | "indeterminate" {
|
|
305
|
+
if (value.isIndeterminate()) return "indeterminate";
|
|
306
|
+
return value.isSelected() ? "checked" : "unchecked";
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* ```tsx
|
|
311
|
+
* <Checkbox defaultSelected onChange={(on) => agreed.set(on)}>
|
|
312
|
+
* I agree
|
|
313
|
+
* </Checkbox>
|
|
314
|
+
* ```
|
|
315
|
+
*
|
|
316
|
+
* The `<input>` is a real one, hidden with a clip rectangle rather than
|
|
317
|
+
* `display: none`, so it stays focusable and keeps its place in the form.
|
|
318
|
+
*
|
|
319
|
+
* With children it is a `<label>` around them, and they name it. With none it
|
|
320
|
+
* is a `<span>`, so it can sit inside the `<label>` that carries the text:
|
|
321
|
+
*
|
|
322
|
+
* ```tsx
|
|
323
|
+
* <label>
|
|
324
|
+
* <Checkbox /> I agree
|
|
325
|
+
* </label>
|
|
326
|
+
* ```
|
|
327
|
+
*/
|
|
328
|
+
export function Checkbox(props: Incoming<CheckboxComponentProps>) {
|
|
329
|
+
const inputRef = makeRef<HTMLInputElement>();
|
|
330
|
+
const options = fromProps(props);
|
|
331
|
+
|
|
332
|
+
const state = toggleState(options);
|
|
333
|
+
const { labelProps, inputProps, isSelected, isPressed, isDisabled, isReadOnly, isInvalid } =
|
|
334
|
+
checkbox(
|
|
335
|
+
{
|
|
336
|
+
...(options as CheckboxOptions),
|
|
337
|
+
validate: callback(props.validate),
|
|
338
|
+
validationBehavior: () => props.validationBehavior?.(),
|
|
339
|
+
},
|
|
340
|
+
state,
|
|
341
|
+
inputRef,
|
|
342
|
+
);
|
|
343
|
+
const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
|
|
344
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
345
|
+
|
|
346
|
+
const part: CheckboxContextValue = {
|
|
347
|
+
isSelected,
|
|
348
|
+
isIndeterminate: () => props.isIndeterminate?.() === true,
|
|
349
|
+
isDisabled,
|
|
350
|
+
};
|
|
351
|
+
const owner = getOwner();
|
|
352
|
+
if (owner !== null) install(owner, CheckboxContext, () => part);
|
|
353
|
+
|
|
354
|
+
const outerProps = mergeProps(
|
|
355
|
+
labelProps,
|
|
356
|
+
hoverProps,
|
|
357
|
+
// NOT `id`: this is a `<label>`, and `id` belongs on the input it wraps.
|
|
358
|
+
// A `<label for>` elsewhere on the page can only reach a LABELABLE element,
|
|
359
|
+
// so an `id` here answered `getElementById` first — the label came earlier
|
|
360
|
+
// in the document — and clicking the text toggled nothing. Measured on the
|
|
361
|
+
// site's checkbox page.
|
|
362
|
+
filterDOMProps(options, { global: true, id: false }),
|
|
363
|
+
styleProps(props),
|
|
364
|
+
{
|
|
365
|
+
"data-selected": isSelected,
|
|
366
|
+
"data-state": () => checkedState(part),
|
|
367
|
+
"data-indeterminate": () => props.isIndeterminate?.() === true,
|
|
368
|
+
"data-pressed": isPressed,
|
|
369
|
+
"data-hovered": isHovered,
|
|
370
|
+
"data-focused": isFocused,
|
|
371
|
+
"data-focus-visible": isFocusVisible,
|
|
372
|
+
"data-disabled": isDisabled,
|
|
373
|
+
"data-readonly": isReadOnly,
|
|
374
|
+
"data-invalid": isInvalid,
|
|
375
|
+
},
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
return toggleInput({
|
|
379
|
+
elementType: toggleElementType(props.elementType?.(), props.children),
|
|
380
|
+
outerProps,
|
|
381
|
+
inputProps,
|
|
382
|
+
focusProps,
|
|
383
|
+
inputRef,
|
|
384
|
+
ref: props.ref?.(),
|
|
385
|
+
children: props.children,
|
|
386
|
+
isDisabled,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export interface CheckboxGroupComponentProps extends StyleProps {
|
|
391
|
+
children?: Child;
|
|
392
|
+
label?: Child;
|
|
393
|
+
description?: Child;
|
|
394
|
+
errorMessage?: Child;
|
|
395
|
+
value?: string[];
|
|
396
|
+
defaultValue?: string[];
|
|
397
|
+
isDisabled?: boolean;
|
|
398
|
+
isReadOnly?: boolean;
|
|
399
|
+
isRequired?: boolean;
|
|
400
|
+
isInvalid?: boolean;
|
|
401
|
+
/** What the page thinks of the whole selection, checked as it changes. */
|
|
402
|
+
validate?: ValidateFunction<string[]>;
|
|
403
|
+
/** @default "aria" */
|
|
404
|
+
validationBehavior?: ValidationBehavior;
|
|
405
|
+
name?: string;
|
|
406
|
+
"aria-label"?: string;
|
|
407
|
+
"aria-labelledby"?: string;
|
|
408
|
+
onChange?: (value: string[]) => void;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* ```tsx
|
|
413
|
+
* <CheckboxGroup label="Toppings" onChange={(v) => picked.set(v)}>
|
|
414
|
+
* <GroupCheckbox value="cheese">Cheese</GroupCheckbox>
|
|
415
|
+
* <GroupCheckbox value="olives">Olives</GroupCheckbox>
|
|
416
|
+
* </CheckboxGroup>
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
export function CheckboxGroup(props: Incoming<CheckboxGroupComponentProps>) {
|
|
420
|
+
const options = fromProps(props);
|
|
421
|
+
const state = checkboxGroupState({
|
|
422
|
+
...(options as CheckboxGroupStateOptions),
|
|
423
|
+
validate: callback(props.validate),
|
|
424
|
+
validationBehavior: () => props.validationBehavior?.(),
|
|
425
|
+
});
|
|
426
|
+
const { groupProps, labelProps, descriptionProps, errorMessageProps } = checkboxGroup(
|
|
427
|
+
options,
|
|
428
|
+
state,
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
provideCheckboxGroup(state, options);
|
|
432
|
+
|
|
433
|
+
const outerProps = mergeProps(
|
|
434
|
+
groupProps,
|
|
435
|
+
filterDOMProps(options, { global: true }),
|
|
436
|
+
styleProps(props),
|
|
437
|
+
{
|
|
438
|
+
"data-disabled": state.isDisabled,
|
|
439
|
+
"data-readonly": state.isReadOnly,
|
|
440
|
+
"data-invalid": state.isInvalid,
|
|
441
|
+
"data-required": state.isRequired,
|
|
442
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
443
|
+
},
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
return (
|
|
447
|
+
<div {...outerProps}>
|
|
448
|
+
<span {...labelProps}>{props.label}</span>
|
|
449
|
+
{props.children}
|
|
450
|
+
<span {...descriptionProps}>{props.description}</span>
|
|
451
|
+
<span {...errorMessageProps}>
|
|
452
|
+
{() => (state.isInvalid() ? (state.errorMessage() as Child) : null)}
|
|
453
|
+
</span>
|
|
454
|
+
</div>
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export interface GroupCheckboxProps extends Omit<
|
|
459
|
+
CheckboxComponentProps,
|
|
460
|
+
"isSelected" | "defaultSelected"
|
|
461
|
+
> {
|
|
462
|
+
value: string;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* A checkbox that belongs to the enclosing {@link CheckboxGroup}.
|
|
467
|
+
*
|
|
468
|
+
* Its selection lives in the group, so the group's value is one array rather
|
|
469
|
+
* than one boolean per member.
|
|
470
|
+
*/
|
|
471
|
+
export function GroupCheckbox(props: Incoming<GroupCheckboxProps>) {
|
|
472
|
+
const inputRef = makeRef<HTMLInputElement>();
|
|
473
|
+
const options = fromProps(props);
|
|
474
|
+
const group = useCheckboxGroup();
|
|
475
|
+
|
|
476
|
+
const state = toggleState({
|
|
477
|
+
isSelected: () => group.state.isSelected(props.value()),
|
|
478
|
+
isReadOnly: () => group.state.isReadOnly() || props.isReadOnly?.() === true,
|
|
479
|
+
onChange: (selected) => {
|
|
480
|
+
if (selected) group.state.addValue(props.value());
|
|
481
|
+
else group.state.removeValue(props.value());
|
|
482
|
+
props.onChange?.()?.(selected);
|
|
483
|
+
},
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
const { labelProps, inputProps, isSelected, isPressed, isDisabled, isReadOnly } = checkbox(
|
|
487
|
+
{
|
|
488
|
+
...(options as CheckboxOptions),
|
|
489
|
+
isDisabled: () => group.state.isDisabled() || props.isDisabled?.() === true,
|
|
490
|
+
isReadOnly: () => group.state.isReadOnly() || props.isReadOnly?.() === true,
|
|
491
|
+
isRequired: () => group.state.isRequired() || props.isRequired?.() === true,
|
|
492
|
+
isInvalid: () => group.state.isInvalid() || props.isInvalid?.() === true,
|
|
493
|
+
name: () => props.name?.() ?? group.name(),
|
|
494
|
+
},
|
|
495
|
+
state,
|
|
496
|
+
inputRef,
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
const { hoverProps, isHovered } = hover({ isDisabled });
|
|
500
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
501
|
+
|
|
502
|
+
const part: CheckboxContextValue = {
|
|
503
|
+
isSelected,
|
|
504
|
+
isIndeterminate: () => props.isIndeterminate?.() === true,
|
|
505
|
+
isDisabled,
|
|
506
|
+
};
|
|
507
|
+
const owner = getOwner();
|
|
508
|
+
if (owner !== null) install(owner, CheckboxContext, () => part);
|
|
509
|
+
|
|
510
|
+
const outerProps = mergeProps(
|
|
511
|
+
labelProps,
|
|
512
|
+
hoverProps,
|
|
513
|
+
// NOT `id`: this is a `<label>`, and `id` belongs on the input it wraps.
|
|
514
|
+
// A `<label for>` elsewhere on the page can only reach a LABELABLE element,
|
|
515
|
+
// so an `id` here answered `getElementById` first — the label came earlier
|
|
516
|
+
// in the document — and clicking the text toggled nothing. Measured on the
|
|
517
|
+
// site's checkbox page.
|
|
518
|
+
filterDOMProps(options, { global: true, id: false }),
|
|
519
|
+
styleProps(props),
|
|
520
|
+
{
|
|
521
|
+
"data-selected": isSelected,
|
|
522
|
+
"data-state": () => checkedState(part),
|
|
523
|
+
"data-pressed": isPressed,
|
|
524
|
+
"data-hovered": isHovered,
|
|
525
|
+
"data-focused": isFocused,
|
|
526
|
+
"data-focus-visible": isFocusVisible,
|
|
527
|
+
"data-disabled": isDisabled,
|
|
528
|
+
"data-readonly": isReadOnly,
|
|
529
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
530
|
+
},
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
return toggleInput({
|
|
534
|
+
elementType: toggleElementType(props.elementType?.(), props.children),
|
|
535
|
+
outerProps,
|
|
536
|
+
inputProps,
|
|
537
|
+
focusProps,
|
|
538
|
+
inputRef,
|
|
539
|
+
ref: props.ref?.(),
|
|
540
|
+
children: props.children,
|
|
541
|
+
isDisabled,
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// ---------------------------------------------------------------------------
|
|
546
|
+
// The group's context
|
|
547
|
+
// ---------------------------------------------------------------------------
|
|
548
|
+
|
|
549
|
+
interface CheckboxGroupContextValue {
|
|
550
|
+
state: CheckboxGroupState;
|
|
551
|
+
name: Accessor<string | undefined>;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const CheckboxGroupContext = createContext<CheckboxGroupContextValue | null>(null);
|
|
555
|
+
|
|
556
|
+
function provideCheckboxGroup(state: CheckboxGroupState, options: CheckboxGroupOptions): void {
|
|
557
|
+
const owner = getOwner();
|
|
558
|
+
if (owner === null) return;
|
|
559
|
+
const value: CheckboxGroupContextValue = { state, name: () => access(options.name) };
|
|
560
|
+
install(owner, CheckboxGroupContext, () => value);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** The enclosing checkbox group. Throws outside one, which is a coding error. */
|
|
564
|
+
export function useCheckboxGroup(): CheckboxGroupContextValue {
|
|
565
|
+
const value = getContext(CheckboxGroupContext);
|
|
566
|
+
if (value === null || value === undefined) {
|
|
567
|
+
throw new Error("A GroupCheckbox must be rendered inside a CheckboxGroup.");
|
|
568
|
+
}
|
|
569
|
+
return value;
|
|
570
|
+
}
|