@navikt/ds-react 4.6.1 → 4.7.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.
Files changed (113) hide show
  1. package/_docs.json +1711 -169
  2. package/cjs/chips/Chips.js +1 -2
  3. package/cjs/form/combobox/ClearButton.js +27 -0
  4. package/cjs/form/combobox/Combobox.js +78 -0
  5. package/cjs/form/combobox/ComboboxProvider.js +99 -0
  6. package/cjs/form/combobox/ComboboxWrapper.js +51 -0
  7. package/cjs/form/combobox/FilteredOptions/CheckIcon.js +11 -0
  8. package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +46 -0
  9. package/cjs/form/combobox/FilteredOptions/filteredOptionsContext.js +208 -0
  10. package/cjs/form/combobox/Input/Input.js +143 -0
  11. package/cjs/form/combobox/Input/inputContext.js +86 -0
  12. package/cjs/form/combobox/SelectedOptions/SelectedOptions.js +27 -0
  13. package/cjs/form/combobox/SelectedOptions/selectedOptionsContext.js +107 -0
  14. package/cjs/form/combobox/ToggleListButton.js +36 -0
  15. package/cjs/form/combobox/customOptionsContext.js +56 -0
  16. package/cjs/form/combobox/index.js +8 -0
  17. package/cjs/form/combobox/package.json +6 -0
  18. package/cjs/form/combobox/types.js +2 -0
  19. package/cjs/form/index.js +3 -1
  20. package/cjs/timeline/AxisLabels.js +12 -12
  21. package/cjs/timeline/Timeline.js +2 -2
  22. package/cjs/util/usePrevious.js +18 -0
  23. package/esm/chips/Chips.js +1 -2
  24. package/esm/chips/Chips.js.map +1 -1
  25. package/esm/date/datepicker/TableHead.d.ts +1 -0
  26. package/esm/form/Fieldset/useFieldset.d.ts +1 -1
  27. package/esm/form/checkbox/useCheckbox.d.ts +4 -4
  28. package/esm/form/combobox/ClearButton.d.ts +7 -0
  29. package/esm/form/combobox/ClearButton.js +21 -0
  30. package/esm/form/combobox/ClearButton.js.map +1 -0
  31. package/esm/form/combobox/Combobox.d.ts +4 -0
  32. package/esm/form/combobox/Combobox.js +50 -0
  33. package/esm/form/combobox/Combobox.js.map +1 -0
  34. package/esm/form/combobox/ComboboxProvider.d.ts +26 -0
  35. package/esm/form/combobox/ComboboxProvider.js +72 -0
  36. package/esm/form/combobox/ComboboxProvider.js.map +1 -0
  37. package/esm/form/combobox/ComboboxWrapper.d.ts +14 -0
  38. package/esm/form/combobox/ComboboxWrapper.js +24 -0
  39. package/esm/form/combobox/ComboboxWrapper.js.map +1 -0
  40. package/esm/form/combobox/FilteredOptions/CheckIcon.d.ts +3 -0
  41. package/esm/form/combobox/FilteredOptions/CheckIcon.js +7 -0
  42. package/esm/form/combobox/FilteredOptions/CheckIcon.js.map +1 -0
  43. package/esm/form/combobox/FilteredOptions/FilteredOptions.d.ts +3 -0
  44. package/esm/form/combobox/FilteredOptions/FilteredOptions.js +42 -0
  45. package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -0
  46. package/esm/form/combobox/FilteredOptions/filteredOptionsContext.d.ts +27 -0
  47. package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js +178 -0
  48. package/esm/form/combobox/FilteredOptions/filteredOptionsContext.js.map +1 -0
  49. package/esm/form/combobox/Input/Input.d.ts +10 -0
  50. package/esm/form/combobox/Input/Input.js +116 -0
  51. package/esm/form/combobox/Input/Input.js.map +1 -0
  52. package/esm/form/combobox/Input/inputContext.d.ts +19 -0
  53. package/esm/form/combobox/Input/inputContext.js +59 -0
  54. package/esm/form/combobox/Input/inputContext.js.map +1 -0
  55. package/esm/form/combobox/SelectedOptions/SelectedOptions.d.ts +8 -0
  56. package/esm/form/combobox/SelectedOptions/SelectedOptions.js +23 -0
  57. package/esm/form/combobox/SelectedOptions/SelectedOptions.js.map +1 -0
  58. package/esm/form/combobox/SelectedOptions/selectedOptionsContext.d.ts +17 -0
  59. package/esm/form/combobox/SelectedOptions/selectedOptionsContext.js +77 -0
  60. package/esm/form/combobox/SelectedOptions/selectedOptionsContext.js.map +1 -0
  61. package/esm/form/combobox/ToggleListButton.d.ts +6 -0
  62. package/esm/form/combobox/ToggleListButton.js +11 -0
  63. package/esm/form/combobox/ToggleListButton.js.map +1 -0
  64. package/esm/form/combobox/customOptionsContext.d.ts +11 -0
  65. package/esm/form/combobox/customOptionsContext.js +29 -0
  66. package/esm/form/combobox/customOptionsContext.js.map +1 -0
  67. package/esm/form/combobox/index.d.ts +2 -0
  68. package/esm/form/combobox/index.js +2 -0
  69. package/esm/form/combobox/index.js.map +1 -0
  70. package/esm/form/combobox/types.d.ts +119 -0
  71. package/esm/form/combobox/types.js +2 -0
  72. package/esm/form/combobox/types.js.map +1 -0
  73. package/esm/form/index.d.ts +1 -0
  74. package/esm/form/index.js +1 -0
  75. package/esm/form/index.js.map +1 -1
  76. package/esm/form/radio/useRadio.d.ts +4 -4
  77. package/esm/form/useFormField.d.ts +11 -10
  78. package/esm/form/useFormField.js.map +1 -1
  79. package/esm/timeline/AxisLabels.d.ts +7 -5
  80. package/esm/timeline/AxisLabels.js +12 -12
  81. package/esm/timeline/AxisLabels.js.map +1 -1
  82. package/esm/timeline/Timeline.d.ts +6 -0
  83. package/esm/timeline/Timeline.js +2 -2
  84. package/esm/timeline/Timeline.js.map +1 -1
  85. package/esm/timeline/utils/types.external.d.ts +5 -0
  86. package/esm/util/usePrevious.d.ts +2 -0
  87. package/esm/util/usePrevious.js +17 -0
  88. package/esm/util/usePrevious.js.map +1 -0
  89. package/package.json +2 -2
  90. package/src/chips/Chips.tsx +1 -1
  91. package/src/form/combobox/ClearButton.tsx +29 -0
  92. package/src/form/combobox/Combobox.tsx +136 -0
  93. package/src/form/combobox/ComboboxProvider.tsx +99 -0
  94. package/src/form/combobox/ComboboxWrapper.tsx +63 -0
  95. package/src/form/combobox/FilteredOptions/CheckIcon.tsx +23 -0
  96. package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +106 -0
  97. package/src/form/combobox/FilteredOptions/filteredOptionsContext.tsx +266 -0
  98. package/src/form/combobox/Input/Input.tsx +170 -0
  99. package/src/form/combobox/Input/inputContext.tsx +127 -0
  100. package/src/form/combobox/SelectedOptions/SelectedOptions.tsx +45 -0
  101. package/src/form/combobox/SelectedOptions/selectedOptionsContext.tsx +147 -0
  102. package/src/form/combobox/ToggleListButton.tsx +37 -0
  103. package/src/form/combobox/combobox.stories.tsx +413 -0
  104. package/src/form/combobox/combobox.test.tsx +123 -0
  105. package/src/form/combobox/customOptionsContext.tsx +57 -0
  106. package/src/form/combobox/index.ts +2 -0
  107. package/src/form/combobox/types.ts +122 -0
  108. package/src/form/index.ts +1 -0
  109. package/src/form/useFormField.ts +19 -1
  110. package/src/timeline/AxisLabels.tsx +23 -13
  111. package/src/timeline/Timeline.tsx +18 -2
  112. package/src/timeline/utils/types.external.ts +6 -0
  113. package/src/util/usePrevious.ts +19 -0
