@olenbetong/appframe-ds 1.5.0 → 1.6.1

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 (159) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/package.json +17 -16
  3. package/scripts/copyAssets.mjs +27 -27
  4. package/src/AfLookup/Lookup.css +147 -130
  5. package/src/AfLookup/Lookup.tsx +186 -186
  6. package/src/AfLookup/LookupCombobox.css +69 -72
  7. package/src/AfLookup/LookupCombobox.tsx +273 -237
  8. package/src/AfLookup/LookupEdit.tsx +130 -130
  9. package/src/AfLookup/LookupGrid.css +145 -52
  10. package/src/AfLookup/LookupGrid.test.tsx +142 -0
  11. package/src/AfLookup/LookupGrid.tsx +324 -124
  12. package/src/AfLookup/index.ts +5 -5
  13. package/src/autocomplete/Autocomplete.css +253 -256
  14. package/src/autocomplete/Autocomplete.tsx +320 -320
  15. package/src/autocomplete/AutocompleteFrame.tsx +168 -168
  16. package/src/autocomplete/Combobox.tsx +286 -286
  17. package/src/autocomplete/index.ts +4 -4
  18. package/src/autocomplete/types.ts +204 -204
  19. package/src/autocomplete/utils.ts +183 -183
  20. package/src/binding/BoundNumberInput.tsx +42 -0
  21. package/src/binding/BoundNumericTextField.tsx +102 -102
  22. package/src/binding/BoundTextField.tsx +49 -49
  23. package/src/binding/CancelButton.tsx +30 -30
  24. package/src/binding/CancelIconButton.tsx +30 -30
  25. package/src/binding/DataEditToolbar.css +23 -23
  26. package/src/binding/DataEditToolbar.tsx +84 -84
  27. package/src/binding/DeleteButton.tsx +35 -35
  28. package/src/binding/DeleteIconButton.tsx +35 -35
  29. package/src/binding/RefreshButton.tsx +31 -31
  30. package/src/binding/RefreshIconButton.tsx +33 -33
  31. package/src/binding/RefreshRowIconButton.tsx +33 -33
  32. package/src/binding/SaveButton.tsx +37 -37
  33. package/src/binding/SaveIconButton.tsx +38 -38
  34. package/src/binding/index.ts +20 -19
  35. package/src/container/Container.tsx +50 -50
  36. package/src/container/index.ts +1 -1
  37. package/src/cutoff/CutOffPicker.css +12 -12
  38. package/src/cutoff/CutOffPicker.tsx +165 -165
  39. package/src/cutoff/context.tsx +194 -194
  40. package/src/cutoff/dsCutOffDates.ts +21 -21
  41. package/src/cutoff/index.ts +4 -4
  42. package/src/cutoff/utils.test.ts +59 -59
  43. package/src/cutoff/utils.ts +276 -276
  44. package/src/filter/ChipListInput.tsx +79 -79
  45. package/src/filter/FieldFilterPanel.css +150 -150
  46. package/src/filter/FieldFilterPanel.tsx +309 -309
  47. package/src/filter/FilterBuilder.css +152 -152
  48. package/src/filter/FilterBuilder.test.tsx +153 -153
  49. package/src/filter/FilterBuilder.tsx +435 -435
  50. package/src/filter/FilterEditor.css +231 -231
  51. package/src/filter/FilterEditor.test.tsx +285 -285
  52. package/src/filter/FilterEditor.tsx +75 -75
  53. package/src/filter/FilterGroupEditor.tsx +160 -160
  54. package/src/filter/FilterNameDialog.css +22 -22
  55. package/src/filter/FilterNameDialog.tsx +81 -81
  56. package/src/filter/FilterRow.tsx +117 -117
  57. package/src/filter/FilterShareDialog.css +28 -28
  58. package/src/filter/FilterShareDialog.tsx +201 -201
  59. package/src/filter/FilterStringField.tsx +78 -78
  60. package/src/filter/FilterValueEditor.tsx +239 -239
  61. package/src/filter/SavedFilterTree.css +107 -107
  62. package/src/filter/SavedFilterTree.test.tsx +94 -94
  63. package/src/filter/SavedFilterTree.tsx +218 -218
  64. package/src/filter/fieldFilter.test.ts +158 -158
  65. package/src/filter/fieldFilter.ts +139 -139
  66. package/src/filter/index.ts +27 -27
  67. package/src/filter/types.ts +38 -38
  68. package/src/filter/useDistinctValues.test.ts +102 -102
  69. package/src/filter/useDistinctValues.ts +230 -230
  70. package/src/global.d.ts +11 -11
  71. package/src/grid/formatters.ts +23 -0
  72. package/src/grid/useAfColumns.tsx +28 -4
  73. package/src/grid/utcDate.test.ts +25 -0
  74. package/src/grid/utcDate.ts +28 -0
  75. package/src/grid/utcDateEditing.tsx +11 -0
  76. package/src/i18n.ts +13 -13
  77. package/src/index.ts +14 -14
  78. package/src/input/InputAdornments.css +80 -84
  79. package/src/input/InputAdornments.tsx +102 -102
  80. package/src/input/NumberInput.css +9 -0
  81. package/src/input/NumberInput.test.tsx +212 -0
  82. package/src/input/NumberInput.tsx +268 -0
  83. package/src/input/index.ts +3 -1
  84. package/src/input/numberFormat.test.ts +79 -0
  85. package/src/input/numberFormat.ts +94 -0
  86. package/src/layout/AppMenu.tsx +74 -74
  87. package/src/layout/AppMenuDrawer.css +71 -71
  88. package/src/layout/AppMenuDrawer.tsx +122 -122
  89. package/src/layout/DialogMotion.css +79 -79
  90. package/src/layout/ErrorBoundary.tsx +162 -162
  91. package/src/layout/PageContainer.css +48 -48
  92. package/src/layout/PageContainer.tsx +41 -41
  93. package/src/layout/ResponsiveDialog.css +86 -86
  94. package/src/layout/ResponsiveDialog.tsx +55 -55
  95. package/src/layout/index.tsx +70 -70
  96. package/src/link/Link.tsx +23 -23
  97. package/src/link/index.ts +1 -1
  98. package/src/mdi/Mdi.css +138 -138
  99. package/src/mdi/Mdi.test.tsx +91 -91
  100. package/src/mdi/Mdi.tsx +141 -141
  101. package/src/mdi/MdiConfirmCloseDialog.tsx +47 -47
  102. package/src/mdi/MdiContext.tsx +59 -59
  103. package/src/mdi/MdiDocumentContext.tsx +60 -60
  104. package/src/mdi/MdiPanel.tsx +61 -61
  105. package/src/mdi/MdiRestore.test.tsx +86 -86
  106. package/src/mdi/MdiTab.tsx +30 -30
  107. package/src/mdi/controller.test.ts +382 -382
  108. package/src/mdi/controller.ts +656 -656
  109. package/src/mdi/dockviewHost.ts +132 -132
  110. package/src/mdi/fakeHost.ts +200 -200
  111. package/src/mdi/host.ts +44 -44
  112. package/src/mdi/ids.test.ts +20 -20
  113. package/src/mdi/ids.ts +18 -18
  114. package/src/mdi/index.ts +31 -31
  115. package/src/mdi/layoutRestore.test.ts +156 -156
  116. package/src/mdi/layoutRestore.ts +311 -311
  117. package/src/mdi/layoutSpec.test.ts +143 -143
  118. package/src/mdi/layoutSpec.ts +214 -214
  119. package/src/mdi/layoutStore.test.ts +93 -93
  120. package/src/mdi/layoutStore.ts +99 -99
  121. package/src/mdi/memorySettingsStore.ts +21 -21
  122. package/src/mdi/registry.ts +34 -34
  123. package/src/mdi/testTypes.ts +37 -37
  124. package/src/mdi/theme.ts +22 -22
  125. package/src/mdi/types.ts +214 -214
  126. package/src/page/Page.css +57 -57
  127. package/src/page/PageBlock.css +64 -64
  128. package/src/page/PageBlock.tsx +93 -93
  129. package/src/page/PageHeader.css +88 -88
  130. package/src/page/PageHeader.tsx +115 -115
  131. package/src/page/PageTitle.css +5 -5
  132. package/src/page/PageTitle.tsx +21 -21
  133. package/src/page/index.ts +4 -4
  134. package/src/paper/Paper.tsx +52 -52
  135. package/src/progress/LinearProgress.css +121 -93
  136. package/src/progress/LinearProgress.tsx +106 -104
  137. package/src/progress/index.ts +1 -1
  138. package/src/report-downloader/ReportDownloader.css +67 -67
  139. package/src/report-downloader/components/StatusItem.tsx +87 -87
  140. package/src/report-downloader/components/StatusList.tsx +28 -28
  141. package/src/report-downloader/index.ts +41 -41
  142. package/src/report-downloader/status.tsx +92 -92
  143. package/src/sortable/DropIndicator.css +51 -51
  144. package/src/sortable/DropIndicator.tsx +27 -27
  145. package/src/sortable/SortableItemList.css +21 -21
  146. package/src/sortable/SortableItemList.tsx +264 -264
  147. package/src/sortable/index.ts +8 -8
  148. package/src/sortable/reorderSortOrder.ts +126 -126
  149. package/src/sortable/sortOrder.ts +101 -101
  150. package/src/sortable/types.ts +9 -9
  151. package/src/sortable/useSortableContainer.ts +53 -53
  152. package/src/sortable/useSortableItem.ts +95 -95
  153. package/src/sortable/useSortableList.ts +117 -117
  154. package/src/test/happy-dom-document-class.ts +24 -24
  155. package/src/test/setup.ts +27 -12
  156. package/src/theme/useDsColorScheme.ts +53 -53
  157. package/tsconfig.build.json +9 -9
  158. package/tsconfig.json +8 -8
  159. package/vitest.config.ts +10 -10
