@pallasoft/ps-input-components 1.4.1 → 1.5.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,9 @@
1
+ export interface PsColorInputProps {
2
+ className?: string;
3
+ helperText?: string;
4
+ label: string;
5
+ onChange: (value: string) => void;
6
+ swatches?: string[];
7
+ value: string;
8
+ }
9
+ export declare const PsColorInput: ({ className, helperText, label, onChange, swatches, value, }: PsColorInputProps) => import("react").JSX.Element;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PsColorInput = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const ps_design_system_1 = require("@pallasoft/ps-design-system");
7
+ const ps_icons_1 = require("@pallasoft/ps-icons");
8
+ const ps_color_input_config_1 = require("../constants/ps-color-input-config");
9
+ const PsColorInput = ({ className, helperText, label, onChange, swatches = [...ps_color_input_config_1.PsColorInputConfig.swatches], value, }) => {
10
+ const labelId = (0, react_1.useId)();
11
+ const [draft, setDraft] = (0, react_1.useState)(value);
12
+ const normalised = value.toUpperCase();
13
+ const isCustom = value !== '' && !swatches.some((swatch) => swatch.toUpperCase() === normalised);
14
+ (0, react_1.useEffect)(() => {
15
+ setDraft(value);
16
+ }, [value]);
17
+ const commit = (next) => {
18
+ const hex = next.startsWith('#') ? next : `#${next}`;
19
+ if (ps_color_input_config_1.PsColorInputConfig.hexPattern.test(hex))
20
+ onChange(hex.toUpperCase());
21
+ else
22
+ setDraft(value);
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", { "aria-labelledby": labelId, className: 'flex flex-wrap items-center gap-2', role: 'radiogroup', children: [swatches.map((swatch) => {
25
+ const isSelected = swatch.toUpperCase() === normalised;
26
+ 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));
27
+ }), (0, jsx_runtime_1.jsxs)("label", { className: ps_design_system_1.PallasoftClassNameService.merge('ps-focus-ring relative flex h-8 w-8 cursor-pointer items-center justify-center overflow-hidden rounded-full border border-dashed border-line text-ink/60 ring-offset-2 ring-offset-surface hover:border-primary/60 hover:text-ink', isCustom ? 'border-solid ring-2 ring-ink/70' : ''), style: isCustom ? { backgroundColor: value } : undefined, title: 'Custom colour', children: [isCustom ? null : ((0, jsx_runtime_1.jsx)(ps_icons_1.PsIcon, { "aria-hidden": 'true', className: 'block h-4 w-4', iconKey: ps_icons_1.PsIconKey.Plus })), (0, jsx_runtime_1.jsx)("input", { "aria-label": 'Custom colour', 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-8 w-28 rounded-full px-3 py-0 font-mono text-xs uppercase', maxLength: 7, onBlur: (event) => commit(event.target.value), onChange: (event) => setDraft(event.target.value), onKeyDown: (event) => {
28
+ if (event.key === 'Enter') {
29
+ event.preventDefault();
30
+ commit(draft);
31
+ }
32
+ }, placeholder: '#000000', value: draft })] }), helperText ? (0, jsx_runtime_1.jsx)("span", { className: 'ps-input-helper', children: helperText }) : null] }));
33
+ };
34
+ exports.PsColorInput = PsColorInput;
@@ -0,0 +1,4 @@
1
+ export declare const PsColorInputConfig: {
2
+ readonly hexPattern: RegExp;
3
+ readonly swatches: readonly ["#1F6FEB", "#0E9F6E", "#F59E0B", "#E02424", "#7E3AF2", "#0F172A", "#DB2777", "#0891B2"];
4
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PsColorInputConfig = void 0;
4
+ exports.PsColorInputConfig = {
5
+ hexPattern: /^#[0-9a-fA-F]{6}$/,
6
+ swatches: [
7
+ '#1F6FEB',
8
+ '#0E9F6E',
9
+ '#F59E0B',
10
+ '#E02424',
11
+ '#7E3AF2',
12
+ '#0F172A',
13
+ '#DB2777',
14
+ '#0891B2',
15
+ ],
16
+ };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './components/ps-checkbox';
2
+ export * from './components/ps-color-input';
2
3
  export * from './components/ps-combobox-input';
3
4
  export * from './components/ps-date-input';
4
5
  export * from './components/ps-dropdown';
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./components/ps-checkbox"), exports);
18
+ __exportStar(require("./components/ps-color-input"), exports);
18
19
  __exportStar(require("./components/ps-combobox-input"), exports);
19
20
  __exportStar(require("./components/ps-date-input"), exports);
20
21
  __exportStar(require("./components/ps-dropdown"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pallasoft/ps-input-components",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",