@@ -3,17 +3,17 @@ import { CheckboxProps } from "../..";
3
3
  * Handles props for Checkboxes in context with Fieldset and CheckboxGroup
4
4
  */
5
5
  declare const useCheckbox: ({ children, ...props }: CheckboxProps) => {
6
- readOnly: true | undefined;
6
+ readOnly: boolean | undefined;
7
7
  nested: boolean;
8
8
  inputProps: {
9
9
  checked: boolean | undefined;
10
10
  defaultChecked: boolean | undefined;
11
11
  onChange: (e: any) => void;
12
12
  onClick: (e: any) => void;
13
- "aria-describedby": string | undefined;
14
- disabled: boolean | undefined;
15
- "aria-invalid"?: boolean | undefined;
16
13
  id: string;
14
+ "aria-invalid"?: boolean | undefined;
15
+ "aria-describedby"?: string | undefined;
16
+ disabled?: boolean | undefined;
17
17
  };
18
18
  showErrorMsg: boolean;
19
19
  hasError: boolean;
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ interface ClearButtonProps extends React.HTMLAttributes<HTMLButtonElement> {
3
+ handleClear: (event: any) => void;
4
+ clearButtonLabel?: string;
5
+ }
6
+ export declare const ClearButton: React.FC<ClearButtonProps>;
7
+ export default ClearButton;
@@ -0,0 +1,21 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import React from "react";
13
+ import { XMarkIcon } from "@navikt/aksel-icons";
14
+ export const ClearButton = (_a) => {
15
+ var { handleClear, clearButtonLabel } = _a, rest = __rest(_a, ["handleClear", "clearButtonLabel"]);
16
+ return (React.createElement("button", Object.assign({ type: "button", onClick: handleClear, className: "navds-combobox__button-clear" }, rest),
17
+ React.createElement("span", { className: "navds-sr-only" }, clearButtonLabel ? clearButtonLabel : "Tøm"),
18
+ React.createElement(XMarkIcon, { "aria-hidden": true })));
19
+ };
20
+ export default ClearButton;
21
+ //# sourceMappingURL=ClearButton.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ClearButton.js","sourceRoot":"","sources":["../../../src/form/combobox/ClearButton.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAOhD,MAAM,CAAC,MAAM,WAAW,GAA+B,CAAC,EAIvD,EAAE,EAAE;QAJmD,EACtD,WAAW,EACX,gBAAgB,OAEjB,EADI,IAAI,cAH+C,mCAIvD,CADQ;IAEP,OAAO,CACL,8CACE,IAAI,EAAC,QAAQ,EACb,OAAO,EAAE,WAAW,EACpB,SAAS,EAAC,8BAA8B,IACpC,IAAI;QAER,8BAAM,SAAS,EAAC,eAAe,IAC5B,gBAAgB,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,KAAK,CACvC;QACP,oBAAC,SAAS,0BAAe,CAClB,CACV,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,WAAW,CAAC"}
@@ -0,0 +1,4 @@
1
+ import React from "react";
2
+ import { ComboboxProps } from "./types";
3
+ export declare const Combobox: React.ForwardRefExoticComponent<Omit<ComboboxProps, "size" | "onChange" | "options"> & React.RefAttributes<HTMLInputElement>>;
4
+ export default Combobox;
@@ -0,0 +1,50 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import cl from "clsx";
13
+ import React, { forwardRef, useMemo, useRef } from "react";
14
+ import { BodyShort, Label, mergeRefs } from "../..";
15
+ import ClearButton from "./ClearButton";
16
+ import FilteredOptions from "./FilteredOptions/FilteredOptions";
17
+ import { useFilteredOptionsContext } from "./FilteredOptions/filteredOptionsContext";
18
+ import SelectedOptions from "./SelectedOptions/SelectedOptions";
19
+ import ToggleListButton from "./ToggleListButton";
20
+ import { useSelectedOptionsContext } from "./SelectedOptions/selectedOptionsContext";
21
+ import ComboboxWrapper from "./ComboboxWrapper";
22
+ import { useInputContext } from "./Input/inputContext";
23
+ import Input from "./Input/Input";
24
+ export const Combobox = forwardRef((props, ref) => {
25
+ const { value: externalValue, onClear, className, hideLabel = false, description, label, clearButton = true, clearButtonLabel, toggleListButton = true, toggleListButtonLabel, inputClassName, shouldShowSelectedOptions = true } = props, rest = __rest(props, ["value", "onClear", "className", "hideLabel", "description", "label", "clearButton", "clearButtonLabel", "toggleListButton", "toggleListButtonLabel", "inputClassName", "shouldShowSelectedOptions"]);
26
+ const toggleListButtonRef = useRef(null);
27
+ const { currentOption, toggleIsListOpen } = useFilteredOptionsContext();
28
+ const { selectedOptions } = useSelectedOptionsContext();
29
+ const { clearInput, focusInput, hasError, inputDescriptionId, inputProps, inputRef, value, size = "medium", } = useInputContext();
30
+ const mergedInputRef = useMemo(() => mergeRefs([inputRef, ref]), [inputRef, ref]);
31
+ return (React.createElement(ComboboxWrapper, { className: className, hasError: hasError, inputProps: inputProps, inputSize: size, toggleIsListOpen: toggleIsListOpen, toggleListButtonRef: toggleListButtonRef },
32
+ React.createElement(Label, { htmlFor: inputProps.id, size: size, className: cl("navds-form-field__label", {
33
+ "navds-sr-only": hideLabel,
34
+ }) }, label),
35
+ !!description && (React.createElement(BodyShort, { as: "div", className: cl("navds-form-field__description", {
36
+ "navds-sr-only": hideLabel,
37
+ }), id: inputDescriptionId, size: size }, description)),
38
+ React.createElement("div", { className: "navds-combobox__wrapper" },
39
+ React.createElement("div", { className: cl("navds-combobox__wrapper-inner navds-text-field__input", {
40
+ "navds-combobox__wrapper-inner--virtually-unfocused": currentOption !== null,
41
+ }), onClick: focusInput },
42
+ !shouldShowSelectedOptions ? (React.createElement(Input, Object.assign({ id: inputProps.id, ref: mergedInputRef, inputClassName: inputClassName }, rest))) : (React.createElement(SelectedOptions, { selectedOptions: selectedOptions, size: size },
43
+ React.createElement(Input, Object.assign({ id: inputProps.id, ref: mergedInputRef, inputClassName: inputClassName }, rest)))),
44
+ React.createElement("div", null,
45
+ value && clearButton && (React.createElement(ClearButton, { handleClear: clearInput, clearButtonLabel: clearButtonLabel, tabIndex: -1 })),
46
+ toggleListButton && (React.createElement(ToggleListButton, { toggleListButtonLabel: toggleListButtonLabel, ref: toggleListButtonRef })))),
47
+ React.createElement(FilteredOptions, null))));
48
+ });
49
+ export default Combobox;
50
+ //# sourceMappingURL=Combobox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Combobox.js","sourceRoot":"","sources":["../../../src/form/combobox/Combobox.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,MAAM,CAAC;AACtB,OAAO,KAAK,EAAE,EAAE,UAAU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c,IAC1B,IAAI,EACR,CACc,CACnB;gBACD;oBACG,KAAK,IAAI,WAAW,IAAI,CACvB,oBAAC,WAAW,IACV,WAAW,EAAE,UAAU,EACvB,gBAAgB,EAAE,gBAAgB,EAClC,QAAQ,EAAE,CAAC,CAAC,GACZ,CACH;oBACA,gBAAgB,IAAI,CACnB,oBAAC,gBAAgB,IACf,qBAAqB,EAAE,qBAAqB,EAC5C,GAAG,EAAE,mBAAmB,GACxB,CACH,CACG,CACF;YACN,oBAAC,eAAe,OAAG,CACf,CACU,CACnB,CAAC;AACJ,CAAC,CAAC,CAAC;AAEH,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,26 @@
1
+ import React from "react";
2
+ import { ComboboxProps } from "./types";
3
+ /**
4
+ * A component that allows the user to search in a list of options
5
+ *
6
+ * Has options for allowing only one or multiple options to be selected,
7
+ * or adding new, user-submitted values.
8
+ *
9
+ * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/combobox)
10
+ *
11
+ * @example
12
+ * ```jsx
13
+ * const options = ["apple", "banana", "orange"];
14
+ *
15
+ * return (
16
+ * <Combobox
17
+ * label="Velg en verdi"
18
+ * options={options}
19
+ * id="my-combobox"
20
+ * shouldAutoComplete
21
+ * />
22
+ * )
23
+ * ```
24
+ */
25
+ declare const ComboboxProvider: React.ForwardRefExoticComponent<ComboboxProps & React.RefAttributes<HTMLInputElement>>;
26
+ export default ComboboxProvider;
@@ -0,0 +1,72 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import React, { forwardRef } from "react";
13
+ import Combobox from "./Combobox";
14
+ import { FilteredOptionsProvider } from "./FilteredOptions/filteredOptionsContext";
15
+ import { CustomOptionsProvider } from "./customOptionsContext";
16
+ import { SelectedOptionsProvider } from "./SelectedOptions/selectedOptionsContext";
17
+ import { InputContextProvider } from "./Input/inputContext";
18
+ /**
19
+ * A component that allows the user to search in a list of options
20
+ *
21
+ * Has options for allowing only one or multiple options to be selected,
22
+ * or adding new, user-submitted values.
23
+ *
24
+ * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/combobox)
25
+ *
26
+ * @example
27
+ * ```jsx
28
+ * const options = ["apple", "banana", "orange"];
29
+ *
30
+ * return (
31
+ * <Combobox
32
+ * label="Velg en verdi"
33
+ * options={options}
34
+ * id="my-combobox"
35
+ * shouldAutoComplete
36
+ * />
37
+ * )
38
+ * ```
39
+ */
40
+ const ComboboxProvider = forwardRef((props, ref) => {
41
+ const { allowNewValues = false, children, defaultValue, error, errorId, filteredOptions, id, isListOpen, isLoading = false, isMultiSelect, onToggleSelected, selectedOptions, options, value, onChange, onClear, shouldAutocomplete, size } = props, rest = __rest(props, ["allowNewValues", "children", "defaultValue", "error", "errorId", "filteredOptions", "id", "isListOpen", "isLoading", "isMultiSelect", "onToggleSelected", "selectedOptions", "options", "value", "onChange", "onClear", "shouldAutocomplete", "size"]);
42
+ return (React.createElement(InputContextProvider, { value: {
43
+ defaultValue,
44
+ error,
45
+ errorId,
46
+ id,
47
+ value,
48
+ onChange,
49
+ onClear,
50
+ shouldAutocomplete,
51
+ size,
52
+ } },
53
+ React.createElement(CustomOptionsProvider, null,
54
+ React.createElement(SelectedOptionsProvider, { value: {
55
+ allowNewValues,
56
+ isMultiSelect,
57
+ selectedOptions,
58
+ onToggleSelected,
59
+ options,
60
+ } },
61
+ React.createElement(FilteredOptionsProvider, { value: {
62
+ allowNewValues,
63
+ filteredOptions,
64
+ isListOpen,
65
+ isLoading,
66
+ isMultiSelect,
67
+ options,
68
+ } },
69
+ React.createElement(Combobox, Object.assign({ ref: ref }, rest), children))))));
70
+ });
71
+ export default ComboboxProvider;
72
+ //# sourceMappingURL=ComboboxProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ComboboxProvider.js","sourceRoot":"","sources":["../../../src/form/combobox/ComboboxProvider.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,QAAQ,MAAM,YAAY,CAAC;AAClC,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,uBAAuB,EAAE,MAAM,0CAA0C,CAAC;AACnF,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAG5D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,gBAAgB,GAAG,UAAU,CACjC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;IACb,MAAM,EACJ,cAAc,GAAG,KAAK,EACtB,QAAQ,EACR,YAAY,EACZ,KAAK,EACL,OAAO,EACP,eAAe,EACf,EAAE,EACF,UAAU,EACV,SAAS,GAAG,KAAK,EACjB,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,OAAO,EACP,KAAK,EACL,QAAQ,EACR,OAAO,EACP,kBAAkB,EAClB,IAAI,KAEF,KAAK,EADJ,IAAI,UACL,KAAK,EApBH,uPAoBL,CAAQ,CAAC;IACV,OAAO,CACL,oBAAC,oBAAoB,IACnB,KAAK,EAAE;YACL,YAAY;YACZ,KAAK;YACL,OAAO;YACP,EAAE;YACF,KAAK;YACL,QAAQ;YACR,OAAO;YACP,kBAAkB;YAClB,IAAI;SACL;QAED,oBAAC,qBAAqB;YACpB,oBAAC,uBAAuB,IACtB,KAAK,EAAE;oBACL,cAAc;oBACd,aAAa;oBACb,eAAe;oBACf,gBAAgB;oBAChB,OAAO;iBACR;gBAED,oBAAC,uBAAuB,IACtB,KAAK,EAAE;wBACL,cAAc;wBACd,eAAe;wBACf,UAAU;wBACV,SAAS;wBACT,aAAa;wBACb,OAAO;qBACR;oBAED,oBAAC,QAAQ,kBAAC,GAAG,EAAE,GAAG,IAAM,IAAI,GACzB,QAAQ,CACA,CACa,CACF,CACJ,CACH,CACxB,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import React from "react";
2
+ type ComboboxWrapperProps = {
3
+ children: any;
4
+ className?: string;
5
+ hasError: boolean;
6
+ inputProps: {
7
+ disabled?: boolean;
8
+ };
9
+ inputSize: string;
10
+ toggleIsListOpen: (isListOpen: boolean) => void;
11
+ toggleListButtonRef: React.RefObject<HTMLButtonElement>;
12
+ };
13
+ declare const ComboboxWrapper: ({ children, className, hasError, inputProps, inputSize, toggleIsListOpen, toggleListButtonRef, }: ComboboxWrapperProps) => React.JSX.Element;
14
+ export default ComboboxWrapper;
@@ -0,0 +1,24 @@
1
+ import cl from "clsx";
2
+ import React, { useRef } from "react";
3
+ const ComboboxWrapper = ({ children, className, hasError, inputProps, inputSize, toggleIsListOpen, toggleListButtonRef, }) => {
4
+ const wrapperRef = useRef(null);
5
+ function onFocusInsideWrapper(e) {
6
+ var _a;
7
+ if (!((_a = wrapperRef.current) === null || _a === void 0 ? void 0 : _a.contains(e.relatedTarget)) &&
8
+ (toggleListButtonRef === null || toggleListButtonRef === void 0 ? void 0 : toggleListButtonRef.current) !== e.target) {
9
+ toggleIsListOpen(true);
10
+ }
11
+ }
12
+ function onBlurWrapper(e) {
13
+ var _a;
14
+ if (!((_a = wrapperRef.current) === null || _a === void 0 ? void 0 : _a.contains(e.relatedTarget))) {
15
+ toggleIsListOpen(false);
16
+ }
17
+ }
18
+ return (React.createElement("div", { ref: wrapperRef, className: cl(className, "navds-form-field", `navds-form-field--${inputSize}`, "navds-search", {
19
+ "navds-search--error": hasError,
20
+ "navds-search--disabled": !!inputProps.disabled,
21
+ }), onFocus: onFocusInsideWrapper, onBlur: onBlurWrapper }, children));
22
+ };
23
+ export default ComboboxWrapper;
24
+ //# sourceMappingURL=ComboboxWrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ComboboxWrapper.js","sourceRoot":"","sources":["../../../src/form/combobox/ComboboxWrapper.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,MAAM,CAAC;AACtB,OAAO,KAAK,EAAE,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AActC,MAAM,eAAe,GAAG,CAAC,EACvB,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,UAAU,EACV,SAAS,EACT,gBAAgB,EAChB,mBAAmB,GACE,EAAE,EAAE;IACzB,MAAM,UAAU,GAAG,MAAM,CAAwB,IAAI,CAAC,CAAC;IAEvD,SAAS,oBAAoB,CAAC,CAAC;;QAC7B,IACE,CAAC,CAAA,MAAA,UAAU,CAAC,OAAO,0CAAE,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,CAAA;YAC9C,CAAA,mBAAmB,aAAnB,mBAAmB,uBAAnB,mBAAmB,CAAE,OAAO,MAAK,CAAC,CAAC,MAAM,EACzC;YACA,gBAAgB,CAAC,IAAI,CAAC,CAAC;SACxB;IACH,CAAC;IAED,SAAS,aAAa,CAAC,CAAC;;QACtB,IAAI,CAAC,CAAA,MAAA,UAAU,CAAC,OAAO,0CAAE,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,CAAA,EAAE;YAClD,gBAAgB,CAAC,KAAK,CAAC,CAAC;SACzB;IACH,CAAC;IAED,OAAO,CACL,6BACE,GAAG,EAAE,UAAU,EACf,SAAS,EAAE,EAAE,CACX,SAAS,EACT,kBAAkB,EAClB,qBAAqB,SAAS,EAAE,EAChC,cAAc,EACd;YACE,qBAAqB,EAAE,QAAQ;YAC/B,wBAAwB,EAAE,CAAC,CAAC,UAAU,CAAC,QAAQ;SAChD,CACF,EACD,OAAO,EAAE,oBAAoB,EAC7B,MAAM,EAAE,aAAa,IAEpB,QAAQ,CACL,CACP,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,eAAe,CAAC"}
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ declare const CheckIcon: () => React.JSX.Element;
3
+ export default CheckIcon;
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ const CheckIcon = () => {
3
+ return (React.createElement("svg", { width: "16", height: "13", viewBox: "0 0 16 13", fill: "none", xmlns: "http://www.w3.org/2000/svg", "aria-hidden": "true" },
4
+ React.createElement("path", { fillRule: "evenodd", clipRule: "evenodd", fill: "#005B82", d: "M14.2014 0L16 1.89047L4.77943 13L0 8.39552L1.79361 6.5L4.77418 9.3019L14.2014 0Z" })));
5
+ };
6
+ export default CheckIcon;
7
+ //# sourceMappingURL=CheckIcon.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CheckIcon.js","sourceRoot":"","sources":["../../../../src/form/combobox/FilteredOptions/CheckIcon.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,SAAS,GAAG,GAAG,EAAE;IACrB,OAAO,CACL,6BACE,KAAK,EAAC,IAAI,EACV,MAAM,EAAC,IAAI,EACX,OAAO,EAAC,WAAW,EACnB,IAAI,EAAC,MAAM,EACX,KAAK,EAAC,4BAA4B,iBACtB,MAAM;QAElB,8BACE,QAAQ,EAAC,SAAS,EAClB,QAAQ,EAAC,SAAS,EAClB,IAAI,EAAC,SAAS,EACd,CAAC,EAAC,kFAAkF,GACpF,CACE,CACP,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ declare const FilteredOptions: () => React.JSX.Element;
3
+ export default FilteredOptions;
@@ -0,0 +1,42 @@
1
+ import React from "react";
2
+ import cl from "clsx";
3
+ import { BodyShort, Label, Loader } from "../../..";
4
+ import { CheckmarkIcon, PlusIcon } from "@navikt/aksel-icons";
5
+ import { useFilteredOptionsContext } from "./filteredOptionsContext";
6
+ import { useSelectedOptionsContext } from "../SelectedOptions/selectedOptionsContext";
7
+ import { useInputContext } from "../Input/inputContext";
8
+ const FilteredOptions = () => {
9
+ const { inputProps: { id }, size, value, } = useInputContext();
10
+ const { allowNewValues, isLoading, isListOpen, filteredOptions, filteredOptionsIndex, filteredOptionsRef, isValueNew, toggleIsListOpen, } = useFilteredOptionsContext();
11
+ const { isMultiSelect, selectedOptions, toggleOption } = useSelectedOptionsContext();
12
+ return (React.createElement("ul", { ref: filteredOptionsRef, className: cl("navds-combobox__list", {
13
+ "navds-combobox__list--closed": !isListOpen,
14
+ }), id: `${id}-filtered-options`, role: "listbox", tabIndex: -1 },
15
+ isLoading && (React.createElement("li", { className: "navds-combobox__list-item--loading", role: "option", "aria-selected": false, id: `${id}-is-loading` },
16
+ React.createElement(Loader, { "aria-label": "S\u00F8ker..." }))),
17
+ isValueNew && allowNewValues && (React.createElement("li", { tabIndex: -1, onPointerUp: (event) => toggleOption(value, event), id: `${id}-combobox-new-option`, className: cl("navds-combobox__list-item__new-option", {
18
+ "navds-combobox__list-item__new-option--focus": filteredOptionsIndex === -1,
19
+ }), role: "option", "aria-selected": false },
20
+ React.createElement(PlusIcon, { "aria-hidden": true }),
21
+ React.createElement(BodyShort, { size: size },
22
+ "Legg til",
23
+ " ",
24
+ React.createElement(Label, { as: "span", size: size },
25
+ "\u201C",
26
+ value,
27
+ "\u201D")))),
28
+ !isLoading && filteredOptions.length === 0 && (React.createElement("li", { className: "navds-combobox__list-item__no-options", role: "option", "aria-selected": false, id: `${id}-no-hits` }, "Ingen s\u00F8ketreff")),
29
+ filteredOptions.map((option, index) => (React.createElement("li", { className: cl("navds-combobox__list-item", {
30
+ "navds-combobox__list-item--focus": index === filteredOptionsIndex,
31
+ "navds-combobox__list-item--selected": selectedOptions.includes(option),
32
+ }), id: `${id}-option-${option.replace(" ", "-")}`, key: option, tabIndex: -1, onPointerUp: (event) => {
33
+ toggleOption(option, event);
34
+ if (!isMultiSelect) {
35
+ toggleIsListOpen(false);
36
+ }
37
+ }, role: "option", "aria-selected": selectedOptions.includes(option) },
38
+ React.createElement(BodyShort, { size: size }, option),
39
+ selectedOptions.includes(option) && React.createElement(CheckmarkIcon, null))))));
40
+ };
41
+ export default FilteredOptions;
42
+ //# sourceMappingURL=FilteredOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FilteredOptions.js","sourceRoot":"","sources":["../../../../src/form/combobox/FilteredOptions/FilteredOptions.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,MAAM,CAAC;AACtB,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;AACtF,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,MAAM,eAAe,GAAG,GAAG,EAAE;IAC3B,MAAM,EACJ,UAAU,EAAE,EAAE,EAAE,EAAE,EAClB,IAAI,EACJ,KAAK,GACN,GAAG,eAAe,EAAE,CAAC;IACtB,MAAM,EACJ,cAAc,EACd,SAAS,EACT,UAAU,EACV,eAAe,EACf,oBAAoB,EACpB,kBAAkB,EAClB,UAAU,EACV,gBAAgB,GACjB,GAAG,yBAAyB,EAAE,CAAC;IAChC,MAAM,EAAE,aAAa,EAAE,eAAe,EAAE,YAAY,EAAE,GACpD,yBAAyB,EAAE,CAAC;IAE9B,OAAO,CACL,4BACE,GAAG,EAAE,kBAAkB,EACvB,SAAS,EAAE,EAAE,CAAC,sBAAsB,EAAE;YACpC,8BAA8B,EAAE,CAAC,UAAU;SAC5C,CAAC,EACF,EAAE,EAAE,GAAG,EAAE,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AAC,GAAG,EAAE,GAAG,CAAC,EAAE,EAC9C,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,CAAC,CAAC,EACZ,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE;gBACrB,YAAY,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;gBAC5B,IAAI,CAAC,aAAa,EAAE;oBAClB,gBAAgB,CAAC,KAAK,CAAC,CAAC;iBACzB;YACH,CAAC,EACD,IAAI,EAAC,QAAQ,mBACE,eAAe,CAAC,QAAQ,CAAC,MAAM,CAAC;YAE/C,oBAAC,SAAS,IAAC,IAAI,EAAE,IAAI,IAAG,MAAM,CAAa;YAC1C,eAAe,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,oBAAC,aAAa,OAAG,CACnD,CACN,CAAC,CACC,CACN,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,eAAe,CAAC"}
@@ -0,0 +1,27 @@
1
+ import React from "react";
2
+ type FilteredOptionsContextType = {
3
+ activeDecendantId?: string;
4
+ allowNewValues?: boolean;
5
+ ariaDescribedBy?: string;
6
+ filteredOptionsRef: React.RefObject<HTMLUListElement>;
7
+ filteredOptionsIndex: number | null;
8
+ setFilteredOptionsIndex: (index: number) => void;
9
+ isListOpen: boolean;
10
+ isLoading?: boolean;
11
+ filteredOptions: string[];
12
+ isValueNew: boolean;
13
+ toggleIsListOpen: (newState?: boolean) => void;
14
+ currentOption: string | null;
15
+ resetFilteredOptionsIndex: () => void;
16
+ moveFocusUp: () => void;
17
+ moveFocusDown: () => void;
18
+ moveFocusToInput: () => void;
19
+ moveFocusToEnd: () => void;
20
+ shouldAutocomplete?: boolean;
21
+ };
22
+ export declare const FilteredOptionsProvider: ({ children, value: props }: {
23
+ children: any;
24
+ value: any;
25
+ }) => React.JSX.Element;
26
+ export declare const useFilteredOptionsContext: () => FilteredOptionsContextType;
27
+ export {};
@@ -0,0 +1,178 @@
1
+ import React, { useState, useEffect, useMemo, createContext, useContext, useCallback, useRef, useLayoutEffect, } from "react";
2
+ import { useCustomOptionsContext } from "../customOptionsContext";
3
+ import { useInputContext } from "../Input/inputContext";
4
+ import usePrevious from "../../../util/usePrevious";
5
+ const normalizeText = (text) => typeof text === "string" ? `${text}`.toLowerCase().trim() : "";
6
+ const isPartOfText = (value, text) => normalizeText(text).startsWith(normalizeText(value !== null && value !== void 0 ? value : ""));
7
+ const isValueInList = (value, list) => list === null || list === void 0 ? void 0 : list.find((listItem) => normalizeText(value) === normalizeText(listItem));
8
+ const getMatchingValuesFromList = (value, list) => list === null || list === void 0 ? void 0 : list.filter((listItem) => isPartOfText(value, listItem));
9
+ const FilteredOptionsContext = createContext({});
10
+ export const FilteredOptionsProvider = ({ children, value: props }) => {
11
+ const { allowNewValues, filteredOptions: externalFilteredOptions, isListOpen: isExternalListOpen, isLoading, options, } = props;
12
+ const filteredOptionsRef = useRef(null);
13
+ const { inputProps: { id }, value, searchTerm, setValue, setSearchTerm, shouldAutocomplete, } = useInputContext();
14
+ const [filteredOptionsIndex, setFilteredOptionsIndex] = useState(null);
15
+ const [isInternalListOpen, setInternalListOpen] = useState(false);
16
+ const { customOptions } = useCustomOptionsContext();
17
+ const filteredOptions = useMemo(() => {
18
+ if (externalFilteredOptions) {
19
+ return externalFilteredOptions;
20
+ }
21
+ const opts = [...customOptions, ...options];
22
+ setFilteredOptionsIndex(null);
23
+ return getMatchingValuesFromList(searchTerm, opts);
24
+ }, [customOptions, externalFilteredOptions, options, searchTerm]);
25
+ const previousSearchTerm = usePrevious(searchTerm);
26
+ useLayoutEffect(() => {
27
+ if (shouldAutocomplete &&
28
+ normalizeText(searchTerm) !== "" &&
29
+ ((previousSearchTerm === null || previousSearchTerm === void 0 ? void 0 : previousSearchTerm.length) || 0) < searchTerm.length &&
30
+ filteredOptions.length > 0 &&
31
+ !isValueInList(searchTerm, filteredOptions)) {
32
+ setValue(`${searchTerm}${filteredOptions[0].substring(searchTerm.length)}`);
33
+ setSearchTerm(searchTerm);
34
+ }
35
+ }, [
36
+ filteredOptions,
37
+ previousSearchTerm,
38
+ searchTerm,
39
+ setSearchTerm,
40
+ setValue,
41
+ shouldAutocomplete,
42
+ ]);
43
+ const isListOpen = useMemo(() => {
44
+ return isExternalListOpen !== null && isExternalListOpen !== void 0 ? isExternalListOpen : isInternalListOpen;
45
+ }, [isExternalListOpen, isInternalListOpen]);
46
+ const toggleIsListOpen = useCallback((newState) => {
47
+ setFilteredOptionsIndex(null);
48
+ setInternalListOpen((oldState) => newState !== null && newState !== void 0 ? newState : !oldState);
49
+ }, []);
50
+ const isValueNew = useMemo(() => Boolean(value) && !isValueInList(value, filteredOptions), [value, filteredOptions]);
51
+ const getMinimumIndex = useCallback(() => {
52
+ return isValueNew && allowNewValues ? -1 : 0;
53
+ }, [allowNewValues, isValueNew]);
54
+ const ariaDescribedBy = useMemo(() => {
55
+ if (!isLoading && filteredOptions.length === 0) {
56
+ return `${id}-no-hits`;
57
+ }
58
+ else if ((value && value !== "") || isLoading) {
59
+ if (shouldAutocomplete && filteredOptions[0]) {
60
+ return `${id}-option-${filteredOptions[0].replace(" ", "-")}`;
61
+ }
62
+ else if (isLoading) {
63
+ return `${id}-is-loading`;
64
+ }
65
+ }
66
+ else {
67
+ return undefined;
68
+ }
69
+ }, [isLoading, value, shouldAutocomplete, filteredOptions, id]);
70
+ const currentOption = useMemo(() => {
71
+ if (filteredOptionsIndex == null) {
72
+ return null;
73
+ }
74
+ if (filteredOptionsIndex === -1) {
75
+ return value;
76
+ }
77
+ return filteredOptions[filteredOptionsIndex];
78
+ }, [filteredOptionsIndex, filteredOptions, value]);
79
+ const resetFilteredOptionsIndex = () => {
80
+ setFilteredOptionsIndex(getMinimumIndex());
81
+ };
82
+ const scrollToOption = useCallback((newIndex) => {
83
+ if (filteredOptionsRef.current &&
84
+ filteredOptionsRef.current.children[newIndex]) {
85
+ const child = filteredOptionsRef.current.children[newIndex];
86
+ const { top, bottom } = child.getBoundingClientRect();
87
+ const parentRect = filteredOptionsRef.current.getBoundingClientRect();
88
+ if (top < parentRect.top || bottom > parentRect.bottom) {
89
+ child.scrollIntoView({ block: "nearest" });
90
+ }
91
+ }
92
+ }, []);
93
+ useEffect(() => {
94
+ if (filteredOptionsIndex !== null && isListOpen) {
95
+ scrollToOption(filteredOptionsIndex);
96
+ }
97
+ }, [filteredOptionsIndex, isListOpen, scrollToOption]);
98
+ const moveFocusToInput = useCallback(() => {
99
+ setFilteredOptionsIndex(null);
100
+ toggleIsListOpen(false);
101
+ }, [toggleIsListOpen]);
102
+ const moveFocusToEnd = useCallback(() => {
103
+ const lastIndex = filteredOptions.length - 1;
104
+ toggleIsListOpen(true);
105
+ setFilteredOptionsIndex(lastIndex);
106
+ }, [filteredOptions.length, toggleIsListOpen]);
107
+ const moveFocusUp = useCallback(() => {
108
+ if (filteredOptionsIndex === null) {
109
+ return;
110
+ }
111
+ if (filteredOptionsIndex === getMinimumIndex()) {
112
+ toggleIsListOpen(false);
113
+ setFilteredOptionsIndex(null);
114
+ }
115
+ else {
116
+ const newIndex = Math.max(getMinimumIndex(), filteredOptionsIndex - 1);
117
+ setFilteredOptionsIndex(newIndex);
118
+ }
119
+ }, [filteredOptionsIndex, getMinimumIndex, toggleIsListOpen]);
120
+ const moveFocusDown = useCallback(() => {
121
+ if (filteredOptionsIndex === null || !isListOpen) {
122
+ toggleIsListOpen(true);
123
+ if (allowNewValues || filteredOptions.length >= 1) {
124
+ setFilteredOptionsIndex(getMinimumIndex());
125
+ }
126
+ return;
127
+ }
128
+ const newIndex = Math.min(filteredOptionsIndex + 1, Math.max(getMinimumIndex(), filteredOptions.length - 1));
129
+ setFilteredOptionsIndex(newIndex);
130
+ }, [
131
+ allowNewValues,
132
+ filteredOptions.length,
133
+ filteredOptionsIndex,
134
+ getMinimumIndex,
135
+ isListOpen,
136
+ toggleIsListOpen,
137
+ ]);
138
+ const activeDecendantId = useMemo(() => {
139
+ if (filteredOptionsIndex === null) {
140
+ return undefined;
141
+ }
142
+ else if (filteredOptionsIndex === -1) {
143
+ return `${id}-combobox-new-option`;
144
+ }
145
+ else {
146
+ return `${id}-option-${currentOption === null || currentOption === void 0 ? void 0 : currentOption.replace(" ", "-")}`;
147
+ }
148
+ }, [filteredOptionsIndex, currentOption, id]);
149
+ const filteredOptionsState = {
150
+ activeDecendantId,
151
+ allowNewValues,
152
+ filteredOptionsRef,
153
+ filteredOptionsIndex,
154
+ setFilteredOptionsIndex,
155
+ shouldAutocomplete,
156
+ isListOpen,
157
+ isLoading,
158
+ filteredOptions,
159
+ isValueNew,
160
+ toggleIsListOpen,
161
+ currentOption,
162
+ resetFilteredOptionsIndex,
163
+ moveFocusUp,
164
+ moveFocusDown,
165
+ moveFocusToInput,
166
+ moveFocusToEnd,
167
+ ariaDescribedBy,
168
+ };
169
+ return (React.createElement(FilteredOptionsContext.Provider, { value: filteredOptionsState }, children));
170
+ };
171
+ export const useFilteredOptionsContext = () => {
172
+ const context = useContext(FilteredOptionsContext);
173
+ if (!context) {
174
+ throw new Error("useFilteredOptionsContext must be used within a FilteredOptionsProvider");
175
+ }
176
+ return context;
177
+ };
178
+ //# sourceMappingURL=filteredOptionsContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filteredOptionsContext.js","sourceRoot":"","sources":["../../../../src/form/combobox/FilteredOptions/filteredOptionsContext.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EACZ,QAAQ,EACR,SAAS,EACT,OAAO,EACP,aAAa,EACb,UAAU,EACV,WAAW,EACX,MAAM,EACN,eAAe,GAChB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,WAAW,MAAM,2BAA2B,CAAC;AAEpD,MAAM,aAAa,GAAG,CAAC,IAAY,EAAU,EAAE,CAC7C,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,IA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AI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,yEAAyE,CAC1E,CAAC;KACH;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import React, { InputHTMLAttributes } from "react";
2
+ interface InputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "value"> {
3
+ ref: React.Ref<HTMLInputElement>;
4
+ inputClassName?: string;
5
+ errorId?: string;
6
+ value?: string;
7
+ error?: React.ReactNode;
8
+ }
9
+ declare const Input: React.ForwardRefExoticComponent<Omit<InputProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
10
+ export default Input;