@pallasoft/ps-input-components 1.7.0 → 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.
@@ -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;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './components/ps-cascader-input';
1
2
  export * from './components/ps-checkbox';
2
3
  export * from './components/ps-color-input';
3
4
  export * from './components/ps-combobox-input';
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pallasoft/ps-input-components",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",