package/src/i18n.ts CHANGED
@@ -1,13 +1,13 @@
1
- /*
2
- * The package's strings live in their own globalization project, `appframe-ds`, so one
3
- * translation covers every app and the package can word a term its own way. The templates
4
- * render the dictionary as af.i18n["appframe-ds"]; on a page that does not (a legacy
5
- * template) the localizer falls back to the article's strings, exactly as before. See
6
- * `createLocalizer` in @olenbetong/appframe-core.
7
- */
8
-
9
- import { createLocalizer } from "@olenbetong/appframe-core";
10
-
11
- export const PROJECT = "appframe-ds";
12
-
13
- export const { getLocalizedString, localize } = createLocalizer(PROJECT);
1
+ /*
2
+ * The package's strings live in their own globalization project, `appframe-ds`, so one
3
+ * translation covers every app and the package can word a term its own way. The templates
4
+ * render the dictionary as af.i18n["appframe-ds"]; on a page that does not (a legacy
5
+ * template) the localizer falls back to the article's strings, exactly as before. See
6
+ * `createLocalizer` in @olenbetong/appframe-core.
7
+ */
8
+
9
+ import { createLocalizer } from "@olenbetong/appframe-core";
10
+
11
+ export const PROJECT = "appframe-ds";
12
+
13
+ export const { getLocalizedString, localize } = createLocalizer(PROJECT);
package/src/index.ts CHANGED
@@ -1,14 +1,14 @@
1
- export * from "./AfLookup/index.js";
2
- export * from "./autocomplete/index.js";
3
- export * from "./binding/index.js";
4
- export * from "./container/index.js";
5
- export * from "./cutoff/index.js";
6
- export * from "./filter/index.js";
7
- export * from "./input/index.js";
8
- export * from "./layout/index.js";
9
- export * from "./link/index.js";
10
- export * from "./list/index.js";
11
- export * from "./page/index.js";
12
- export * from "./paper/index.js";
13
- export * from "./progress/index.js";
14
- export * from "./sortable/index.js";
1
+ export * from "./AfLookup/index.js";
2
+ export * from "./autocomplete/index.js";
3
+ export * from "./binding/index.js";
4
+ export * from "./container/index.js";
5
+ export * from "./cutoff/index.js";
6
+ export * from "./filter/index.js";
7
+ export * from "./input/index.js";
8
+ export * from "./layout/index.js";
9
+ export * from "./link/index.js";
10
+ export * from "./list/index.js";
11
+ export * from "./page/index.js";
12
+ export * from "./paper/index.js";
13
+ export * from "./progress/index.js";
14
+ export * from "./sortable/index.js";
@@ -1,84 +1,80 @@
1
- /*
2
- * The wrapper is the visual input: it owns the border, background, hover and
3
- * focus styling, while the inner Designsystemet input is rendered bare so the
4
- * input and the adornments read as a single control.
5
- */
6
- .ObInputAdornments-root {
7
- --obInputAdornments-padding: 0.25rem;
8
- --obInputAdornments-gap: 0.25rem;
9
- --obInputAdornments-fontSize: var(--ds-font-size-3, 1rem);
10
-
11
- display: flex;
12
- align-items: center;
13
- box-sizing: border-box;
14
- gap: var(--obInputAdornments-gap);
15
- padding: var(--obInputAdornments-padding);
16
- inline-size: 100%;
17
- color: var(--ds-color-neutral-text-default, #1e2b3c);
18
- background-color: var(--ds-color-neutral-surface-default, #fff);
19
- border: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
20
- border-radius: var(--ds-border-radius-md, 0.375rem);
21
- }
22
-
23
- .ObInputAdornments-root[data-size="xs"] {
24
- --obInputAdornments-padding: 0.125rem;
25
- --obInputAdornments-fontSize: var(--ds-font-size-1, 0.75rem);
26
- }
27
-
28
- .ObInputAdornments-root[data-size="sm"] {
29
- --obInputAdornments-padding: 0.25rem;
30
- --obInputAdornments-fontSize: var(--ds-font-size-2, 0.875rem);
31
- }
32
-
33
- .ObInputAdornments-root[data-size="md"] {
34
- --obInputAdornments-padding: 0.25rem;
35
- --obInputAdornments-fontSize: var(--ds-font-size-3, 1rem);
36
- }
37
-
38
- .ObInputAdornments-root[data-size="lg"] {
39
- --obInputAdornments-padding: 0.375rem;
40
- --obInputAdornments-fontSize: var(--ds-font-size-4, 1.125rem);
41
- }
42
-
43
- /* The Designsystemet input hover (an inner outline doubling the border), drawn on the wrapper */
44
- .ObInputAdornments-root:hover:not(
45
- :focus-within,
46
- :has(.ObInputAdornments-field:disabled, .ObInputAdornments-field[readonly])
47
- ) {
48
- outline: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
49
- outline-offset: -0.1px;
50
- }
51
-
52
- .ObInputAdornments-root:has(.ObInputAdornments-field:focus-visible) {
53
- box-shadow: var(--dsc-focus-boxShadow);
54
- outline: var(--dsc-focus-outline);
55
- outline-offset: var(--ds-border-width-focus, 3px);
56
- }
57
-
58
- .ObInputAdornments-root:has(.ObInputAdornments-field:disabled) {
59
- cursor: not-allowed;
60
- opacity: var(--ds-disabled-opacity, 0.3);
61
- }
62
-
63
- /* The bare input: all the visual styling lives on the wrapper instead */
64
- .ObInputAdornments-root .ObInputAdornments-field {
65
- flex: 1;
66
- min-inline-size: 0;
67
- font-size: var(--obInputAdornments-fontSize);
68
- background: transparent;
69
- border: none;
70
- }
71
-
72
- .ObInputAdornments-root .ObInputAdornments-field:disabled {
73
- opacity: 1;
74
- }
75
-
76
- .ObInputAdornments-root .ObInputAdornments-field:hover {
77
- outline: none;
78
- }
79
-
80
- .ObInputAdornments-root .ObInputAdornments-field:focus-visible {
81
- box-shadow: none;
82
- outline: none;
83
- outline-offset: 0;
84
- }
1
+ /*
2
+ * The wrapper is the visual input: it owns the border, background, hover and
3
+ * focus styling, while the inner Designsystemet input is rendered bare so the
4
+ * input and the adornments read as a single control.
5
+ */
6
+ .ObInputAdornments-root {
7
+ --obInputAdornments-padding: 0.25rem;
8
+ --obInputAdornments-gap: 0.25rem;
9
+ --obInputAdornments-fontSize: var(--ds-font-size-3, 1rem);
10
+
11
+ display: flex;
12
+ align-items: center;
13
+ box-sizing: border-box;
14
+ gap: var(--obInputAdornments-gap);
15
+ padding: var(--obInputAdornments-padding);
16
+ inline-size: 100%;
17
+ color: var(--ds-color-neutral-text-default, #1e2b3c);
18
+ background-color: var(--ds-color-neutral-surface-default, #fff);
19
+ border: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
20
+ border-radius: var(--ds-border-radius-md, 0.375rem);
21
+ }
22
+
23
+ /* Designsystemet components come in sm, md and lg; xs and xl are typography-only sizes */
24
+ .ObInputAdornments-root[data-size="sm"] {
25
+ --obInputAdornments-padding: 0.25rem;
26
+ --obInputAdornments-fontSize: var(--ds-font-size-2, 0.875rem);
27
+ }
28
+
29
+ .ObInputAdornments-root[data-size="md"] {
30
+ --obInputAdornments-padding: 0.25rem;
31
+ --obInputAdornments-fontSize: var(--ds-font-size-3, 1rem);
32
+ }
33
+
34
+ .ObInputAdornments-root[data-size="lg"] {
35
+ --obInputAdornments-padding: 0.375rem;
36
+ --obInputAdornments-fontSize: var(--ds-font-size-4, 1.125rem);
37
+ }
38
+
39
+ /* The Designsystemet input hover (an inner outline doubling the border), drawn on the wrapper */
40
+ .ObInputAdornments-root:hover:not(
41
+ :focus-within,
42
+ :has(.ObInputAdornments-field:disabled, .ObInputAdornments-field[readonly])
43
+ ) {
44
+ outline: var(--ds-border-width-default, 1px) solid var(--ds-color-neutral-border-default, #b3b3b3);
45
+ outline-offset: -0.1px;
46
+ }
47
+
48
+ .ObInputAdornments-root:has(.ObInputAdornments-field:focus-visible) {
49
+ box-shadow: var(--dsc-focus-boxShadow);
50
+ outline: var(--dsc-focus-outline);
51
+ outline-offset: var(--ds-border-width-focus, 3px);
52
+ }
53
+
54
+ .ObInputAdornments-root:has(.ObInputAdornments-field:disabled) {
55
+ cursor: not-allowed;
56
+ opacity: var(--ds-disabled-opacity, 0.3);
57
+ }
58
+
59
+ /* The bare input: all the visual styling lives on the wrapper instead */
60
+ .ObInputAdornments-root .ObInputAdornments-field {
61
+ flex: 1;
62
+ min-inline-size: 0;
63
+ font-size: var(--obInputAdornments-fontSize);
64
+ background: transparent;
65
+ border: none;
66
+ }
67
+
68
+ .ObInputAdornments-root .ObInputAdornments-field:disabled {
69
+ opacity: 1;
70
+ }
71
+
72
+ .ObInputAdornments-root .ObInputAdornments-field:hover {
73
+ outline: none;
74
+ }
75
+
76
+ .ObInputAdornments-root .ObInputAdornments-field:focus-visible {
77
+ box-shadow: none;
78
+ outline: none;
79
+ outline-offset: 0;
80
+ }
@@ -1,102 +1,102 @@
1
- import "./InputAdornments.css";
2
-
3
- import { Input, type InputProps } from "@digdir/designsystemet-react";
4
- import clsx from "clsx";
5
- import { Children, isValidElement, type ReactElement, type ReactNode, type Ref } from "react";
6
-
7
- export type InputSlotProps = {
8
- /**
9
- * Extra classes for the input rendered in this slot
10
- */
11
- className?: string;
12
- };
13
-
14
- /**
15
- * Marks where the input should be rendered among the children of
16
- * `InputAdornments`. Renders nothing on its own; `InputAdornments` swaps it
17
- * for the actual input. When it is left out, the input is rendered before the
18
- * adornments.
19
- *
20
- * @example
21
- * ```jsx
22
- * <InputAdornments value={value} onChange={handleChange}>
23
- * <SearchIcon aria-hidden />
24
- * <InputSlot />
25
- * <Button icon variant="tertiary"><XMarkIcon aria-hidden /></Button>
26
- * </InputAdornments>
27
- * ```
28
- */
29
- export function InputSlot(_props: InputSlotProps) {
30
- return null;
31
- }
32
-
33
- export type InputAdornmentsProps = Omit<InputProps, "children"> & {
34
- /**
35
- * Adornments rendered next to the input, e.g. icons or buttons. Include an
36
- * `InputSlot` to control where the input itself ends up.
37
- */
38
- children?: ReactNode;
39
- /**
40
- * Classes for the wrapper that carries the input styling
41
- */
42
- className?: string;
43
- /**
44
- * Classes for the bare input element itself
45
- */
46
- inputClassName?: string;
47
- /**
48
- * Ref to the input element. The wrapper is an implementation detail, so the
49
- * ref points at the input rather than at the element carrying the styling.
50
- */
51
- ref?: Ref<HTMLInputElement>;
52
- };
53
-
54
- /**
55
- * Input with adornments, equivalent to a MUI `TextField` with
56
- * `InputProps.startAdornment` / `endAdornment`. Designsystemet has no
57
- * equivalent, so the border, background, hover and focus styling is moved from
58
- * the input onto a wrapper element, making the input and its adornments read
59
- * as a single control.
60
- *
61
- * All other props are forwarded to the Designsystemet `Input`, so `ref`,
62
- * `value`, `onChange`, `data-size` and `data-color` work as usual.
63
- *
64
- * @example
65
- * ```jsx
66
- * // Input first (default), adornments after
67
- * <InputAdornments aria-label="Search" value={value} onChange={handleChange}>
68
- * <Button icon variant="tertiary"><MagnifyingGlassIcon aria-hidden /></Button>
69
- * </InputAdornments>
70
- *
71
- * // Explicit placement with InputSlot
72
- * <InputAdornments aria-label="Amount" data-size="lg">
73
- * <span aria-hidden>kr</span>
74
- * <InputSlot />
75
- * <span aria-hidden>,-</span>
76
- * </InputAdornments>
77
- * ```
78
- */
79
- export function InputAdornments({ children, className, inputClassName, ...props }: InputAdornmentsProps) {
80
- let size = props["data-size"];
81
- let color = props["data-color"];
82
- let childArray = Children.toArray(children);
83
- let slot = childArray.find((child) => isValidElement(child) && child.type === InputSlot) as
84
- | ReactElement<InputSlotProps>
85
- | undefined;
86
-
87
- let input = (
88
- <Input
89
- key="ObInputAdornments-input"
90
- {...props}
91
- className={clsx("ObInputAdornments-field", inputClassName, slot?.props.className)}
92
- />
93
- );
94
-
95
- let content = slot ? childArray.map((child) => (child === slot ? input : child)) : [input, ...childArray];
96
-
97
- return (
98
- <div className={clsx("ObInputAdornments-root", className)} data-size={size} data-color={color}>
99
- {content}
100
- </div>
101
- );
102
- }
1
+ import "./InputAdornments.css";
2
+
3
+ import { Input, type InputProps } from "@digdir/designsystemet-react";
4
+ import clsx from "clsx";
5
+ import { Children, isValidElement, type ReactElement, type ReactNode, type Ref } from "react";
6
+
7
+ export type InputSlotProps = {
8
+ /**
9
+ * Extra classes for the input rendered in this slot
10
+ */
11
+ className?: string;
12
+ };
13
+
14
+ /**
15
+ * Marks where the input should be rendered among the children of
16
+ * `InputAdornments`. Renders nothing on its own; `InputAdornments` swaps it
17
+ * for the actual input. When it is left out, the input is rendered before the
18
+ * adornments.
19
+ *
20
+ * @example
21
+ * ```jsx
22
+ * <InputAdornments value={value} onChange={handleChange}>
23
+ * <SearchIcon aria-hidden />
24
+ * <InputSlot />
25
+ * <Button icon variant="tertiary"><XMarkIcon aria-hidden /></Button>
26
+ * </InputAdornments>
27
+ * ```
28
+ */
29
+ export function InputSlot(_props: InputSlotProps) {
30
+ return null;
31
+ }
32
+
33
+ export type InputAdornmentsProps = Omit<InputProps, "children"> & {
34
+ /**
35
+ * Adornments rendered next to the input, e.g. icons or buttons. Include an
36
+ * `InputSlot` to control where the input itself ends up.
37
+ */
38
+ children?: ReactNode;
39
+ /**
40
+ * Classes for the wrapper that carries the input styling
41
+ */
42
+ className?: string;
43
+ /**
44
+ * Classes for the bare input element itself
45
+ */
46
+ inputClassName?: string;
47
+ /**
48
+ * Ref to the input element. The wrapper is an implementation detail, so the
49
+ * ref points at the input rather than at the element carrying the styling.
50
+ */
51
+ ref?: Ref<HTMLInputElement>;
52
+ };
53
+
54
+ /**
55
+ * Input with adornments, equivalent to a MUI `TextField` with
56
+ * `InputProps.startAdornment` / `endAdornment`. Designsystemet has no
57
+ * equivalent, so the border, background, hover and focus styling is moved from
58
+ * the input onto a wrapper element, making the input and its adornments read
59
+ * as a single control.
60
+ *
61
+ * All other props are forwarded to the Designsystemet `Input`, so `ref`,
62
+ * `value`, `onChange`, `data-size` and `data-color` work as usual.
63
+ *
64
+ * @example
65
+ * ```jsx
66
+ * // Input first (default), adornments after
67
+ * <InputAdornments aria-label="Search" value={value} onChange={handleChange}>
68
+ * <Button icon variant="tertiary"><MagnifyingGlassIcon aria-hidden /></Button>
69
+ * </InputAdornments>
70
+ *
71
+ * // Explicit placement with InputSlot
72
+ * <InputAdornments aria-label="Amount" data-size="lg">
73
+ * <span aria-hidden>kr</span>
74
+ * <InputSlot />
75
+ * <span aria-hidden>,-</span>
76
+ * </InputAdornments>
77
+ * ```
78
+ */
79
+ export function InputAdornments({ children, className, inputClassName, ...props }: InputAdornmentsProps) {
80
+ let size = props["data-size"];
81
+ let color = props["data-color"];
82
+ let childArray = Children.toArray(children);
83
+ let slot = childArray.find((child) => isValidElement(child) && child.type === InputSlot) as
84
+ | ReactElement<InputSlotProps>
85
+ | undefined;
86
+
87
+ let input = (
88
+ <Input
89
+ key="ObInputAdornments-input"
90
+ {...props}
91
+ className={clsx("ObInputAdornments-field", inputClassName, slot?.props.className)}
92
+ />
93
+ );
94
+
95
+ let content = slot ? childArray.map((child) => (child === slot ? input : child)) : [input, ...childArray];
96
+
97
+ return (
98
+ <div className={clsx("ObInputAdornments-root", className)} data-size={size} data-color={color}>
99
+ {content}
100
+ </div>
101
+ );
102
+ }
@@ -0,0 +1,9 @@
1
+ .ObNumberInput-root .ObInputAdornments-field {
2
+ /* Numbers read best right aligned, and the steppers sit at the end anyway */
3
+ text-align: end;
4
+ font-variant-numeric: tabular-nums;
5
+ }
6
+
7
+ .ObNumberInput-root .ObNumberInput-stepper {
8
+ flex-shrink: 0;
9
+ }
@@ -0,0 +1,212 @@
1
+ import { fireEvent, render, screen } from "@testing-library/react";
2
+ import userEvent from "@testing-library/user-event";
3
+ import { useState } from "react";
4
+ import { describe, expect, it, vi } from "vitest";
5
+
6
+ import { NumberInput, type NumberInputProps } from "./NumberInput.js";
7
+
8
+ function Controlled({
9
+ initial = null,
10
+ onChange,
11
+ ...props
12
+ }: Partial<Omit<NumberInputProps, "value" | "onChange">> & {
13
+ initial?: number | null;
14
+ onChange?: (value: number | null) => void;
15
+ }) {
16
+ let [value, setValue] = useState<number | null>(initial);
17
+
18
+ return (
19
+ <NumberInput
20
+ label="Hours"
21
+ {...props}
22
+ value={value}
23
+ onChange={(next) => {
24
+ setValue(next);
25
+ onChange?.(next);
26
+ }}
27
+ />
28
+ );
29
+ }
30
+
31
+ describe("NumberInput", () => {
32
+ it("shows the value formatted with the fraction digits, in nb-NO without a session", () => {
33
+ render(<Controlled initial={8} />);
34
+
35
+ expect(screen.getByLabelText("Hours")).toHaveValue("8,00");
36
+ });
37
+
38
+ it("formats in the culture given", () => {
39
+ render(<Controlled initial={1234.5} culture="en-GB" />);
40
+
41
+ expect(screen.getByLabelText("Hours")).toHaveValue("1,234.50");
42
+ });
43
+
44
+ it("reads a period as the decimal separator for a Norwegian user too", async () => {
45
+ let onChange = vi.fn();
46
+ let user = userEvent.setup();
47
+ render(<Controlled initial={null} culture="nb-NO" onChange={onChange} />);
48
+
49
+ let input = screen.getByLabelText("Hours");
50
+ await user.type(input, "1.5");
51
+ expect(onChange).toHaveBeenLastCalledWith(1.5);
52
+
53
+ await user.tab();
54
+ expect(input).toHaveValue("1,50");
55
+ });
56
+
57
+ it("shows an empty input for null", () => {
58
+ render(<Controlled initial={null} />);
59
+
60
+ expect(screen.getByLabelText("Hours")).toHaveValue("");
61
+ });
62
+
63
+ it("parses typed text with either decimal separator", async () => {
64
+ let onChange = vi.fn();
65
+ let user = userEvent.setup();
66
+ render(<Controlled initial={null} onChange={onChange} />);
67
+
68
+ let input = screen.getByLabelText("Hours");
69
+ await user.type(input, "1,5");
70
+ expect(onChange).toHaveBeenLastCalledWith(1.5);
71
+ expect(input).toHaveValue("1,5");
72
+
73
+ await user.clear(input);
74
+ await user.type(input, "2.25");
75
+ expect(onChange).toHaveBeenLastCalledWith(2.25);
76
+ });
77
+
78
+ it("reports null when cleared and nothing while the text is incomplete", async () => {
79
+ let onChange = vi.fn();
80
+ let user = userEvent.setup();
81
+ render(<Controlled initial={3} onChange={onChange} />);
82
+
83
+ let input = screen.getByLabelText("Hours");
84
+ await user.clear(input);
85
+ expect(onChange).toHaveBeenLastCalledWith(null);
86
+
87
+ onChange.mockClear();
88
+ await user.type(input, "-");
89
+ expect(onChange).not.toHaveBeenCalled();
90
+ });
91
+
92
+ it("formats the value again on blur", async () => {
93
+ let user = userEvent.setup();
94
+ render(<Controlled initial={null} />);
95
+
96
+ let input = screen.getByLabelText("Hours");
97
+ await user.type(input, "1,5");
98
+ await user.tab();
99
+
100
+ expect(input).toHaveValue("1,50");
101
+ });
102
+
103
+ it("steps with the buttons, rounding to the fraction digits", async () => {
104
+ let onChange = vi.fn();
105
+ let user = userEvent.setup();
106
+ render(<Controlled initial={0.1} step={0.2} onChange={onChange} />);
107
+
108
+ await user.click(screen.getByRole("button", { name: "Increase value" }));
109
+ expect(onChange).toHaveBeenLastCalledWith(0.3);
110
+
111
+ await user.click(screen.getByRole("button", { name: "Decrease value" }));
112
+ await user.click(screen.getByRole("button", { name: "Decrease value" }));
113
+ expect(onChange).toHaveBeenLastCalledWith(-0.1);
114
+ });
115
+
116
+ it("steps from zero when empty", async () => {
117
+ let onChange = vi.fn();
118
+ let user = userEvent.setup();
119
+ render(<Controlled initial={null} step={0.25} onChange={onChange} />);
120
+
121
+ await user.click(screen.getByRole("button", { name: "Increase value" }));
122
+ expect(onChange).toHaveBeenLastCalledWith(0.25);
123
+ });
124
+
125
+ it("steps with the arrow keys and keeps the text selected", async () => {
126
+ let onChange = vi.fn();
127
+ let user = userEvent.setup();
128
+ render(<Controlled initial={1} onChange={onChange} />);
129
+
130
+ let input = screen.getByLabelText("Hours") as HTMLInputElement;
131
+ await user.click(input);
132
+ await user.keyboard("{ArrowUp}{ArrowUp}{ArrowDown}");
133
+
134
+ expect(onChange).toHaveBeenLastCalledWith(2);
135
+ expect(input).toHaveValue("2,00");
136
+ });
137
+
138
+ it("clamps typed and stepped values to min and max", async () => {
139
+ let onChange = vi.fn();
140
+ let user = userEvent.setup();
141
+ render(<Controlled initial={23} min={0} max={24} onChange={onChange} />);
142
+
143
+ let input = screen.getByLabelText("Hours");
144
+ await user.click(screen.getByRole("button", { name: "Increase value" }));
145
+ expect(onChange).toHaveBeenLastCalledWith(24);
146
+ expect(screen.getByRole("button", { name: "Increase value" })).toBeDisabled();
147
+
148
+ await user.clear(input);
149
+ await user.type(input, "30");
150
+ expect(onChange).toHaveBeenLastCalledWith(24);
151
+ expect(input).toHaveValue("24,00");
152
+
153
+ await user.clear(input);
154
+ await user.type(input, "-5");
155
+ expect(onChange).toHaveBeenLastCalledWith(0);
156
+ });
157
+
158
+ it("hides the steppers when read only, disabled or asked to", () => {
159
+ let { rerender } = render(<Controlled initial={1} readOnly />);
160
+ expect(screen.queryByRole("button")).toBeNull();
161
+
162
+ rerender(<Controlled initial={1} disabled />);
163
+ expect(screen.queryByRole("button")).toBeNull();
164
+
165
+ rerender(<Controlled initial={1} hideSteppers />);
166
+ expect(screen.queryByRole("button")).toBeNull();
167
+
168
+ rerender(<Controlled initial={1} />);
169
+ expect(screen.getAllByRole("button")).toHaveLength(2);
170
+ });
171
+
172
+ it("does not step a read only input from the keyboard", () => {
173
+ let onChange = vi.fn();
174
+ render(<Controlled initial={1} readOnly onChange={onChange} />);
175
+
176
+ fireEvent.keyDown(screen.getByLabelText("Hours"), { key: "ArrowUp" });
177
+ expect(onChange).not.toHaveBeenCalled();
178
+ });
179
+
180
+ it("shows the description and the error, and marks the input invalid", () => {
181
+ render(<Controlled initial={1} description="Including overtime" error="Too many hours" />);
182
+
183
+ expect(screen.getByText("Including overtime")).toBeInTheDocument();
184
+ expect(screen.getByText("Too many hours")).toBeInTheDocument();
185
+ expect(screen.getByLabelText("Hours")).toHaveAttribute("aria-invalid", "true");
186
+ });
187
+
188
+ it("shows a value changed from the outside while focused", async () => {
189
+ function Resettable() {
190
+ let [value, setValue] = useState<number | null>(5);
191
+ return (
192
+ <>
193
+ <NumberInput label="Hours" value={value} onChange={setValue} />
194
+ <button type="button" onClick={() => setValue(0)}>
195
+ Reset
196
+ </button>
197
+ </>
198
+ );
199
+ }
200
+
201
+ let user = userEvent.setup();
202
+ render(<Resettable />);
203
+
204
+ let input = screen.getByLabelText("Hours");
205
+ await user.click(input);
206
+ await user.keyboard("7");
207
+ expect(input).toHaveValue("7");
208
+
209
+ fireEvent.click(screen.getByRole("button", { name: "Reset" }));
210
+ expect(input).toHaveValue("0,00");
211
+ });
212
+ });