@olenbetong/appframe-ds 1.7.0 → 1.7.2
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/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/src/autocomplete/Autocomplete.css +260 -253
- package/src/autocomplete/Autocomplete.tsx +320 -320
- package/src/autocomplete/AutocompleteFrame.tsx +168 -168
- package/src/autocomplete/Combobox.tsx +286 -286
- package/src/autocomplete/index.ts +4 -4
- package/src/autocomplete/types.ts +204 -204
- package/src/autocomplete/utils.ts +183 -183
- package/src/input/InputAdornments.css +101 -80
- package/src/input/InputAdornments.tsx +102 -102
- package/src/input/NumberInput.css +9 -9
- package/src/input/NumberInput.test.tsx +212 -212
- package/src/input/NumberInput.tsx +268 -268
- package/src/input/index.ts +3 -3
- package/src/input/numberFormat.test.ts +79 -79
- package/src/input/numberFormat.ts +94 -94
- package/src/layout/AppMenuItem.css +10 -0
|
@@ -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
|
+
}
|
|
@@ -1,9 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,212 +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
|
-
});
|
|
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
|
+
});
|