@jirawatpyk/aura-react 4.4.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.
@@ -0,0 +1,29 @@
1
+ 'use client';
2
+ import * as React from "react";
3
+ import { cx, omit } from "./internal.js";
4
+ import { Icon } from "./Icon.js";
5
+ const h = React.createElement;
6
+ const Button = React.forwardRef(function Button2(props, ref) {
7
+ var variant = props.variant || "primary";
8
+ var loading = !!props.loading;
9
+ var rest = omit(props, ["variant", "className", "children", "type", "icon", "iconRight", "loading", "onClick"]);
10
+ return h(
11
+ "button",
12
+ Object.assign({}, rest, {
13
+ ref,
14
+ type: props.type || "button",
15
+ className: cx("aura-btn", "aura-btn--" + variant, loading && "is-loading", props.className),
16
+ "aria-busy": loading || void 0,
17
+ "aria-disabled": loading || void 0,
18
+ onClick: loading ? function(e) {
19
+ e.preventDefault();
20
+ } : props.onClick
21
+ }),
22
+ loading ? h(Icon, { name: "loader-circle", className: "aura-spin" }) : props.icon ? h(Icon, { name: props.icon }) : null,
23
+ props.children,
24
+ props.iconRight && !loading ? h(Icon, { name: props.iconRight }) : null
25
+ );
26
+ });
27
+ export {
28
+ Button
29
+ };
@@ -0,0 +1,49 @@
1
+ 'use client';
2
+ import * as React from "react";
3
+ import { cx, omit, useMergedRef, useMaybeControlled } from "./internal.js";
4
+ import { Icon } from "./Icon.js";
5
+ const h = React.createElement;
6
+ const Checkbox = React.forwardRef(function Checkbox2(props, ref) {
7
+ var own = React.useRef(null), merged = useMergedRef(ref, own);
8
+ var st = useMaybeControlled(props.checked, !!props.defaultChecked, props.onChange);
9
+ var on = !!st[0];
10
+ React.useEffect(function() {
11
+ if (own.current) own.current.indeterminate = !!props.indeterminate;
12
+ });
13
+ var rest = omit(props, ["checked", "defaultChecked", "onChange", "indeterminate", "label", "children", "description", "className", "tabIndex", "disabled"]);
14
+ var labelled = props.children != null;
15
+ var descId = props.description && props.id ? props.id + "-desc" : void 0;
16
+ return h(
17
+ "label",
18
+ { className: cx("aura-check", labelled && "aura-check--labelled", props.disabled && "is-disabled", props.className), onClick: function(e) {
19
+ e.stopPropagation();
20
+ } },
21
+ h("input", Object.assign({}, rest, {
22
+ ref: merged,
23
+ type: "checkbox",
24
+ className: "aura-check__input",
25
+ checked: on,
26
+ tabIndex: props.tabIndex,
27
+ disabled: props.disabled,
28
+ "aria-describedby": descId,
29
+ "aria-label": labelled ? void 0 : props.label,
30
+ onChange: function(e) {
31
+ st[1](e.target.checked);
32
+ }
33
+ })),
34
+ h(
35
+ "span",
36
+ { className: "aura-check__box", "aria-hidden": true },
37
+ props.indeterminate ? h(Icon, { name: "minus", size: 12, strokeWidth: 3 }) : on ? h(Icon, { name: "check", size: 12, strokeWidth: 3 }) : null
38
+ ),
39
+ labelled ? h(
40
+ "span",
41
+ { className: "aura-check__text" },
42
+ h("span", { className: "aura-check__label" }, props.children),
43
+ props.description ? h("span", { className: "aura-check__desc", id: descId }, props.description) : null
44
+ ) : null
45
+ );
46
+ });
47
+ export {
48
+ Checkbox
49
+ };
@@ -0,0 +1,257 @@
1
+ 'use client';
2
+ import * as React from "react";
3
+ import { useStrings, useAuraLocale } from "./locale.js";
4
+ import { createPortal } from "react-dom";
5
+ import { cx, uid, useMaybeControlled, useMounted, useIsoLayoutEffect, useMergedRef } from "./internal.js";
6
+ import { Icon } from "./Icon.js";
7
+ import { Field } from "./forms.js";
8
+ const h = React.createElement;
9
+ function norm(s) {
10
+ return String(s == null ? "" : s).normalize("NFC").toLocaleLowerCase("th");
11
+ }
12
+ function toOpt(o) {
13
+ return typeof o === "object" ? o : { value: String(o), label: String(o) };
14
+ }
15
+ function defaultFilter(option, query) {
16
+ var q = norm(query).trim();
17
+ if (!q) return true;
18
+ return norm(option.label).indexOf(q) >= 0 || option.description && norm(option.description).indexOf(q) >= 0 || (option.keywords || []).some(function(k) {
19
+ return norm(k).indexOf(q) >= 0;
20
+ });
21
+ }
22
+ const Combobox = React.forwardRef(function Combobox2(props, ref) {
23
+ var t = useStrings();
24
+ var auto = uid(), id = props.id || auto, listId = id + "-list";
25
+ var options = (props.options || []).map(toOpt);
26
+ var st = useMaybeControlled(props.value, props.defaultValue == null ? null : props.defaultValue, props.onChange);
27
+ var value = st[0], setValue = st[1];
28
+ var selected = options.filter(function(o) {
29
+ return o.value === value;
30
+ })[0] || null;
31
+ var openState = React.useState(false), open = openState[0], setOpen = openState[1];
32
+ var qState = React.useState(null), query = qState[0], setQuery = qState[1];
33
+ var aState = React.useState(0), active = aState[0], setActive = aState[1];
34
+ var posState = React.useState(null);
35
+ var inputRef = React.useRef(null), inputMerged = useMergedRef(ref, inputRef), boxRef = React.useRef(null), listRef = React.useRef(null);
36
+ var mounted = useMounted();
37
+ var limit = props.limit || 200;
38
+ var filter = props.filter || defaultFilter;
39
+ var shown = props.onSearch || query == null ? options : options.filter(function(o) {
40
+ return filter(o, query);
41
+ });
42
+ var more = shown.length > limit;
43
+ shown = shown.slice(0, limit);
44
+ var activeIdx = Math.min(active, shown.length - 1);
45
+ function place() {
46
+ if (!boxRef.current) return;
47
+ var r = boxRef.current.getBoundingClientRect();
48
+ var maxH = 320, below = window.innerHeight - r.bottom - 8, up = below < 200 && r.top > below;
49
+ posState[1]({ left: r.left, width: r.width, top: up ? void 0 : r.bottom + 4, bottom: up ? window.innerHeight - r.top + 4 : void 0, maxHeight: Math.min(maxH, (up ? r.top : below) - 8) });
50
+ }
51
+ useIsoLayoutEffect(function() {
52
+ if (open) place();
53
+ }, [open, shown.length]);
54
+ React.useEffect(function() {
55
+ if (!open) return;
56
+ function outside(e) {
57
+ if (boxRef.current && boxRef.current.contains(e.target)) return;
58
+ if (listRef.current && listRef.current.contains(e.target)) return;
59
+ close(false);
60
+ }
61
+ function onScroll(e) {
62
+ if (!listRef.current || !listRef.current.contains(e.target)) place();
63
+ }
64
+ document.addEventListener("pointerdown", outside, true);
65
+ window.addEventListener("scroll", onScroll, true);
66
+ window.addEventListener("resize", place);
67
+ return function() {
68
+ document.removeEventListener("pointerdown", outside, true);
69
+ window.removeEventListener("scroll", onScroll, true);
70
+ window.removeEventListener("resize", place);
71
+ };
72
+ }, [open]);
73
+ React.useEffect(function() {
74
+ if (!open || !listRef.current) return;
75
+ var el = listRef.current.querySelector('[data-idx="' + activeIdx + '"]');
76
+ if (el && el.scrollIntoView) el.scrollIntoView({ block: "nearest" });
77
+ }, [activeIdx, open]);
78
+ function openList() {
79
+ if (!open && !props.disabled && !props.readOnly) {
80
+ setOpen(true);
81
+ var i = selected ? shown.indexOf(selected) : 0;
82
+ setActive(i < 0 ? 0 : i);
83
+ }
84
+ }
85
+ function close(restoreLabel) {
86
+ setOpen(false);
87
+ setQuery(null);
88
+ }
89
+ function choose(o) {
90
+ if (!o || o.disabled) return;
91
+ setValue(o.value);
92
+ setQuery(null);
93
+ setOpen(false);
94
+ if (inputRef.current) inputRef.current.focus();
95
+ }
96
+ function onKeyDown(e) {
97
+ var k = e.key;
98
+ if (k === "ArrowDown") {
99
+ e.preventDefault();
100
+ if (!open) openList();
101
+ else setActive(Math.min(activeIdx + 1, shown.length - 1));
102
+ } else if (k === "ArrowUp") {
103
+ e.preventDefault();
104
+ if (!open) openList();
105
+ else setActive(Math.max(activeIdx - 1, 0));
106
+ } else if (k === "Home" && open) {
107
+ e.preventDefault();
108
+ setActive(0);
109
+ } else if (k === "End" && open) {
110
+ e.preventDefault();
111
+ setActive(shown.length - 1);
112
+ } else if (k === "Enter") {
113
+ if (open && shown[activeIdx]) {
114
+ e.preventDefault();
115
+ choose(shown[activeIdx]);
116
+ }
117
+ } else if (k === "Escape") {
118
+ if (open) {
119
+ e.preventDefault();
120
+ e.stopPropagation();
121
+ close();
122
+ } else if (props.clearable !== false && value != null && query == null) {
123
+ setValue(null);
124
+ }
125
+ } else if (k === "Tab") {
126
+ if (open) close();
127
+ }
128
+ }
129
+ var text = query != null ? query : selected ? selected.label : "";
130
+ var optId = function(i) {
131
+ return id + "-opt-" + i;
132
+ };
133
+ var list = open && mounted && posState[0] ? createPortal(
134
+ h(
135
+ "div",
136
+ { ref: listRef, className: "aura-combo__popover", style: posState[0] },
137
+ h(
138
+ "ul",
139
+ { id: listId, role: "listbox", "aria-label": props.label, className: "aura-combo__list" },
140
+ props.loading ? h("li", { className: "aura-combo__note", role: "presentation" }, h(Icon, { name: "loader-circle", className: "aura-spin" }), props.loadingText || t.searching) : !shown.length ? h("li", { className: "aura-combo__note", role: "presentation" }, props.emptyText || t.noMatches) : shown.map(function(o, i) {
141
+ var isSel = selected && o.value === selected.value;
142
+ return h(
143
+ "li",
144
+ {
145
+ key: o.value,
146
+ id: optId(i),
147
+ role: "option",
148
+ "data-idx": i,
149
+ "aria-selected": isSel,
150
+ "aria-disabled": o.disabled || void 0,
151
+ className: cx("aura-combo__option", i === activeIdx && "is-active", isSel && "is-selected", o.disabled && "is-disabled"),
152
+ onPointerDown: function(e) {
153
+ e.preventDefault();
154
+ },
155
+ onClick: function() {
156
+ choose(o);
157
+ },
158
+ onPointerMove: function() {
159
+ if (activeIdx !== i) setActive(i);
160
+ }
161
+ },
162
+ o.icon ? h(Icon, { name: o.icon }) : null,
163
+ h(
164
+ "span",
165
+ { className: "aura-combo__text" },
166
+ h("span", { className: "aura-combo__label" }, o.label),
167
+ o.description ? h("span", { className: "aura-combo__desc" }, o.description) : null
168
+ ),
169
+ isSel ? h(Icon, { name: "check", className: "aura-combo__check" }) : null
170
+ );
171
+ }),
172
+ more && !props.loading ? h("li", { className: "aura-combo__note", role: "presentation" }, t.keepTyping((props.options || []).length)) : null
173
+ )
174
+ ),
175
+ document.body
176
+ ) : null;
177
+ return h(
178
+ Field,
179
+ { id, label: props.label, hint: props.hint, error: props.error, required: props.required, optional: props.optional, disabled: props.disabled, className: props.className },
180
+ h(
181
+ "div",
182
+ { ref: boxRef, className: cx("aura-input aura-combo has-icon", open && "is-open") },
183
+ h(Icon, { name: props.icon || "search", className: "aura-input__icon" }),
184
+ h("input", {
185
+ ref: inputMerged,
186
+ id,
187
+ type: "text",
188
+ role: "combobox",
189
+ className: "aura-input__control",
190
+ autoComplete: "off",
191
+ "aria-expanded": open,
192
+ "aria-controls": listId,
193
+ "aria-autocomplete": "list",
194
+ "aria-activedescendant": open && shown[activeIdx] ? optId(activeIdx) : void 0,
195
+ "aria-invalid": props.error ? true : void 0,
196
+ "aria-describedby": props.error ? id + "-error" : props.hint ? id + "-hint" : void 0,
197
+ placeholder: props.placeholder,
198
+ disabled: props.disabled,
199
+ readOnly: props.readOnly,
200
+ required: props.required,
201
+ name: props.name,
202
+ value: text,
203
+ onChange: function(e) {
204
+ setQuery(e.target.value);
205
+ setActive(0);
206
+ if (!open) setOpen(true);
207
+ if (props.onSearch) props.onSearch(e.target.value);
208
+ },
209
+ onClick: openList,
210
+ onKeyDown,
211
+ onBlur: function() {
212
+ setTimeout(function() {
213
+ if (listRef.current && listRef.current.contains(document.activeElement)) return;
214
+ setOpen(false);
215
+ setQuery(null);
216
+ }, 0);
217
+ }
218
+ }),
219
+ props.clearable !== false && value != null && !props.disabled ? h("button", {
220
+ type: "button",
221
+ className: "aura-combo__clear",
222
+ "aria-label": t.clear(props.label),
223
+ tabIndex: -1,
224
+ onClick: function() {
225
+ setValue(null);
226
+ setQuery(null);
227
+ if (inputRef.current) inputRef.current.focus();
228
+ }
229
+ }, h(Icon, { name: "x" })) : null,
230
+ h(
231
+ "button",
232
+ {
233
+ type: "button",
234
+ tabIndex: -1,
235
+ "aria-hidden": true,
236
+ className: "aura-combo__toggle",
237
+ onPointerDown: function(e) {
238
+ e.preventDefault();
239
+ },
240
+ onClick: function() {
241
+ if (open) close();
242
+ else {
243
+ openList();
244
+ inputRef.current && inputRef.current.focus();
245
+ }
246
+ }
247
+ },
248
+ h(Icon, { name: "chevron-down" })
249
+ )
250
+ ),
251
+ list
252
+ );
253
+ });
254
+ export {
255
+ Combobox,
256
+ defaultFilter
257
+ };