@barqjs/aria 0.0.0-stage → 0.2.0-next.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/LICENSE +21 -0
- package/README.md +304 -2
- package/package.json +437 -4
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +297 -0
- package/src/button.tsx +468 -0
- package/src/calendar.tsx +2316 -0
- package/src/checkbox.tsx +732 -0
- package/src/collections.ts +1059 -0
- package/src/color.ts +2119 -0
- package/src/colorpicker.tsx +2023 -0
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +3003 -0
- package/src/command.tsx +1203 -0
- package/src/date.ts +674 -0
- package/src/datefield.tsx +2066 -0
- package/src/datepicker.tsx +907 -0
- package/src/dialog.tsx +1077 -0
- package/src/disclosure.tsx +711 -0
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +703 -0
- package/src/drag-and-drop.tsx +669 -0
- package/src/drawer.ts +674 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +1354 -0
- package/src/group.tsx +146 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1672 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +226 -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 +298 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +428 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +226 -0
- package/src/interactions/press.ts +915 -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 +140 -0
- package/src/link.tsx +477 -0
- package/src/listbox.tsx +1247 -0
- package/src/live.ts +208 -0
- package/src/menu.tsx +2383 -0
- package/src/menubar.tsx +579 -0
- package/src/navigation-tree.tsx +357 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +942 -0
- package/src/otp.ts +430 -0
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +1549 -0
- package/src/platform.ts +75 -0
- package/src/presence.ts +164 -0
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +1717 -0
- package/src/radio.tsx +641 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1745 -0
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +1472 -0
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +1137 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +212 -0
- package/src/table.tsx +2218 -0
- package/src/tabs.tsx +1127 -0
- package/src/tag.tsx +637 -0
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +483 -0
- package/src/toast-components.tsx +589 -0
- package/src/toast.ts +658 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +86 -0
- package/src/toggle.ts +384 -0
- package/src/togglegroup.ts +365 -0
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +184 -0
- package/src/tooltip.tsx +674 -0
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +787 -0
- package/src/validation.ts +504 -0
- package/src/virtualizer.tsx +837 -0
package/src/textarea.tsx
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A text area on its own: a `<textarea>` with the interaction states a
|
|
3
|
+
* stylesheet needs.
|
|
4
|
+
*
|
|
5
|
+
* A labelled, validated multi-line field is `<TextField isMultiLine>`, which
|
|
6
|
+
* wires the label, description and error message to the control. This is the
|
|
7
|
+
* control alone, for a caller that labels it some other way: a chat composer
|
|
8
|
+
* named by `aria-label`, a cell editor inside a grid.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { cells, type Accessor } from "@barqjs/core";
|
|
12
|
+
import { mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
13
|
+
import { focusRing } from "./focus.ts";
|
|
14
|
+
import { hover, type HoverEvent } from "./interactions/hover.ts";
|
|
15
|
+
import {
|
|
16
|
+
access,
|
|
17
|
+
controllable,
|
|
18
|
+
type DOMProps,
|
|
19
|
+
filterDOMProps,
|
|
20
|
+
fromProps,
|
|
21
|
+
type MaybeAccessor,
|
|
22
|
+
mergeProps,
|
|
23
|
+
type StyleProps,
|
|
24
|
+
styleProps,
|
|
25
|
+
} from "./utils.ts";
|
|
26
|
+
|
|
27
|
+
export interface TextAreaOptions {
|
|
28
|
+
value?: MaybeAccessor<string | undefined>;
|
|
29
|
+
defaultValue?: MaybeAccessor<string | undefined>;
|
|
30
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
31
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
32
|
+
isRequired?: MaybeAccessor<boolean | undefined>;
|
|
33
|
+
isInvalid?: MaybeAccessor<boolean | undefined>;
|
|
34
|
+
autoFocus?: MaybeAccessor<boolean | undefined>;
|
|
35
|
+
onChange?: (value: string) => void;
|
|
36
|
+
onHoverStart?: (event: HoverEvent) => void;
|
|
37
|
+
onHoverEnd?: (event: HoverEvent) => void;
|
|
38
|
+
onHoverChange?: (isHovered: boolean) => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TextAreaResult {
|
|
42
|
+
textAreaProps: DOMProps;
|
|
43
|
+
value: Accessor<string>;
|
|
44
|
+
isHovered: Accessor<boolean>;
|
|
45
|
+
isFocused: Accessor<boolean>;
|
|
46
|
+
isFocusVisible: Accessor<boolean>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function textArea(options: TextAreaOptions = {}): TextAreaResult {
|
|
50
|
+
const [value, setValue] = controllable<string>(
|
|
51
|
+
() => access(options.value),
|
|
52
|
+
() => access(options.defaultValue) ?? "",
|
|
53
|
+
options.onChange,
|
|
54
|
+
);
|
|
55
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
56
|
+
const isInvalid = (): boolean => access(options.isInvalid) === true;
|
|
57
|
+
|
|
58
|
+
const { hoverProps, isHovered } = hover({
|
|
59
|
+
isDisabled,
|
|
60
|
+
onHoverStart: options.onHoverStart,
|
|
61
|
+
onHoverEnd: options.onHoverEnd,
|
|
62
|
+
onHoverChange: options.onHoverChange,
|
|
63
|
+
});
|
|
64
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing({
|
|
65
|
+
isTextInput: true,
|
|
66
|
+
autoFocus: options.autoFocus,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const onInput = (event: Event): void => {
|
|
70
|
+
const element = event.target as HTMLTextAreaElement;
|
|
71
|
+
setValue(element.value);
|
|
72
|
+
// A controlled owner that declined re-renders nothing on a value that did
|
|
73
|
+
// not move, so the element is put back by hand.
|
|
74
|
+
if (element.value !== value()) element.value = value();
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
value,
|
|
79
|
+
isHovered,
|
|
80
|
+
isFocused,
|
|
81
|
+
isFocusVisible,
|
|
82
|
+
textAreaProps: mergeProps(hoverProps, focusProps, {
|
|
83
|
+
value,
|
|
84
|
+
disabled: isDisabled,
|
|
85
|
+
readOnly: () => access(options.isReadOnly) === true,
|
|
86
|
+
required: () => access(options.isRequired) === true,
|
|
87
|
+
autofocus: () => access(options.autoFocus) === true || undefined,
|
|
88
|
+
"aria-invalid": () => isInvalid() || undefined,
|
|
89
|
+
onInput,
|
|
90
|
+
"data-hovered": isHovered,
|
|
91
|
+
"data-focused": isFocused,
|
|
92
|
+
"data-focus-visible": isFocusVisible,
|
|
93
|
+
"data-disabled": isDisabled,
|
|
94
|
+
"data-invalid": isInvalid,
|
|
95
|
+
}),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const TEXTAREA_ATTRIBUTES = new Set([
|
|
100
|
+
"aria-controls",
|
|
101
|
+
"aria-activedescendant",
|
|
102
|
+
"aria-autocomplete",
|
|
103
|
+
"aria-errormessage",
|
|
104
|
+
"aria-haspopup",
|
|
105
|
+
"onKeyDown",
|
|
106
|
+
"onKeyUp",
|
|
107
|
+
"onBeforeInput",
|
|
108
|
+
"onSelect",
|
|
109
|
+
"onCopy",
|
|
110
|
+
"onCut",
|
|
111
|
+
"onPaste",
|
|
112
|
+
"onCompositionStart",
|
|
113
|
+
"onCompositionEnd",
|
|
114
|
+
"onCompositionUpdate",
|
|
115
|
+
]);
|
|
116
|
+
|
|
117
|
+
export interface TextAreaComponentProps extends StyleProps {
|
|
118
|
+
value?: string;
|
|
119
|
+
defaultValue?: string;
|
|
120
|
+
isDisabled?: boolean;
|
|
121
|
+
isReadOnly?: boolean;
|
|
122
|
+
isRequired?: boolean;
|
|
123
|
+
isInvalid?: boolean;
|
|
124
|
+
autoFocus?: boolean;
|
|
125
|
+
name?: string;
|
|
126
|
+
form?: string;
|
|
127
|
+
placeholder?: string;
|
|
128
|
+
rows?: number;
|
|
129
|
+
cols?: number;
|
|
130
|
+
wrap?: "hard" | "soft" | "off";
|
|
131
|
+
maxLength?: number;
|
|
132
|
+
minLength?: number;
|
|
133
|
+
autoComplete?: string;
|
|
134
|
+
spellcheck?: boolean | "true" | "false";
|
|
135
|
+
inputMode?: string;
|
|
136
|
+
enterKeyHint?: string;
|
|
137
|
+
"aria-label"?: string;
|
|
138
|
+
"aria-labelledby"?: string;
|
|
139
|
+
"aria-describedby"?: string;
|
|
140
|
+
"aria-details"?: string;
|
|
141
|
+
"aria-controls"?: string;
|
|
142
|
+
"aria-activedescendant"?: string;
|
|
143
|
+
"aria-autocomplete"?: "none" | "inline" | "list" | "both";
|
|
144
|
+
"aria-errormessage"?: string;
|
|
145
|
+
ref?: RefTarget<HTMLTextAreaElement>;
|
|
146
|
+
onChange?: (value: string) => void;
|
|
147
|
+
onKeyDown?: (event: KeyboardEvent) => void;
|
|
148
|
+
onKeyUp?: (event: KeyboardEvent) => void;
|
|
149
|
+
onBeforeInput?: (event: InputEvent) => void;
|
|
150
|
+
onHoverStart?: (event: HoverEvent) => void;
|
|
151
|
+
onHoverEnd?: (event: HoverEvent) => void;
|
|
152
|
+
onHoverChange?: (isHovered: boolean) => void;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* ```tsx
|
|
157
|
+
* <TextArea aria-label="Message" rows={4} onChange={(text) => draft.set(text)} />
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export function TextArea(props: TextAreaComponentProps) {
|
|
161
|
+
const options = fromProps(cells(props));
|
|
162
|
+
const { textAreaProps } = textArea(options);
|
|
163
|
+
|
|
164
|
+
const elementProps = mergeProps(
|
|
165
|
+
textAreaProps,
|
|
166
|
+
{
|
|
167
|
+
name: () => props.name,
|
|
168
|
+
form: () => props.form,
|
|
169
|
+
placeholder: () => props.placeholder,
|
|
170
|
+
rows: () => props.rows,
|
|
171
|
+
cols: () => props.cols,
|
|
172
|
+
wrap: () => props.wrap,
|
|
173
|
+
maxlength: () => props.maxLength,
|
|
174
|
+
minlength: () => props.minLength,
|
|
175
|
+
autocomplete: () => props.autoComplete,
|
|
176
|
+
spellcheck: () => props.spellcheck,
|
|
177
|
+
inputmode: () => props.inputMode,
|
|
178
|
+
enterkeyhint: () => props.enterKeyHint,
|
|
179
|
+
},
|
|
180
|
+
filterDOMProps(options, { global: true, labelable: true, propNames: TEXTAREA_ATTRIBUTES }),
|
|
181
|
+
styleProps(cells(props)),
|
|
182
|
+
{ "data-testid": () => props["data-testid"] },
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
return <textarea {...elementProps} ref={mergeRefs(props.ref)} />;
|
|
186
|
+
}
|
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text fields.
|
|
3
|
+
*
|
|
4
|
+
* The input itself is the platform's, and almost all of the accessibility with
|
|
5
|
+
* it. What is added is the wiring a field needs and the platform has no way to
|
|
6
|
+
* express: the label, the description and the error message all announced with
|
|
7
|
+
* the control, the value controllable without the caret jumping, and a search
|
|
8
|
+
* field's Escape-to-clear.
|
|
9
|
+
*
|
|
10
|
+
* `aria-describedby` rather than `aria-errormessage` for the error, because
|
|
11
|
+
* VoiceOver and NVDA still do not announce the latter.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { cells, type Accessor, type Child } from "@barqjs/core";
|
|
15
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
16
|
+
import { type AutocompleteInputValue, useAutocompleteInput } from "./autocomplete.tsx";
|
|
17
|
+
import { focusRing } from "./focus.ts";
|
|
18
|
+
import { focusable, type FocusableOptions } from "./interactions/focusable.ts";
|
|
19
|
+
import { hover } from "./interactions/hover.ts";
|
|
20
|
+
import { type ElementRef } from "./interactions/press.ts";
|
|
21
|
+
import { field, type FieldOptions } from "./label.ts";
|
|
22
|
+
import { formReset } from "./toggle.ts";
|
|
23
|
+
import {
|
|
24
|
+
fieldValidation,
|
|
25
|
+
formValidation,
|
|
26
|
+
type FormValidationState,
|
|
27
|
+
type ValidateFunction,
|
|
28
|
+
type ValidationBehavior,
|
|
29
|
+
} from "./validation.ts";
|
|
30
|
+
import { button } from "./button.tsx";
|
|
31
|
+
import {
|
|
32
|
+
access,
|
|
33
|
+
callback,
|
|
34
|
+
chain,
|
|
35
|
+
controllable,
|
|
36
|
+
filterDOMProps,
|
|
37
|
+
fromProps,
|
|
38
|
+
mergeProps,
|
|
39
|
+
styleProps,
|
|
40
|
+
type DOMProps,
|
|
41
|
+
type MaybeAccessor,
|
|
42
|
+
type StyleProps,
|
|
43
|
+
} from "./utils.ts";
|
|
44
|
+
|
|
45
|
+
export type TextFieldType = "text" | "search" | "url" | "tel" | "email" | "password";
|
|
46
|
+
|
|
47
|
+
export interface TextFieldOptions extends FieldOptions, FocusableOptions {
|
|
48
|
+
value?: MaybeAccessor<string | undefined>;
|
|
49
|
+
/** What the page thinks of the value, checked as it changes. */
|
|
50
|
+
validate?: ValidateFunction<string>;
|
|
51
|
+
/** @default "aria" */
|
|
52
|
+
validationBehavior?: MaybeAccessor<ValidationBehavior | undefined>;
|
|
53
|
+
defaultValue?: MaybeAccessor<string | undefined>;
|
|
54
|
+
/** @default "text" */
|
|
55
|
+
type?: MaybeAccessor<TextFieldType | undefined>;
|
|
56
|
+
/** Render a `<textarea>` rather than an `<input>`. */
|
|
57
|
+
isMultiLine?: MaybeAccessor<boolean | undefined>;
|
|
58
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
59
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
60
|
+
isRequired?: MaybeAccessor<boolean | undefined>;
|
|
61
|
+
placeholder?: MaybeAccessor<string | undefined>;
|
|
62
|
+
name?: MaybeAccessor<string | undefined>;
|
|
63
|
+
form?: MaybeAccessor<string | undefined>;
|
|
64
|
+
autoComplete?: MaybeAccessor<string | undefined>;
|
|
65
|
+
maxLength?: MaybeAccessor<number | undefined>;
|
|
66
|
+
minLength?: MaybeAccessor<number | undefined>;
|
|
67
|
+
pattern?: MaybeAccessor<string | undefined>;
|
|
68
|
+
inputMode?: MaybeAccessor<string | undefined>;
|
|
69
|
+
onChange?: (value: string) => void;
|
|
70
|
+
onKeyDown?: (event: KeyboardEvent) => void;
|
|
71
|
+
onKeyUp?: (event: KeyboardEvent) => void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface TextFieldResult {
|
|
75
|
+
labelProps: DOMProps;
|
|
76
|
+
inputProps: DOMProps;
|
|
77
|
+
descriptionProps: DOMProps;
|
|
78
|
+
errorMessageProps: DOMProps;
|
|
79
|
+
value: Accessor<string>;
|
|
80
|
+
setValue(value: string): void;
|
|
81
|
+
/** What is wrong with the value, and whether the user is being told. */
|
|
82
|
+
validation: FormValidationState;
|
|
83
|
+
/** The messages to show, which may be the browser's own. */
|
|
84
|
+
errors: Accessor<string[]>;
|
|
85
|
+
isInvalid: Accessor<boolean>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function textField(
|
|
89
|
+
options: TextFieldOptions,
|
|
90
|
+
ref: ElementRef<HTMLInputElement | HTMLTextAreaElement>,
|
|
91
|
+
): TextFieldResult {
|
|
92
|
+
const [value, setValue] = controllable<string>(
|
|
93
|
+
() => access(options.value),
|
|
94
|
+
() => access(options.defaultValue) ?? "",
|
|
95
|
+
options.onChange,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
// What the BROWSER makes of it is NOT read back into the state's own view of
|
|
99
|
+
// the value: the page's errors are written onto the element with
|
|
100
|
+
// `setCustomValidity`, so reading the element's validity as an input would
|
|
101
|
+
// be reading back what was just written and calling it a new finding.
|
|
102
|
+
// `formValidation` reads it only once the page has nothing to say.
|
|
103
|
+
const {
|
|
104
|
+
state: validation,
|
|
105
|
+
behavior,
|
|
106
|
+
isInvalid,
|
|
107
|
+
errors,
|
|
108
|
+
errorMessage,
|
|
109
|
+
} = fieldValidation<string>({
|
|
110
|
+
value,
|
|
111
|
+
validate: options.validate,
|
|
112
|
+
validationBehavior: options.validationBehavior,
|
|
113
|
+
isInvalid: options.isInvalid,
|
|
114
|
+
errorMessage: options.errorMessage,
|
|
115
|
+
name: options.name,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
formValidation(
|
|
119
|
+
{
|
|
120
|
+
validationBehavior: behavior,
|
|
121
|
+
focus: () => (access(ref) as HTMLInputElement | null)?.focus(),
|
|
122
|
+
},
|
|
123
|
+
validation,
|
|
124
|
+
ref,
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const { labelProps, fieldProps, descriptionProps, errorMessageProps } = field({
|
|
128
|
+
...options,
|
|
129
|
+
isInvalid,
|
|
130
|
+
errorMessage,
|
|
131
|
+
});
|
|
132
|
+
const { focusableProps } = focusable(options, ref);
|
|
133
|
+
|
|
134
|
+
formReset(ref as ElementRef<HTMLInputElement>, access(options.defaultValue) ?? "", setValue);
|
|
135
|
+
|
|
136
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
137
|
+
const isReadOnly = (): boolean => access(options.isReadOnly) === true;
|
|
138
|
+
const isRequired = (): boolean => access(options.isRequired) === true;
|
|
139
|
+
const isNative = (): boolean => behavior() === "native";
|
|
140
|
+
|
|
141
|
+
// A blur commits only what was edited, as `change` would: tabbing through a
|
|
142
|
+
// field the server rejected has fixed nothing, and must not clear the error.
|
|
143
|
+
let focusedValue: string | null = null;
|
|
144
|
+
const onFocus = (): void => {
|
|
145
|
+
focusedValue = value();
|
|
146
|
+
};
|
|
147
|
+
const onBlur = (): void => {
|
|
148
|
+
const edited = focusedValue !== null && focusedValue !== value();
|
|
149
|
+
focusedValue = null;
|
|
150
|
+
if (edited) validation.commitValidation();
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const onInput = (event: Event): void => {
|
|
154
|
+
const input = event.target as HTMLInputElement;
|
|
155
|
+
setValue(input.value);
|
|
156
|
+
// The field has already changed itself. When the component is controlled
|
|
157
|
+
// and its owner declined, or it is read-only, nothing re-renders on a
|
|
158
|
+
// value that did not move, so the DOM has to be put back by hand.
|
|
159
|
+
if (input.value !== value()) input.value = value();
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
value,
|
|
164
|
+
setValue,
|
|
165
|
+
validation,
|
|
166
|
+
errors,
|
|
167
|
+
isInvalid,
|
|
168
|
+
labelProps,
|
|
169
|
+
descriptionProps,
|
|
170
|
+
errorMessageProps,
|
|
171
|
+
inputProps: mergeProps(
|
|
172
|
+
filterDOMProps(options, { labelable: true }),
|
|
173
|
+
fieldProps,
|
|
174
|
+
focusableProps,
|
|
175
|
+
{
|
|
176
|
+
type: () =>
|
|
177
|
+
access(options.isMultiLine) === true ? undefined : (access(options.type) ?? "text"),
|
|
178
|
+
value,
|
|
179
|
+
disabled: isDisabled,
|
|
180
|
+
readOnly: isReadOnly,
|
|
181
|
+
// The browser refuses the submit for `required`, so only when the page
|
|
182
|
+
// asked for that; otherwise assistive technology is told instead.
|
|
183
|
+
required: () => isRequired() && isNative(),
|
|
184
|
+
placeholder: () => access(options.placeholder),
|
|
185
|
+
name: () => access(options.name),
|
|
186
|
+
form: () => access(options.form),
|
|
187
|
+
autocomplete: () => access(options.autoComplete),
|
|
188
|
+
maxlength: () => access(options.maxLength),
|
|
189
|
+
minlength: () => access(options.minLength),
|
|
190
|
+
pattern: () => access(options.pattern),
|
|
191
|
+
inputmode: () => access(options.inputMode),
|
|
192
|
+
"aria-invalid": () => isInvalid() || undefined,
|
|
193
|
+
"aria-required": () => (isRequired() && !isNative()) || undefined,
|
|
194
|
+
"aria-readonly": () => isReadOnly() || undefined,
|
|
195
|
+
onInput,
|
|
196
|
+
onKeyDown: options.onKeyDown,
|
|
197
|
+
onKeyUp: options.onKeyUp,
|
|
198
|
+
onFocus,
|
|
199
|
+
onBlur,
|
|
200
|
+
},
|
|
201
|
+
),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
// Search field
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
|
|
209
|
+
export interface SearchFieldOptions extends TextFieldOptions {
|
|
210
|
+
/** Fired on Enter, and on the clear button. */
|
|
211
|
+
onSubmit?: (value: string) => void;
|
|
212
|
+
onClear?: () => void;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface SearchFieldResult extends TextFieldResult {
|
|
216
|
+
clearButtonProps: DOMProps;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* A search field: `type="search"`, Enter submits, Escape clears.
|
|
221
|
+
*
|
|
222
|
+
* The clear button is `tabIndex={-1}` and hidden from assistive technology,
|
|
223
|
+
* which sounds wrong and is not: Escape already clears the field from the
|
|
224
|
+
* keyboard, so a Tab stop that does the same thing is an extra stop between
|
|
225
|
+
* the field and whatever follows it, for no new capability.
|
|
226
|
+
*/
|
|
227
|
+
export function searchField(
|
|
228
|
+
options: SearchFieldOptions,
|
|
229
|
+
ref: ElementRef<HTMLInputElement>,
|
|
230
|
+
): SearchFieldResult {
|
|
231
|
+
const result = textField({ ...options, type: () => access(options.type) ?? "search" }, ref);
|
|
232
|
+
|
|
233
|
+
const clear = (): void => {
|
|
234
|
+
if (access(options.isReadOnly) === true || access(options.isDisabled) === true) return;
|
|
235
|
+
result.setValue("");
|
|
236
|
+
const input = access(ref) as HTMLInputElement | null;
|
|
237
|
+
if (input !== null) input.value = "";
|
|
238
|
+
options.onClear?.();
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
242
|
+
if (access(options.isReadOnly) === true || access(options.isDisabled) === true) return;
|
|
243
|
+
if (event.key === "Enter") {
|
|
244
|
+
// Without a handler of its own, Enter is the form's: it submits.
|
|
245
|
+
if (options.onSubmit === undefined) return;
|
|
246
|
+
event.preventDefault();
|
|
247
|
+
options.onSubmit(result.value());
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (event.key === "Escape") {
|
|
251
|
+
const input = access(ref) as HTMLInputElement | null;
|
|
252
|
+
// Nothing to clear, so the key is someone else's: the dialog around it.
|
|
253
|
+
if (result.value() === "" && (input === null || input.value === "")) return;
|
|
254
|
+
// Handled here, so it goes no further: Escape that cleared the field
|
|
255
|
+
// must not also close the dialog it is in.
|
|
256
|
+
event.preventDefault();
|
|
257
|
+
event.stopPropagation();
|
|
258
|
+
clear();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const { buttonProps } = button({
|
|
263
|
+
isDisabled: () => access(options.isDisabled) === true || access(options.isReadOnly) === true,
|
|
264
|
+
// Pressing it must not take focus off the field the user is typing in.
|
|
265
|
+
preventFocusOnPress: true,
|
|
266
|
+
excludeFromTabOrder: true,
|
|
267
|
+
onPress: clear,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
return {
|
|
271
|
+
...result,
|
|
272
|
+
inputProps: mergeProps(result.inputProps, {
|
|
273
|
+
onKeyDown: chain(onKeyDown, options.onKeyDown),
|
|
274
|
+
}),
|
|
275
|
+
clearButtonProps: mergeProps(buttonProps, { "aria-hidden": true, tabIndex: -1 }),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ---------------------------------------------------------------------------
|
|
280
|
+
// Components
|
|
281
|
+
// ---------------------------------------------------------------------------
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Inside an {@link Autocomplete} the field's value is the query. A value the
|
|
285
|
+
* caller gave still wins, and the caller's `onChange` hears every change after
|
|
286
|
+
* the autocomplete has.
|
|
287
|
+
*/
|
|
288
|
+
function queryOf(
|
|
289
|
+
auto: AutocompleteInputValue | null,
|
|
290
|
+
options: TextFieldOptions,
|
|
291
|
+
): Partial<TextFieldOptions> {
|
|
292
|
+
if (auto === null) return {};
|
|
293
|
+
return {
|
|
294
|
+
value: () => access(options.value) ?? auto.inputOptions.value(),
|
|
295
|
+
onChange: chain((value: string) => auto.inputOptions.onChange(value), options.onChange),
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export interface TextFieldComponentProps extends StyleProps {
|
|
300
|
+
label?: Child;
|
|
301
|
+
description?: Child;
|
|
302
|
+
errorMessage?: Child;
|
|
303
|
+
value?: string;
|
|
304
|
+
defaultValue?: string;
|
|
305
|
+
type?: TextFieldType;
|
|
306
|
+
isMultiLine?: boolean;
|
|
307
|
+
isDisabled?: boolean;
|
|
308
|
+
isReadOnly?: boolean;
|
|
309
|
+
isRequired?: boolean;
|
|
310
|
+
isInvalid?: boolean;
|
|
311
|
+
/** What the page thinks of the value. Return a message, or nothing. */
|
|
312
|
+
validate?: ValidateFunction<string>;
|
|
313
|
+
/** @default "aria" */
|
|
314
|
+
validationBehavior?: ValidationBehavior;
|
|
315
|
+
placeholder?: string;
|
|
316
|
+
name?: string;
|
|
317
|
+
form?: string;
|
|
318
|
+
autoComplete?: string;
|
|
319
|
+
autoFocus?: boolean;
|
|
320
|
+
maxLength?: number;
|
|
321
|
+
minLength?: number;
|
|
322
|
+
pattern?: string;
|
|
323
|
+
inputMode?: string;
|
|
324
|
+
"aria-label"?: string;
|
|
325
|
+
"aria-labelledby"?: string;
|
|
326
|
+
"aria-describedby"?: string;
|
|
327
|
+
ref?: RefTarget<HTMLInputElement>;
|
|
328
|
+
onChange?: (value: string) => void;
|
|
329
|
+
onFocus?: (event: FocusEvent) => void;
|
|
330
|
+
onBlur?: (event: FocusEvent) => void;
|
|
331
|
+
onKeyDown?: (event: KeyboardEvent) => void;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* ```tsx
|
|
336
|
+
* <TextField label="Name" onChange={(v) => name.set(v)} />
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
export function TextField(props: TextFieldComponentProps) {
|
|
340
|
+
const inputRef = makeRef<HTMLInputElement>();
|
|
341
|
+
const options = fromProps(cells(props));
|
|
342
|
+
const auto = useAutocompleteInput();
|
|
343
|
+
|
|
344
|
+
const { labelProps, inputProps, descriptionProps, errorMessageProps, errors, isInvalid } =
|
|
345
|
+
textField(
|
|
346
|
+
{
|
|
347
|
+
...(options as TextFieldOptions),
|
|
348
|
+
...queryOf(auto, options as TextFieldOptions),
|
|
349
|
+
validate: callback(cells(props).validate),
|
|
350
|
+
validationBehavior: () => props.validationBehavior,
|
|
351
|
+
},
|
|
352
|
+
inputRef,
|
|
353
|
+
);
|
|
354
|
+
const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
|
|
355
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing({ isTextInput: true });
|
|
356
|
+
|
|
357
|
+
const controlProps = mergeProps(
|
|
358
|
+
inputProps,
|
|
359
|
+
hoverProps,
|
|
360
|
+
focusProps,
|
|
361
|
+
{
|
|
362
|
+
"data-hovered": isHovered,
|
|
363
|
+
"data-focused": isFocused,
|
|
364
|
+
"data-focus-visible": isFocusVisible,
|
|
365
|
+
"data-disabled": () => props.isDisabled === true,
|
|
366
|
+
"data-readonly": () => props.isReadOnly === true,
|
|
367
|
+
"data-required": () => props.isRequired === true,
|
|
368
|
+
"data-invalid": isInvalid,
|
|
369
|
+
},
|
|
370
|
+
auto?.inputProps,
|
|
371
|
+
);
|
|
372
|
+
|
|
373
|
+
return (
|
|
374
|
+
<div
|
|
375
|
+
{...mergeProps(styleProps(cells(props)), {
|
|
376
|
+
"data-testid": () => props["data-testid"],
|
|
377
|
+
})}
|
|
378
|
+
>
|
|
379
|
+
<label {...labelProps}>{props.label}</label>
|
|
380
|
+
{() =>
|
|
381
|
+
props.isMultiLine === true ? (
|
|
382
|
+
<textarea
|
|
383
|
+
{...controlProps}
|
|
384
|
+
ref={
|
|
385
|
+
mergeRefs(inputRef.set, props.ref, auto?.ref) as unknown as (
|
|
386
|
+
el: HTMLTextAreaElement,
|
|
387
|
+
) => void
|
|
388
|
+
}
|
|
389
|
+
/>
|
|
390
|
+
) : (
|
|
391
|
+
<input {...controlProps} ref={mergeRefs(inputRef.set, props.ref, auto?.ref)} />
|
|
392
|
+
)
|
|
393
|
+
}
|
|
394
|
+
<span {...descriptionProps}>{props.description}</span>
|
|
395
|
+
{/* The caller's message when it gave one, and otherwise whatever found
|
|
396
|
+
the problem — which may be the browser, in its own language. */}
|
|
397
|
+
<span {...errorMessageProps}>
|
|
398
|
+
{() => {
|
|
399
|
+
if (!isInvalid()) return null;
|
|
400
|
+
const given = props.errorMessage;
|
|
401
|
+
if (given !== undefined && given !== null) return given;
|
|
402
|
+
return errors().join(" ");
|
|
403
|
+
}}
|
|
404
|
+
</span>
|
|
405
|
+
</div>
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export interface SearchFieldComponentProps extends TextFieldComponentProps {
|
|
410
|
+
onSubmit?: (value: string) => void;
|
|
411
|
+
onClear?: () => void;
|
|
412
|
+
/** What the clear button says. @default "Clear search" */
|
|
413
|
+
clearLabel?: string;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* ```tsx
|
|
418
|
+
* <SearchField label="Search" onSubmit={(q) => search(q)} />
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
export function SearchField(props: SearchFieldComponentProps) {
|
|
422
|
+
const inputRef = makeRef<HTMLInputElement>();
|
|
423
|
+
const options = fromProps(cells(props));
|
|
424
|
+
const auto = useAutocompleteInput();
|
|
425
|
+
|
|
426
|
+
const {
|
|
427
|
+
labelProps,
|
|
428
|
+
inputProps,
|
|
429
|
+
descriptionProps,
|
|
430
|
+
errorMessageProps,
|
|
431
|
+
clearButtonProps,
|
|
432
|
+
value,
|
|
433
|
+
errors,
|
|
434
|
+
isInvalid,
|
|
435
|
+
} = searchField(
|
|
436
|
+
{ ...(options as SearchFieldOptions), ...queryOf(auto, options as SearchFieldOptions) },
|
|
437
|
+
inputRef,
|
|
438
|
+
);
|
|
439
|
+
const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
|
|
440
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing({ isTextInput: true });
|
|
441
|
+
|
|
442
|
+
const controlProps = mergeProps(
|
|
443
|
+
inputProps,
|
|
444
|
+
hoverProps,
|
|
445
|
+
focusProps,
|
|
446
|
+
{
|
|
447
|
+
"data-hovered": isHovered,
|
|
448
|
+
"data-focused": isFocused,
|
|
449
|
+
"data-focus-visible": isFocusVisible,
|
|
450
|
+
"data-disabled": () => props.isDisabled === true,
|
|
451
|
+
"data-readonly": () => props.isReadOnly === true,
|
|
452
|
+
"data-required": () => props.isRequired === true,
|
|
453
|
+
"data-invalid": isInvalid,
|
|
454
|
+
},
|
|
455
|
+
auto?.inputProps,
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
return (
|
|
459
|
+
<div
|
|
460
|
+
{...mergeProps(styleProps(cells(props)), {
|
|
461
|
+
"data-testid": () => props["data-testid"],
|
|
462
|
+
"data-empty": () => value() === "",
|
|
463
|
+
})}
|
|
464
|
+
>
|
|
465
|
+
<label {...labelProps}>{props.label}</label>
|
|
466
|
+
<input {...controlProps} ref={mergeRefs(inputRef.set, props.ref, auto?.ref)} />
|
|
467
|
+
<button type="button" {...clearButtonProps}>
|
|
468
|
+
{() => props.clearLabel ?? "Clear search"}
|
|
469
|
+
</button>
|
|
470
|
+
<span {...descriptionProps}>{props.description}</span>
|
|
471
|
+
{/* The caller's message when it gave one, and otherwise whatever found
|
|
472
|
+
the problem — which may be the browser, in its own language. */}
|
|
473
|
+
<span {...errorMessageProps}>
|
|
474
|
+
{() => {
|
|
475
|
+
if (!isInvalid()) return null;
|
|
476
|
+
const given = props.errorMessage;
|
|
477
|
+
if (given !== undefined && given !== null) return given;
|
|
478
|
+
return errors().join(" ");
|
|
479
|
+
}}
|
|
480
|
+
</span>
|
|
481
|
+
</div>
|
|
482
|
+
);
|
|
483
|
+
}
|