@nicertravel/ui 1.3.0 → 1.4.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/index.d.ts CHANGED
@@ -83,13 +83,20 @@ interface AutocompleteProps<T> extends InputHTMLAttributes<HTMLInputElement> {
83
83
  inputValue?: string;
84
84
  isLoading?: boolean;
85
85
  label?: string;
86
+ /**
87
+ * An X empties the field as deleting what was typed would: it answers
88
+ * `onInputChange("")`. For a host that follows the typing there.
89
+ */
90
+ clearable?: boolean;
91
+ /** Empties the field, as a text input's X; with `clearable`, after it. */
92
+ onClear?: () => void;
86
93
  onInputChange?: (value: string) => void;
87
94
  onSelectOption?: (value?: T) => void;
88
95
  options: DropdownOption<T>[];
89
96
  required?: boolean;
90
97
  selectedOptions?: T[];
91
98
  }
92
- declare function Autocomplete<T>({ label, options, onInputChange, onSelectOption, selectedOptions, inputValue, required, isLoading, onBlur, onFocus, defaultOptions, icon, ...rest }: AutocompleteProps<T>): import("react").JSX.Element;
99
+ declare function Autocomplete<T>({ label, options, onInputChange, onSelectOption, selectedOptions, inputValue, required, isLoading, onBlur, onFocus, defaultOptions, icon, clearable, onClear, ...rest }: AutocompleteProps<T>): import("react").JSX.Element;
93
100
  export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
94
101
  buttonType: "primary" | "secondary" | "tertiary" | "destructive";
95
102
  dropdown?: boolean;
@@ -773,8 +780,13 @@ interface TextInputProps extends InputHTMLAttributes<HTMLInputElement> {
773
780
  icon?: string;
774
781
  isLoading?: boolean;
775
782
  label?: string;
783
+ /**
784
+ * Empties the field: with it, an X shows at the field's right while it
785
+ * holds text, and puts the cursor back in it. The host clears its value.
786
+ */
787
+ onClear?: () => void;
776
788
  }
777
- declare function TextInput({ icon, id, isLoading, label, error, ...rest }: TextInputProps): import("react").JSX.Element;
789
+ declare function TextInput({ icon, id, isLoading, label, error, onClear, ...rest }: TextInputProps): import("react").JSX.Element;
778
790
  export type ThemeName = "dark" | "light";
779
791
  /**
780
792
  * Shows `theme` and saves it as the visitor's choice. With `persist: false`
package/dist/index.js CHANGED
@@ -448,12 +448,19 @@ function FieldError({ children, className, id }) {
448
448
  });
449
449
  }
450
450
  // src/components/TextInput.tsx
451
- function TextInput({ icon, id, isLoading, label, error, ...rest }) {
451
+ function TextInput({ icon, id, isLoading, label, error, onClear, ...rest }) {
452
+ const wrapperRef = useRef(null);
453
+ const clearable = onClear !== void 0 && !isLoading && String(rest.value ?? "") !== "";
452
454
  const generatedId = useId();
453
455
  const inputId = id ?? generatedId;
454
456
  const errorId = `${inputId}-error`;
455
457
  return /* @__PURE__ */ jsxs("div", {
456
- className: `ui-text-input ${icon ? "icon" : ""}`,
458
+ className: [
459
+ "ui-text-input",
460
+ icon ? "icon" : "",
461
+ clearable ? "clearable" : ""
462
+ ].filter(Boolean).join(" "),
463
+ ref: wrapperRef,
457
464
  children: [
458
465
  /* @__PURE__ */ jsx("input", {
459
466
  "aria-describedby": error ? errorId : void 0,
@@ -475,6 +482,24 @@ function TextInput({ icon, id, isLoading, label, error, ...rest }) {
475
482
  color: "light",
476
483
  size: 16
477
484
  }),
485
+ clearable ? /* @__PURE__ */ jsx("button", {
486
+ "aria-label": "Clear",
487
+ className: "clear",
488
+ onClick: () => {
489
+ const input = wrapperRef.current?.querySelector("input");
490
+ onClear();
491
+ if (input) {
492
+ input.value = "";
493
+ input.focus();
494
+ }
495
+ },
496
+ onMouseDown: (event) => event.preventDefault(),
497
+ type: "button",
498
+ children: /* @__PURE__ */ jsx(Icon, {
499
+ misc: true,
500
+ name: "misc-cancel"
501
+ })
502
+ }) : null,
478
503
  error ? /* @__PURE__ */ jsx(FieldError, {
479
504
  id: errorId,
480
505
  children: error
@@ -483,7 +508,11 @@ function TextInput({ icon, id, isLoading, label, error, ...rest }) {
483
508
  });
484
509
  }
485
510
  // src/components/Autocomplete.tsx
486
- function Autocomplete({ label, options, onInputChange, onSelectOption, selectedOptions, inputValue, required, isLoading, onBlur, onFocus, defaultOptions, icon, ...rest }) {
511
+ function Autocomplete({ label, options, onInputChange, onSelectOption, selectedOptions, inputValue, required, isLoading, onBlur, onFocus, defaultOptions, icon, clearable, onClear, ...rest }) {
512
+ const clear = clearable || onClear ? () => {
513
+ if (clearable) onInputChange?.("");
514
+ onClear?.();
515
+ } : void 0;
487
516
  const [dropdownShown, setDropdownShown] = useState(false);
488
517
  const [defaultOptionsDropdownShown, setDefaultOptionsDropdownShown] = useState(false);
489
518
  const wrapperRef = useRef(null);
@@ -511,6 +540,7 @@ function Autocomplete({ label, options, onInputChange, onSelectOption, selectedO
511
540
  value: inputValue,
512
541
  ...rest,
513
542
  icon,
543
+ onClear: isLoading ? void 0 : clear,
514
544
  onBlur,
515
545
  onFocus: (e) => {
516
546
  if (!inputValue) setDefaultOptionsDropdownShown(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nicertravel/ui",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Nicer's design system: React primitives, tokens and styles.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -1748,6 +1748,33 @@ img.ui-image-reveal
1748
1748
  left: 12px
1749
1749
  pointer-events: none
1750
1750
 
1751
+ // Its X while it holds text (onClear), over the field's right end; after
1752
+ // the icon's rule, so the X's icon stays in its button.
1753
+ &.clearable input
1754
+ padding-right: 40px
1755
+
1756
+ > .clear
1757
+ width: 24px
1758
+ height: 24px
1759
+ border-radius: 50%
1760
+ display: grid
1761
+ place-items: center
1762
+ position: absolute
1763
+ top: 8px
1764
+ right: 8px
1765
+ color: var(--textSoft)
1766
+ cursor: pointer
1767
+ transition: background 150ms, color 150ms
1768
+
1769
+ .ui-icon
1770
+ opacity: 1
1771
+ position: static
1772
+
1773
+ &:hover,
1774
+ &:focus-visible
1775
+ background: var(--bgSoft)
1776
+ color: var(--highlight)
1777
+
1751
1778
 
1752
1779
  // Field error: what is wrong with a field, the same under every field and
1753
1780
  // in a field's bubble. The icon sits on the first line when the words wrap.