@pallasoft/ps-input-components 1.6.1 → 1.8.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/dist/components/ps-cascader-input.d.ts +23 -0
- package/dist/components/ps-cascader-input.js +48 -0
- package/dist/components/ps-color-input.js +10 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/services/ps-cascader-service.d.ts +9 -0
- package/dist/services/ps-cascader-service.js +26 -0
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface PsCascaderOption {
|
|
2
|
+
description?: string;
|
|
3
|
+
label: string;
|
|
4
|
+
value: string;
|
|
5
|
+
}
|
|
6
|
+
export interface PsCascaderGroup {
|
|
7
|
+
key: string;
|
|
8
|
+
label: string;
|
|
9
|
+
options: PsCascaderOption[];
|
|
10
|
+
}
|
|
11
|
+
export interface PsCascaderInputProps {
|
|
12
|
+
className?: string;
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
groups: PsCascaderGroup[];
|
|
15
|
+
helperText?: string;
|
|
16
|
+
hideLabel?: boolean;
|
|
17
|
+
label: string;
|
|
18
|
+
onChange: (value: string) => void;
|
|
19
|
+
placeholder?: string;
|
|
20
|
+
searchPlaceholder?: string;
|
|
21
|
+
value: string;
|
|
22
|
+
}
|
|
23
|
+
export declare const PsCascaderInput: ({ className, disabled, groups, helperText, hideLabel, label, onChange, placeholder, searchPlaceholder, value, }: PsCascaderInputProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PsCascaderInput = void 0;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const ps_core_components_1 = require("@pallasoft/ps-core-components");
|
|
7
|
+
const ps_design_system_1 = require("@pallasoft/ps-design-system");
|
|
8
|
+
const ps_icons_1 = require("@pallasoft/ps-icons");
|
|
9
|
+
const react_aria_components_1 = require("react-aria-components");
|
|
10
|
+
const ps_cascader_service_1 = require("../services/ps-cascader-service");
|
|
11
|
+
const PsCascaderInput = ({ className, disabled, groups, helperText, hideLabel, label, onChange, placeholder = 'Select an option', searchPlaceholder = 'Search', value, }) => {
|
|
12
|
+
const zIndex = (0, ps_core_components_1.usePsLayerZIndex)(ps_design_system_1.PallasoftZIndex.Dropdown);
|
|
13
|
+
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
14
|
+
const [query, setQuery] = (0, react_1.useState)('');
|
|
15
|
+
const [activeGroupKey, setActiveGroupKey] = (0, react_1.useState)(null);
|
|
16
|
+
const selected = (0, react_1.useMemo)(() => ps_cascader_service_1.PsCascaderService.findSelected(groups, value), [groups, value]);
|
|
17
|
+
const matches = (0, react_1.useMemo)(() => ps_cascader_service_1.PsCascaderService.search(groups, query), [groups, query]);
|
|
18
|
+
const activeGroup = (0, react_1.useMemo)(() => {
|
|
19
|
+
const key = activeGroupKey
|
|
20
|
+
? activeGroupKey
|
|
21
|
+
: selected
|
|
22
|
+
? selected.group.key
|
|
23
|
+
: groups.length > 0
|
|
24
|
+
? groups[0].key
|
|
25
|
+
: null;
|
|
26
|
+
return groups.find((group) => group.key === key) ?? null;
|
|
27
|
+
}, [activeGroupKey, groups, selected]);
|
|
28
|
+
const open = (nextOpen) => {
|
|
29
|
+
setIsOpen(nextOpen);
|
|
30
|
+
if (!nextOpen)
|
|
31
|
+
return;
|
|
32
|
+
setQuery('');
|
|
33
|
+
setActiveGroupKey(selected ? selected.group.key : null);
|
|
34
|
+
};
|
|
35
|
+
const select = (nextValue, close) => {
|
|
36
|
+
onChange(nextValue);
|
|
37
|
+
close();
|
|
38
|
+
};
|
|
39
|
+
const renderOption = (option, groupLabel, close) => ((0, jsx_runtime_1.jsxs)("button", { "aria-selected": option.value === value, className: ps_design_system_1.PallasoftClassNameService.merge('ps-focus-ring flex w-full items-start gap-2 rounded-ps-control px-3 py-2.5 text-left text-sm font-medium transition-colors', option.value === value
|
|
40
|
+
? 'bg-primary/10 text-ink'
|
|
41
|
+
: 'text-ink/80 hover:bg-mist/60 hover:text-ink'), onClick: () => select(option.value, close), role: 'option', type: 'button', children: [(0, jsx_runtime_1.jsx)("span", { className: 'flex h-5 w-4 shrink-0 items-center justify-center text-primary', children: option.value === value ? ((0, jsx_runtime_1.jsx)(ps_icons_1.PsIcon, { "aria-hidden": 'true', className: 'block h-4 w-4', iconKey: ps_icons_1.PsIconKey.Check })) : null }), (0, jsx_runtime_1.jsxs)("span", { className: 'min-w-0 flex-1', children: [(0, jsx_runtime_1.jsx)("span", { className: 'block truncate', children: option.label }), groupLabel ? ((0, jsx_runtime_1.jsx)("span", { className: 'mt-0.5 block truncate text-xs font-normal text-ink/50', children: groupLabel })) : null, option.description ? ((0, jsx_runtime_1.jsx)("span", { className: 'mt-0.5 block truncate text-xs font-normal text-ink/55', children: option.description })) : null] })] }, option.value));
|
|
42
|
+
return ((0, jsx_runtime_1.jsxs)("div", { className: ps_design_system_1.PallasoftClassNameService.merge('block', className), children: [hideLabel ? null : (0, jsx_runtime_1.jsx)("span", { className: 'ps-input-label block', children: label }), (0, jsx_runtime_1.jsxs)(react_aria_components_1.DialogTrigger, { isOpen: isOpen, onOpenChange: open, children: [(0, jsx_runtime_1.jsxs)(react_aria_components_1.Button, { "aria-label": hideLabel ? label : undefined, className: ps_design_system_1.PallasoftClassNameService.merge('ps-input-control ps-focus-ring relative flex min-h-11 w-full items-center pr-10 text-left text-sm font-semibold disabled:cursor-not-allowed disabled:opacity-60', hideLabel ? '' : 'mt-2'), isDisabled: disabled, children: [(0, jsx_runtime_1.jsx)("span", { className: 'min-w-0 flex-1 truncate', children: selected ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("span", { className: 'truncate', children: selected.option.label }), (0, jsx_runtime_1.jsx)("span", { className: 'ml-2 text-xs font-medium text-ink/45', children: selected.group.label })] })) : ((0, jsx_runtime_1.jsx)("span", { className: 'font-medium text-ink/45', children: placeholder })) }), (0, jsx_runtime_1.jsx)(ps_icons_1.PsIcon, { "aria-hidden": 'true', className: 'pointer-events-none absolute top-1/2 right-3 h-4 w-4 -translate-y-1/2 text-primary', iconKey: ps_icons_1.PsIconKey.Chevron })] }), (0, jsx_runtime_1.jsx)(react_aria_components_1.Popover, { className: 'ps-floating-panel w-[min(calc(100vw-2rem),32rem)] min-w-[var(--trigger-width)] outline-none', offset: 6, placement: 'bottom start', style: { zIndex }, children: (0, jsx_runtime_1.jsx)(react_aria_components_1.Dialog, { "aria-label": label, className: 'flex flex-col outline-none', children: ({ close }) => ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: 'border-b border-line p-2', children: (0, jsx_runtime_1.jsx)("input", { "aria-label": searchPlaceholder, autoFocus: true, className: 'ps-input-control ps-focus-ring h-10 w-full text-sm', onChange: (event) => setQuery(event.target.value), placeholder: searchPlaceholder, type: 'search', value: query }) }), query.trim() === '' ? ((0, jsx_runtime_1.jsxs)("div", { className: 'grid grid-cols-1 sm:grid-cols-[minmax(0,11rem)_minmax(0,1fr)]', children: [(0, jsx_runtime_1.jsx)("div", { className: ps_design_system_1.PallasoftClassNameService.merge('ps-scrollbar max-h-72 overflow-y-auto p-1.5 sm:border-r sm:border-line', activeGroupKey && groups.length > 1 ? 'hidden sm:block' : ''), children: groups.map((group) => ((0, jsx_runtime_1.jsxs)("button", { className: ps_design_system_1.PallasoftClassNameService.merge('ps-focus-ring flex w-full items-center gap-2 rounded-ps-control px-3 py-2.5 text-left text-sm font-semibold transition-colors', activeGroup?.key === group.key
|
|
43
|
+
? 'bg-mist text-ink'
|
|
44
|
+
: 'text-ink/70 hover:bg-mist/60 hover:text-ink'), onClick: () => setActiveGroupKey(group.key), type: 'button', children: [(0, jsx_runtime_1.jsx)("span", { className: 'min-w-0 flex-1 truncate', children: group.label }), (0, jsx_runtime_1.jsx)("span", { className: 'text-xs font-medium text-ink/45', children: group.options.length }), (0, jsx_runtime_1.jsx)(ps_icons_1.PsIcon, { "aria-hidden": 'true', className: 'block h-4 w-4 shrink-0 -rotate-90 text-ink/35', iconKey: ps_icons_1.PsIconKey.Chevron })] }, group.key))) }), (0, jsx_runtime_1.jsxs)("div", { className: ps_design_system_1.PallasoftClassNameService.merge('ps-scrollbar max-h-72 overflow-y-auto p-1.5', activeGroupKey && groups.length > 1 ? '' : 'hidden sm:block'), role: 'listbox', children: [groups.length > 1 ? ((0, jsx_runtime_1.jsxs)("button", { className: 'ps-focus-ring mb-1 flex w-full items-center gap-2 rounded-ps-control px-3 py-2 text-left text-xs font-semibold text-ink/60 hover:text-ink sm:hidden', onClick: () => setActiveGroupKey(null), type: 'button', children: [(0, jsx_runtime_1.jsx)(ps_icons_1.PsIcon, { "aria-hidden": 'true', className: 'block h-4 w-4 rotate-90', iconKey: ps_icons_1.PsIconKey.Chevron }), "All groups"] })) : null, activeGroup
|
|
45
|
+
? activeGroup.options.map((option) => renderOption(option, null, close))
|
|
46
|
+
: null] })] })) : ((0, jsx_runtime_1.jsx)("div", { className: 'ps-scrollbar max-h-72 overflow-y-auto p-1.5', role: 'listbox', children: matches.length === 0 ? ((0, jsx_runtime_1.jsx)("p", { className: 'px-3 py-6 text-center text-sm text-ink/55', children: "No matches." })) : (matches.map((match) => renderOption(match.option, match.group.label, close))) }))] })) }) })] }), helperText ? (0, jsx_runtime_1.jsx)("span", { className: 'ps-input-helper mt-2 block', children: helperText }) : null] }));
|
|
47
|
+
};
|
|
48
|
+
exports.PsCascaderInput = PsCascaderInput;
|
|
@@ -8,9 +8,9 @@ const ps_icons_1 = require("@pallasoft/ps-icons");
|
|
|
8
8
|
const ps_color_input_config_1 = require("../constants/ps-color-input-config");
|
|
9
9
|
const PsColorInput = ({ className, helperText, label, onChange, swatches = [...ps_color_input_config_1.PsColorInputConfig.swatches], value, }) => {
|
|
10
10
|
const labelId = (0, react_1.useId)();
|
|
11
|
+
const swatchesId = (0, react_1.useId)();
|
|
11
12
|
const [draft, setDraft] = (0, react_1.useState)(value);
|
|
12
13
|
const normalised = value.toUpperCase();
|
|
13
|
-
const isCustom = value !== '' && !swatches.some((swatch) => swatch.toUpperCase() === normalised);
|
|
14
14
|
(0, react_1.useEffect)(() => {
|
|
15
15
|
setDraft(value);
|
|
16
16
|
}, [value]);
|
|
@@ -21,14 +21,14 @@ const PsColorInput = ({ className, helperText, label, onChange, swatches = [...p
|
|
|
21
21
|
else
|
|
22
22
|
setDraft(value);
|
|
23
23
|
};
|
|
24
|
-
return ((0, jsx_runtime_1.jsxs)("div", { className: ps_design_system_1.PallasoftClassNameService.merge('flex flex-col gap-2', className), children: [(0, jsx_runtime_1.jsx)("span", { className: 'ps-input-label', id: labelId, children: label }), (0, jsx_runtime_1.jsxs)("div", {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
return ((0, jsx_runtime_1.jsxs)("div", { className: ps_design_system_1.PallasoftClassNameService.merge('flex flex-col gap-2', className), children: [(0, jsx_runtime_1.jsx)("span", { className: 'ps-input-label', id: labelId, children: label }), (0, jsx_runtime_1.jsxs)("div", { className: 'flex flex-col gap-4 rounded-ps-control border border-line bg-surface p-4', children: [(0, jsx_runtime_1.jsxs)("div", { className: 'flex items-center gap-3', children: [(0, jsx_runtime_1.jsx)("label", { className: 'ps-focus-ring relative h-11 w-11 shrink-0 cursor-pointer overflow-hidden rounded-ps-control border border-line ring-offset-2 ring-offset-surface', style: { backgroundColor: value === '' ? 'transparent' : value }, title: 'Pick a colour', children: (0, jsx_runtime_1.jsx)("input", { "aria-label": `${label} picker`, className: 'absolute inset-0 cursor-pointer opacity-0', onChange: (event) => onChange(event.target.value.toUpperCase()), type: 'color', value: value === '' ? '#000000' : value }) }), (0, jsx_runtime_1.jsx)("input", { "aria-label": `${label} hex`, className: 'ps-input-control ps-focus-ring h-11 min-w-0 flex-1 font-mono text-sm uppercase', maxLength: 7, onBlur: (event) => commit(event.target.value), onChange: (event) => setDraft(event.target.value), onKeyDown: (event) => {
|
|
25
|
+
if (event.key === 'Enter') {
|
|
26
|
+
event.preventDefault();
|
|
27
|
+
commit(draft);
|
|
28
|
+
}
|
|
29
|
+
}, placeholder: '#000000', value: draft })] }), (0, jsx_runtime_1.jsxs)("div", { className: 'flex flex-col gap-2 border-t border-line pt-4', children: [(0, jsx_runtime_1.jsx)("span", { className: 'ps-input-helper', id: swatchesId, children: "Presets" }), (0, jsx_runtime_1.jsx)("div", { "aria-labelledby": swatchesId, className: 'flex flex-wrap gap-2', role: 'radiogroup', children: swatches.map((swatch) => {
|
|
30
|
+
const isSelected = swatch.toUpperCase() === normalised;
|
|
31
|
+
return ((0, jsx_runtime_1.jsx)("button", { "aria-checked": isSelected, "aria-label": swatch, className: ps_design_system_1.PallasoftClassNameService.merge('ps-focus-ring flex h-8 w-8 items-center justify-center rounded-full text-white ring-offset-2 ring-offset-surface transition-shadow', isSelected ? 'ring-2 ring-ink/70' : 'hover:ring-2 hover:ring-ink/25'), onClick: () => onChange(swatch.toUpperCase()), role: 'radio', style: { backgroundColor: swatch }, type: 'button', children: isSelected ? ((0, jsx_runtime_1.jsx)(ps_icons_1.PsIcon, { "aria-hidden": 'true', className: 'block h-4 w-4', iconKey: ps_icons_1.PsIconKey.Check })) : null }, swatch));
|
|
32
|
+
}) })] })] }), helperText ? (0, jsx_runtime_1.jsx)("span", { className: 'ps-input-helper', children: helperText }) : null] }));
|
|
33
33
|
};
|
|
34
34
|
exports.PsColorInput = PsColorInput;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./components/ps-cascader-input"), exports);
|
|
17
18
|
__exportStar(require("./components/ps-checkbox"), exports);
|
|
18
19
|
__exportStar(require("./components/ps-color-input"), exports);
|
|
19
20
|
__exportStar(require("./components/ps-combobox-input"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PsCascaderGroup, PsCascaderOption } from '../components/ps-cascader-input';
|
|
2
|
+
export interface PsCascaderMatch {
|
|
3
|
+
group: PsCascaderGroup;
|
|
4
|
+
option: PsCascaderOption;
|
|
5
|
+
}
|
|
6
|
+
export declare class PsCascaderService {
|
|
7
|
+
static findSelected(groups: PsCascaderGroup[], value: string): PsCascaderMatch | null;
|
|
8
|
+
static search(groups: PsCascaderGroup[], query: string): PsCascaderMatch[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PsCascaderService = void 0;
|
|
4
|
+
class PsCascaderService {
|
|
5
|
+
static findSelected(groups, value) {
|
|
6
|
+
if (value === '')
|
|
7
|
+
return null;
|
|
8
|
+
for (const group of groups) {
|
|
9
|
+
const option = group.options.find((item) => item.value === value);
|
|
10
|
+
if (option)
|
|
11
|
+
return { group, option };
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
static search(groups, query) {
|
|
16
|
+
const term = query.trim().toLowerCase();
|
|
17
|
+
if (term === '')
|
|
18
|
+
return [];
|
|
19
|
+
return groups.flatMap((group) => group.options
|
|
20
|
+
.filter((option) => option.label.toLowerCase().includes(term) ||
|
|
21
|
+
group.label.toLowerCase().includes(term) ||
|
|
22
|
+
(option.description ? option.description.toLowerCase().includes(term) : false))
|
|
23
|
+
.map((option) => ({ group, option })));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.PsCascaderService = PsCascaderService;
|