@antadesign/anta 0.3.4 → 0.3.6
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/dist/anta_helpers.d.ts +10 -0
- package/dist/anta_helpers.js +8 -0
- package/dist/components/Calendar.d.ts +3 -2
- package/dist/components/Calendar.js +18 -7
- package/dist/components/Dialog.d.ts +92 -0
- package/dist/components/Dialog.js +71 -0
- package/dist/components/Input.d.ts +3 -0
- package/dist/components/InputAutocomplete.d.ts +69 -0
- package/dist/components/InputAutocomplete.js +95 -0
- package/dist/components/InputDate.d.ts +3 -2
- package/dist/components/InputDate.js +36 -91
- package/dist/components/InputDate.module.css +1 -1
- package/dist/components/InputTime.d.ts +105 -0
- package/dist/components/InputTime.js +110 -0
- package/dist/components/Menu.d.ts +9 -1
- package/dist/components/Menu.js +4 -1
- package/dist/components/MenuItem.d.ts +9 -1
- package/dist/components/MenuItem.js +2 -1
- package/dist/components/Select.d.ts +29 -8
- package/dist/components/Select.js +23 -30
- package/dist/components/Select.module.css +1 -1
- package/dist/components/SelectFaceted.d.ts +190 -0
- package/dist/components/SelectFaceted.js +332 -0
- package/dist/components/SelectFaceted.module.css +1 -0
- package/dist/components/Tabs.d.ts +6 -0
- package/dist/components/Tabs.js +3 -1
- package/dist/components/select-options.d.ts +14 -0
- package/dist/components/select-options.js +31 -0
- package/dist/elements/a-button.css +1 -1
- package/dist/elements/a-calendar.css +1 -1
- package/dist/elements/a-dialog.css +1 -0
- package/dist/elements/a-dialog.d.ts +26 -0
- package/dist/elements/a-dialog.js +386 -0
- package/dist/elements/a-expander.js +2 -2
- package/dist/elements/a-input-time.css +1 -0
- package/dist/elements/a-input-time.d.ts +27 -0
- package/dist/elements/a-input-time.js +857 -0
- package/dist/elements/a-input.css +1 -1
- package/dist/elements/a-input.js +7 -1
- package/dist/elements/a-menu-item.css +1 -1
- package/dist/elements/a-menu.d.ts +5 -2
- package/dist/elements/a-menu.js +42 -7
- package/dist/elements/a-tab.css +1 -1
- package/dist/elements/a-tabs.css +1 -1
- package/dist/elements/a-tooltip.d.ts +17 -0
- package/dist/elements/a-tooltip.js +29 -1
- package/dist/elements/index.d.ts +2 -0
- package/dist/elements/index.js +6 -0
- package/dist/general_types.d.ts +97 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/jsx-runtime.d.ts +3 -1
- package/dist/reset.css +1 -1
- package/dist/tokens.css +1 -1
- package/package.json +1 -1
package/dist/anta_helpers.d.ts
CHANGED
|
@@ -22,6 +22,16 @@ export declare function nativeStateChange<D>(e: CustomEvent<D> | {
|
|
|
22
22
|
event: CustomEvent<D>;
|
|
23
23
|
detail?: D;
|
|
24
24
|
};
|
|
25
|
+
export declare const IS_MAC: boolean;
|
|
26
|
+
/** Default row hint teaching the Alt/⌥-click "select only this" accelerator on
|
|
27
|
+
* bulk-select rows (`Select` multiple, `SelectFaceted` multiple facets). Shared
|
|
28
|
+
* so the wording stays identical across both. */
|
|
29
|
+
export declare const ISOLATE_HINT: string;
|
|
30
|
+
/** Parse an open/closed `state` / `default-state` attribute — the shared
|
|
31
|
+
* open-state vocabulary (`a-dialog`, `a-expander`, …). Anything but the literal
|
|
32
|
+
* `'open'` is `'closed'`, so an absent / malformed attribute reads closed. Kept
|
|
33
|
+
* here so the two elements can't drift on how the token is parsed. */
|
|
34
|
+
export declare function parseOpenState(v: string | null): 'open' | 'closed';
|
|
25
35
|
/** The six named tones every toned component shares. Anything else is a literal
|
|
26
36
|
* CSS colour the element resolves through its `--{component}-tone-source` var. */
|
|
27
37
|
export declare const NAMED_TONES: Set<string>;
|
package/dist/anta_helpers.js
CHANGED
|
@@ -16,6 +16,11 @@ function nativeStateChange(e) {
|
|
|
16
16
|
const event = "nativeEvent" in e ? e.nativeEvent : e;
|
|
17
17
|
return { event, detail: event?.detail };
|
|
18
18
|
}
|
|
19
|
+
const IS_MAC = typeof navigator !== "undefined" && /Mac|iPhone|iPad/i.test(navigator.userAgent || "");
|
|
20
|
+
const ISOLATE_HINT = IS_MAC ? "\u2325+Click to select only this" : "Alt+Click to select only this";
|
|
21
|
+
function parseOpenState(v) {
|
|
22
|
+
return v === "open" ? "open" : "closed";
|
|
23
|
+
}
|
|
19
24
|
const NAMED_TONES = /* @__PURE__ */ new Set([
|
|
20
25
|
"brand",
|
|
21
26
|
"neutral",
|
|
@@ -105,6 +110,8 @@ class SelectableChildElement extends HTMLElementBase {
|
|
|
105
110
|
}
|
|
106
111
|
export {
|
|
107
112
|
HTMLElementBase,
|
|
113
|
+
ISOLATE_HINT,
|
|
114
|
+
IS_MAC,
|
|
108
115
|
NAMED_TONES,
|
|
109
116
|
SelectableChildElement,
|
|
110
117
|
anchorRect,
|
|
@@ -112,6 +119,7 @@ export {
|
|
|
112
119
|
isInsideOpenMenu,
|
|
113
120
|
isMenuOpen,
|
|
114
121
|
nativeStateChange,
|
|
122
|
+
parseOpenState,
|
|
115
123
|
roundStyle,
|
|
116
124
|
setMenuPresence,
|
|
117
125
|
toneStyle,
|
|
@@ -61,8 +61,9 @@ export interface CalendarProps extends Omit<BaseProps, "children" | "onChange">
|
|
|
61
61
|
* `<Calendar>` — a single-date month grid you pick a day from, **composed from
|
|
62
62
|
* Anta components in light DOM** (nothing is hidden in a shadow root): days and
|
|
63
63
|
* the prev/next chevrons are `<Button>`s, so they inherit the design system's
|
|
64
|
-
* states for free — a selected day is literally a `
|
|
65
|
-
* `selected` state
|
|
64
|
+
* states for free — a selected day is literally a `tertiary` Button in its
|
|
65
|
+
* `selected` state, toned `brand` so the active date reads in the brand color.
|
|
66
|
+
* All date math runs on the Temporal engine
|
|
66
67
|
* (`@antadesign/anta` exports `buildMonth` & friends), and the value is an ISO
|
|
67
68
|
* `YYYY-MM-DD` string.
|
|
68
69
|
*
|
|
@@ -38,11 +38,21 @@ const Calendar = ({
|
|
|
38
38
|
const [cursor, setCursor] = useState(
|
|
39
39
|
() => clampDate(parseISODate(value ?? defaultValue) ?? today, minD, maxD)
|
|
40
40
|
);
|
|
41
|
+
const [lastSyncedValue, setLastSyncedValue] = useState(value);
|
|
42
|
+
let effectiveCursor = cursor;
|
|
43
|
+
if (value !== lastSyncedValue) {
|
|
44
|
+
setLastSyncedValue(value);
|
|
45
|
+
const d = parseISODate(value);
|
|
46
|
+
if (d) {
|
|
47
|
+
effectiveCursor = clampDate(d, minD, maxD);
|
|
48
|
+
setCursor(effectiveCursor);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
41
51
|
const [focusReq, setFocusReq] = useState(null);
|
|
42
52
|
const [jumpOpen, setJumpOpen] = useState(false);
|
|
43
53
|
const headingId = useId();
|
|
44
|
-
const month = buildMonth({ anchor:
|
|
45
|
-
const cursorIso =
|
|
54
|
+
const month = buildMonth({ anchor: effectiveCursor, locale: resolvedLocale, min: minD, max: maxD, selected, today });
|
|
55
|
+
const cursorIso = effectiveCursor.toString();
|
|
46
56
|
const [lastFocusSignal, setLastFocusSignal] = useState(focusSignal);
|
|
47
57
|
if (focusSignal !== lastFocusSignal) {
|
|
48
58
|
setLastFocusSignal(focusSignal);
|
|
@@ -73,24 +83,25 @@ const Calendar = ({
|
|
|
73
83
|
const jumpItems = useMemo(() => {
|
|
74
84
|
const monthItem = (y, i) => {
|
|
75
85
|
const m = i + 1;
|
|
86
|
+
const isCurrent = y === cursor.year && m === cursor.month;
|
|
76
87
|
return /* @__PURE__ */ jsx(
|
|
77
88
|
MenuItem,
|
|
78
89
|
{
|
|
79
90
|
label: monthNames[i],
|
|
80
|
-
|
|
81
|
-
selected: y === cursor.year && m === cursor.month,
|
|
91
|
+
selected: isCurrent,
|
|
82
92
|
disabled: monthOutOfRange(y, m) || void 0,
|
|
83
93
|
"data-menu-open": "",
|
|
84
94
|
onSelect: () => {
|
|
85
95
|
pickMonth(y, m);
|
|
86
96
|
setJumpOpen(false);
|
|
87
|
-
}
|
|
97
|
+
},
|
|
98
|
+
children: isCurrent && /* @__PURE__ */ jsx("span", { className: "jump-dot", "aria-hidden": "true" })
|
|
88
99
|
},
|
|
89
100
|
`${y}-${m}`
|
|
90
101
|
);
|
|
91
102
|
};
|
|
92
103
|
return years.length === 1 ? monthNames.map((_, i) => monthItem(years[0], i)) : years.map((y) => /* @__PURE__ */ jsxs(MenuItem, { submenu: true, label: String(y), selected: y === cursor.year, children: [
|
|
93
|
-
y === cursor.year && /* @__PURE__ */ jsx("span", { className: "
|
|
104
|
+
y === cursor.year && /* @__PURE__ */ jsx("span", { className: "jump-dot", "aria-hidden": "true" }),
|
|
94
105
|
/* @__PURE__ */ jsx(Menu, { children: monthNames.map((_, i) => monthItem(y, i)) })
|
|
95
106
|
] }, y));
|
|
96
107
|
}, [monthNames, cursor.year, cursor.month, minYear, maxYear, min, max, disabled]);
|
|
@@ -192,7 +203,7 @@ const Calendar = ({
|
|
|
192
203
|
`wd${i}`
|
|
193
204
|
)),
|
|
194
205
|
month.weeks.flat().map((d) => {
|
|
195
|
-
const variant = d.today && !d.selected ? { priority: "secondary" } : { priority: "tertiary", selected: d.selected };
|
|
206
|
+
const variant = d.today && !d.selected ? { priority: "secondary" } : { priority: "tertiary", selected: d.selected, tone: d.selected ? "brand" : void 0 };
|
|
196
207
|
return /* @__PURE__ */ jsxs(
|
|
197
208
|
Button,
|
|
198
209
|
{
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { BaseProps } from "../general_types";
|
|
2
|
+
/** Public props for the `<Dialog>` modal / drawer. `header` and `footer` are the
|
|
3
|
+
* chrome zones; `children` is the (scrollable) body. */
|
|
4
|
+
export interface DialogProps extends Omit<BaseProps, "title"> {
|
|
5
|
+
/** Header content: a title, or any node. Rendered in the top zone. Omit for a
|
|
6
|
+
* chromeless dialog with only a body. */
|
|
7
|
+
header?: React.ReactNode;
|
|
8
|
+
/** Footer content, usually the action buttons. Rendered in the bottom zone as
|
|
9
|
+
* a right-aligned row (wraps under pressure). Omit for none. */
|
|
10
|
+
footer?: React.ReactNode;
|
|
11
|
+
/** The dialog body. Scrolls when it overflows the available height. */
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
/** Placement. `center` (the default) is a centered modal; `left` / `right` /
|
|
14
|
+
* `top` / `bottom` turn it into an edge drawer (full height for left/right,
|
|
15
|
+
* full width for top/bottom); `fullscreen` fills the whole viewport with no
|
|
16
|
+
* edge gap or corner radius.
|
|
17
|
+
* @defaultValue 'center' */
|
|
18
|
+
position?: "center" | "left" | "right" | "top" | "bottom" | "fullscreen";
|
|
19
|
+
/** Whether the top-right ✕ button is present. It's one way to close the dialog,
|
|
20
|
+
* alongside Esc, the backdrop, a `data-dialog-close` / footer action, and your
|
|
21
|
+
* own code; `false` just removes the ✕, it doesn't make the dialog un-closable.
|
|
22
|
+
* @defaultValue true */
|
|
23
|
+
closable?: boolean;
|
|
24
|
+
/** Turn off light-dismiss: a backdrop click and Esc no longer close the dialog.
|
|
25
|
+
* It stays closable through explicit controls (the ✕ when `closable`, a footer
|
|
26
|
+
* action, your own code); `persistent` isn't "un-closable", it stops an
|
|
27
|
+
* accidental click or stray Esc from dismissing. For an alert / confirm that
|
|
28
|
+
* should be answered deliberately. Omit for the default dismissible behavior. */
|
|
29
|
+
persistent?: boolean;
|
|
30
|
+
/** Corner radius. A side keyword — `'top'` / `'right'` / `'bottom'` / `'left'` —
|
|
31
|
+
* rounds only that edge's two corners (at `--dialog-radius`), which pairs with
|
|
32
|
+
* a drawer's exposed edge (a bottom sheet → `'top'`, a right drawer → `'left'`).
|
|
33
|
+
* A `number` (px) or CSS string rounds all corners at that value; the string
|
|
34
|
+
* may be a full `border-radius` shorthand (`'12px 12px 0 0'`). `true` rounds all
|
|
35
|
+
* corners at `--dialog-radius`. Omit for the position default: `center` is
|
|
36
|
+
* rounded, drawers and `fullscreen` are square. */
|
|
37
|
+
round?: boolean | number | "top" | "right" | "bottom" | "left" | (string & {});
|
|
38
|
+
/** Controlled open state. When provided, the consumer owns open/close: the
|
|
39
|
+
* dialog only follows this prop, and every user dismiss (Esc, backdrop, close
|
|
40
|
+
* button) *requests* a change via `onStateChange` (reject by not updating).
|
|
41
|
+
* Leave undefined for uncontrolled. */
|
|
42
|
+
open?: boolean;
|
|
43
|
+
/** Initial open state for the uncontrolled case (read once on mount). */
|
|
44
|
+
defaultOpen?: boolean;
|
|
45
|
+
/** Uncontrolled trigger name. Any element with `data-dialog-open="{name}"`
|
|
46
|
+
* opens this dialog, `data-dialog-close="{name}"` closes it. A convenience for
|
|
47
|
+
* triggers rendered elsewhere; ignored in controlled mode. */
|
|
48
|
+
name?: string;
|
|
49
|
+
/** Fired before the open state changes, on every user open or dismiss.
|
|
50
|
+
* `event` is the cancelable `statechange`: call `event.preventDefault()` to
|
|
51
|
+
* veto an *uncontrolled* transition. `detail.next` is the requested open
|
|
52
|
+
* state, `detail.prev` the current one (booleans). In controlled mode, apply
|
|
53
|
+
* `detail.next` to `open` to accept, or do nothing to reject. */
|
|
54
|
+
onStateChange?: (event: CustomEvent, detail: {
|
|
55
|
+
next: boolean;
|
|
56
|
+
prev: boolean;
|
|
57
|
+
}) => void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* `<Dialog>` — a modal dialog (or edge drawer, via `position`), built on a
|
|
61
|
+
* native `<dialog>` inside `<a-dialog>` — so the top layer, focus trap, focus
|
|
62
|
+
* return, backdrop, and Esc all come from the platform.
|
|
63
|
+
*
|
|
64
|
+
* A pure, stateless pass-through: the element owns all interaction (open/close,
|
|
65
|
+
* dismiss, focus), so the wrapper holds no state and grabs no ref — it only maps
|
|
66
|
+
* props to attributes and drops content into the `header` / body / `footer` /
|
|
67
|
+
* `close` slots.
|
|
68
|
+
*
|
|
69
|
+
* Uncontrolled (`defaultOpen`, or the `name` trigger convenience): the element
|
|
70
|
+
* owns open/close. Controlled (`open` + `onStateChange`): the wrapper emits
|
|
71
|
+
* `state="open"|"closed"`; the element treats the attribute as the source of
|
|
72
|
+
* truth, and every user dismiss only *requests* a change (a consumer can reject
|
|
73
|
+
* it). See STATEFUL-COMPONENTS.md.
|
|
74
|
+
*
|
|
75
|
+
* Requires `@antadesign/anta/elements` to be imported (client-side only) to
|
|
76
|
+
* register the underlying custom element.
|
|
77
|
+
*
|
|
78
|
+
* @example Controlled
|
|
79
|
+
* ```tsx
|
|
80
|
+
* const [open, setOpen] = useState(false)
|
|
81
|
+
* <Button onClick={() => setOpen(true)}>Open</Button>
|
|
82
|
+
* <Dialog
|
|
83
|
+
* open={open}
|
|
84
|
+
* onStateChange={(_e, { next }) => setOpen(next)}
|
|
85
|
+
* header="Delete project?"
|
|
86
|
+
* footer={<><Button onClick={() => setOpen(false)}>Cancel</Button><Button priority="primary" tone="critical">Delete</Button></>}
|
|
87
|
+
* >
|
|
88
|
+
* This action cannot be undone.
|
|
89
|
+
* </Dialog>
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare const Dialog: ({ header, footer, position, closable, persistent, round, open, defaultOpen, name, onStateChange, className, style, children, ...rest }: DialogProps) => any;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { jsx, jsxs } from "@antadesign/anta/jsx-runtime";
|
|
2
|
+
import { nativeStateChange, roundStyle } from "../anta_helpers";
|
|
3
|
+
import { Button } from "./Button";
|
|
4
|
+
const ROUND_SIDES = /* @__PURE__ */ new Set(["top", "right", "bottom", "left"]);
|
|
5
|
+
const Dialog = ({
|
|
6
|
+
header,
|
|
7
|
+
footer,
|
|
8
|
+
position,
|
|
9
|
+
closable,
|
|
10
|
+
persistent,
|
|
11
|
+
round,
|
|
12
|
+
open,
|
|
13
|
+
defaultOpen,
|
|
14
|
+
name,
|
|
15
|
+
onStateChange,
|
|
16
|
+
className,
|
|
17
|
+
style,
|
|
18
|
+
children,
|
|
19
|
+
...rest
|
|
20
|
+
}) => {
|
|
21
|
+
const controlled = open !== void 0;
|
|
22
|
+
const roundSide = typeof round === "string" && ROUND_SIDES.has(round) ? round : void 0;
|
|
23
|
+
return /* @__PURE__ */ jsxs(
|
|
24
|
+
"a-dialog",
|
|
25
|
+
{
|
|
26
|
+
state: controlled ? open ? "open" : "closed" : void 0,
|
|
27
|
+
"default-state": !controlled && defaultOpen ? "open" : void 0,
|
|
28
|
+
position: position && position !== "center" ? position : void 0,
|
|
29
|
+
persistent: persistent ? "" : void 0,
|
|
30
|
+
round: roundSide ?? (round ? "" : void 0),
|
|
31
|
+
name: !controlled ? name : void 0,
|
|
32
|
+
onstatechange: onStateChange ? (e) => {
|
|
33
|
+
const { event, detail } = nativeStateChange(e);
|
|
34
|
+
if (detail)
|
|
35
|
+
onStateChange(event, {
|
|
36
|
+
next: detail.next === "open",
|
|
37
|
+
prev: detail.prev === "open"
|
|
38
|
+
});
|
|
39
|
+
} : void 0,
|
|
40
|
+
class: className,
|
|
41
|
+
style: roundStyle(roundSide ? void 0 : round, "--dialog-round", style),
|
|
42
|
+
...rest,
|
|
43
|
+
children: [
|
|
44
|
+
closable !== false && // A real <a-button> (light DOM → fully styled, keyboard-focusable) in the
|
|
45
|
+
// element's `close` slot. It fires the bubbling `closerequest` event via
|
|
46
|
+
// a-button's global listener, so closing works even without framework
|
|
47
|
+
// hydration; the element turns that into a close request.
|
|
48
|
+
// CONTRACT: `data-custom-event` below MUST match `CLOSE_TRIGGER` in the
|
|
49
|
+
// element (src/elements/a-dialog.ts). Duplicated, not shared — importing
|
|
50
|
+
// the element module here would self-register it and break the
|
|
51
|
+
// wrapper/element decoupling. Rename in both places.
|
|
52
|
+
/* @__PURE__ */ jsx("span", { slot: "close", style: { display: "contents" }, children: /* @__PURE__ */ jsx(
|
|
53
|
+
Button,
|
|
54
|
+
{
|
|
55
|
+
priority: "tertiary",
|
|
56
|
+
size: "large",
|
|
57
|
+
icon: "x",
|
|
58
|
+
"aria-label": "Close",
|
|
59
|
+
"data-custom-event": "closerequest"
|
|
60
|
+
}
|
|
61
|
+
) }),
|
|
62
|
+
header != null && /* @__PURE__ */ jsx("div", { slot: "header", children: header }),
|
|
63
|
+
children,
|
|
64
|
+
footer != null && /* @__PURE__ */ jsx("span", { slot: "footer", style: { display: "contents" }, children: footer })
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
export {
|
|
70
|
+
Dialog
|
|
71
|
+
};
|
|
@@ -146,6 +146,9 @@ export interface InputProps extends BaseProps, DOMEventHandlers {
|
|
|
146
146
|
onFocus?: (e: any) => void;
|
|
147
147
|
/** Fires when the field loses focus. */
|
|
148
148
|
onBlur?: (e: any) => void;
|
|
149
|
+
/** ARIA `role` for the field — e.g. `combobox` when the input drives a
|
|
150
|
+
* suggestion `listbox` (see `InputAutocomplete`). Left unset by default. */
|
|
151
|
+
role?: string;
|
|
149
152
|
}
|
|
150
153
|
/**
|
|
151
154
|
* `<Input>` — a text field. Renders an `<a-input>` web component whose real
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { BaseProps } from '../general_types';
|
|
2
|
+
import type { IconShape } from '../elements/a-icon.shapes';
|
|
3
|
+
import type { SelectOption } from './Select';
|
|
4
|
+
export interface InputAutocompleteProps extends Omit<BaseProps, 'children'> {
|
|
5
|
+
/** The suggestions: bare strings or `SelectOption`s (`value` / `label` / `hint` /
|
|
6
|
+
* `icon` / `tone` / `disabled`). A flat list; picking one fills the field. */
|
|
7
|
+
suggestions: (string | SelectOption)[];
|
|
8
|
+
/** Controlled value: the field's text, a **free** string not constrained to a
|
|
9
|
+
* suggestion. Leave undefined for uncontrolled. */
|
|
10
|
+
value?: string;
|
|
11
|
+
/** Initial value for the uncontrolled case (the wrapper then owns it). */
|
|
12
|
+
defaultValue?: string;
|
|
13
|
+
/** Fires as the text changes (typing, picking a suggestion, or clearing) with
|
|
14
|
+
* the new field text. */
|
|
15
|
+
onValueChange?: (value: string) => void;
|
|
16
|
+
/** Fires only when a suggestion is chosen, with that option. */
|
|
17
|
+
onSelect?: (option: SelectOption) => void;
|
|
18
|
+
/** How suggestions match the text. `true` uses the built-in case-insensitive
|
|
19
|
+
* substring match on `value` / `label` / `hint`; a function `(option, query) =>
|
|
20
|
+
* boolean` does custom matching; `false` shows `suggestions` verbatim so you
|
|
21
|
+
* can filter them yourself (async / remote).
|
|
22
|
+
* @defaultValue true */
|
|
23
|
+
filter?: boolean | ((option: SelectOption, query: string) => boolean);
|
|
24
|
+
/** Placeholder text. */
|
|
25
|
+
placeholder?: string;
|
|
26
|
+
/** Field size.
|
|
27
|
+
* @defaultValue medium */
|
|
28
|
+
size?: 'small' | 'medium' | 'large';
|
|
29
|
+
/** Validation status. */
|
|
30
|
+
status?: 'neutral' | 'brand' | 'info' | 'success' | 'warning' | 'critical';
|
|
31
|
+
/** Status icon override, or `false` to drop it. */
|
|
32
|
+
statusIcon?: IconShape | (string & {}) | false;
|
|
33
|
+
/** Custom accent colour — a named tone or any CSS colour. */
|
|
34
|
+
tone?: string;
|
|
35
|
+
/** Show a clear button while the field has text. */
|
|
36
|
+
clearable?: boolean;
|
|
37
|
+
/** Form field name. */
|
|
38
|
+
name?: string;
|
|
39
|
+
/** Disable the field. */
|
|
40
|
+
disabled?: boolean;
|
|
41
|
+
/** Field label. */
|
|
42
|
+
label?: React.ReactNode;
|
|
43
|
+
/** Hint under the field. */
|
|
44
|
+
hint?: React.ReactNode;
|
|
45
|
+
/** Leading adornment inside the field. */
|
|
46
|
+
leading?: React.ReactNode;
|
|
47
|
+
/** Trailing adornment inside the field. */
|
|
48
|
+
trailing?: React.ReactNode;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* `<InputAutocomplete>` — a text field whose value is a free string, with a
|
|
52
|
+
* suggestion dropdown that assists without constraining. Typing something not in
|
|
53
|
+
* the list is valid and stays the value; picking a suggestion just fills the
|
|
54
|
+
* field. For a value that MUST be one of the options, use `Select` with `filter`.
|
|
55
|
+
*
|
|
56
|
+
* Controlled (`value` + `onValueChange`) or uncontrolled (`defaultValue`).
|
|
57
|
+
* Requires `@antadesign/anta/elements` (client-side only).
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```tsx
|
|
61
|
+
* <InputAutocomplete
|
|
62
|
+
* label="City"
|
|
63
|
+
* suggestions={['Berlin', 'Bogotá', 'Boston']}
|
|
64
|
+
* value={city}
|
|
65
|
+
* onValueChange={setCity}
|
|
66
|
+
* />
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare const InputAutocomplete: (props: InputAutocompleteProps) => any;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Fragment, jsx, jsxs } from "@antadesign/anta/jsx-runtime";
|
|
2
|
+
import { useState, useMemo, useId } from "../jsx-runtime";
|
|
3
|
+
import { nativeStateChange } from "../anta_helpers";
|
|
4
|
+
import { normalizeOpt, matchQueryRegex, matchesQuery, highlight } from "./select-options";
|
|
5
|
+
import { Input } from "./Input";
|
|
6
|
+
import { Menu } from "./Menu";
|
|
7
|
+
import { MenuItem } from "./MenuItem";
|
|
8
|
+
const InputAutocomplete = (props) => {
|
|
9
|
+
const {
|
|
10
|
+
suggestions,
|
|
11
|
+
value,
|
|
12
|
+
defaultValue,
|
|
13
|
+
onValueChange,
|
|
14
|
+
onSelect,
|
|
15
|
+
filter = true,
|
|
16
|
+
...rest
|
|
17
|
+
} = props;
|
|
18
|
+
const rid = useId();
|
|
19
|
+
const controlled = value !== void 0;
|
|
20
|
+
const [internal, setInternal] = useState(defaultValue ?? "");
|
|
21
|
+
const text = controlled ? value : internal;
|
|
22
|
+
const [open, setOpen] = useState(false);
|
|
23
|
+
const [activeId, setActiveId] = useState(null);
|
|
24
|
+
const options = useMemo(() => suggestions.map(normalizeOpt), [suggestions]);
|
|
25
|
+
const custom = typeof filter === "function";
|
|
26
|
+
const queryRe = filter !== false && !custom ? matchQueryRegex(text) : null;
|
|
27
|
+
const visible = custom ? options.filter((o) => filter(o, text)) : options.filter((o) => matchesQuery(o, queryRe));
|
|
28
|
+
const menuOpen = open && visible.length > 0;
|
|
29
|
+
const setText = (next) => {
|
|
30
|
+
if (!controlled) setInternal(next);
|
|
31
|
+
onValueChange?.(next);
|
|
32
|
+
};
|
|
33
|
+
const pick = (opt) => {
|
|
34
|
+
setText(opt.value);
|
|
35
|
+
onSelect?.(opt);
|
|
36
|
+
setOpen(false);
|
|
37
|
+
};
|
|
38
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
39
|
+
/* @__PURE__ */ jsx(
|
|
40
|
+
Input,
|
|
41
|
+
{
|
|
42
|
+
...rest,
|
|
43
|
+
value: text,
|
|
44
|
+
"data-menu-search": "",
|
|
45
|
+
role: "combobox",
|
|
46
|
+
"aria-haspopup": "listbox",
|
|
47
|
+
"aria-controls": `${rid}-list`,
|
|
48
|
+
"aria-expanded": menuOpen ? "true" : "false",
|
|
49
|
+
"aria-autocomplete": "list",
|
|
50
|
+
"aria-activedescendant": menuOpen && activeId ? activeId : void 0,
|
|
51
|
+
onFocus: () => setOpen(true),
|
|
52
|
+
onInput: (e) => {
|
|
53
|
+
setText(e.currentTarget.value);
|
|
54
|
+
setOpen(true);
|
|
55
|
+
},
|
|
56
|
+
onKeyDown: (e) => {
|
|
57
|
+
if (e.key === "Enter") setOpen(false);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
),
|
|
61
|
+
/* @__PURE__ */ jsx(
|
|
62
|
+
Menu,
|
|
63
|
+
{
|
|
64
|
+
id: `${rid}-list`,
|
|
65
|
+
role: "listbox",
|
|
66
|
+
open: menuOpen,
|
|
67
|
+
onStateChange: (_e, { next }) => {
|
|
68
|
+
setOpen(next);
|
|
69
|
+
if (!next) setActiveId(null);
|
|
70
|
+
},
|
|
71
|
+
onactivedescendant: (e) => setActiveId(nativeStateChange(e).detail?.id ?? null),
|
|
72
|
+
children: visible.map((opt, i) => /* @__PURE__ */ jsx(
|
|
73
|
+
MenuItem,
|
|
74
|
+
{
|
|
75
|
+
id: `${rid}-o${i}`,
|
|
76
|
+
role: "option",
|
|
77
|
+
"aria-selected": activeId === `${rid}-o${i}` ? "true" : "false",
|
|
78
|
+
icon: opt.icon,
|
|
79
|
+
label: highlight(opt.label ?? opt.value, queryRe),
|
|
80
|
+
hint: opt.hint ? highlight(opt.hint, queryRe) : opt.hint,
|
|
81
|
+
tone: opt.tone,
|
|
82
|
+
disabled: opt.disabled,
|
|
83
|
+
value: opt.value,
|
|
84
|
+
onMouseDown: (e) => e.preventDefault(),
|
|
85
|
+
onSelect: () => pick(opt)
|
|
86
|
+
},
|
|
87
|
+
`${rid}-${opt.value}-${i}`
|
|
88
|
+
))
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
] });
|
|
92
|
+
};
|
|
93
|
+
export {
|
|
94
|
+
InputAutocomplete
|
|
95
|
+
};
|
|
@@ -45,9 +45,10 @@ export interface InputDateProps extends Omit<BaseProps, 'children'> {
|
|
|
45
45
|
round?: boolean | number | string;
|
|
46
46
|
/** Show a clear button once the field has a value. */
|
|
47
47
|
clearable?: boolean;
|
|
48
|
-
/** Leading icon at the start of the field — the calendar affordance.
|
|
48
|
+
/** Leading icon at the start of the field — the calendar affordance. Pass
|
|
49
|
+
* another shape to change it, or `false` to drop it.
|
|
49
50
|
* @defaultValue calendar-days */
|
|
50
|
-
icon?: IconShape;
|
|
51
|
+
icon?: IconShape | false;
|
|
51
52
|
/** Include a time. The value becomes ISO `YYYY-MM-DDTHH:mm`, the field parses a
|
|
52
53
|
* trailing time after a space (`06/15/2026 14:30`, `… 2:30pm`), and the menu
|
|
53
54
|
* shows a time row (hours : minutes, an AM/PM toggle in 12-hour locales, then a
|