@inseefr/lunatic 3.4.18 → 3.4.19
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/components/Input/Input.js +6 -3
- package/components/Input/Input.js.map +1 -1
- package/components/Input/Input.spec.js +13 -0
- package/components/Input/Input.spec.js.map +1 -1
- package/components/Textarea/Textarea.js +4 -1
- package/components/Textarea/Textarea.js.map +1 -1
- package/components/Textarea/Textarea.spec.js +13 -0
- package/components/Textarea/Textarea.spec.js.map +1 -1
- package/components/shared/CharactersCount/CharactersCount.d.ts +7 -0
- package/components/shared/CharactersCount/CharactersCount.js +20 -0
- package/components/shared/CharactersCount/CharactersCount.js.map +1 -0
- package/components/shared/CharactersCount/CharactersCount.spec.d.ts +1 -0
- package/components/shared/CharactersCount/CharactersCount.spec.js +28 -0
- package/components/shared/CharactersCount/CharactersCount.spec.js.map +1 -0
- package/components/shared/utils/getCharactersCountId.d.ts +4 -0
- package/components/shared/utils/getCharactersCountId.js +10 -0
- package/components/shared/utils/getCharactersCountId.js.map +1 -0
- package/esm/components/Input/Input.js +6 -3
- package/esm/components/Input/Input.js.map +1 -1
- package/esm/components/Input/Input.spec.js +13 -0
- package/esm/components/Input/Input.spec.js.map +1 -1
- package/esm/components/Textarea/Textarea.js +4 -1
- package/esm/components/Textarea/Textarea.js.map +1 -1
- package/esm/components/Textarea/Textarea.spec.js +13 -0
- package/esm/components/Textarea/Textarea.spec.js.map +1 -1
- package/esm/components/shared/CharactersCount/CharactersCount.d.ts +7 -0
- package/esm/components/shared/CharactersCount/CharactersCount.js +15 -0
- package/esm/components/shared/CharactersCount/CharactersCount.js.map +1 -0
- package/esm/components/shared/CharactersCount/CharactersCount.spec.d.ts +1 -0
- package/esm/components/shared/CharactersCount/CharactersCount.spec.js +26 -0
- package/esm/components/shared/CharactersCount/CharactersCount.spec.js.map +1 -0
- package/esm/components/shared/utils/getCharactersCountId.d.ts +4 -0
- package/esm/components/shared/utils/getCharactersCountId.js +7 -0
- package/esm/components/shared/utils/getCharactersCountId.js.map +1 -0
- package/esm/main.css +10 -0
- package/esm/main.css.map +1 -1
- package/main.css +10 -0
- package/main.css.map +1 -1
- package/package.json +23 -1
- package/src/components/Input/Input.spec.tsx +19 -0
- package/src/components/Input/Input.tsx +26 -18
- package/src/components/Input/__snapshots__/Input.spec.tsx.snap +27 -19
- package/src/components/RosterForLoop/__snapshots__/RosterForLoop.spec.tsx.snap +42 -20
- package/src/components/Textarea/Textarea.spec.tsx +18 -0
- package/src/components/Textarea/Textarea.tsx +21 -13
- package/src/components/Textarea/__snapshots__/Textarea.spec.tsx.snap +21 -11
- package/src/components/shared/CharactersCount/CharactersCount.spec.tsx +31 -0
- package/src/components/shared/CharactersCount/CharactersCount.tsx +27 -0
- package/src/components/shared/utils/getCharactersCountId.test.ts +14 -0
- package/src/components/shared/utils/getCharactersCountId.ts +6 -0
- package/src/css/components/Input.scss +10 -0
- package/tsconfig.build.tsbuildinfo +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inseefr/lunatic",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.19",
|
|
4
4
|
"description": "Library of questionnaire components",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -98,6 +98,8 @@
|
|
|
98
98
|
"src/components/shared/Button/IconButton.spec.tsx",
|
|
99
99
|
"src/components/shared/Button/IconButton.tsx",
|
|
100
100
|
"src/components/shared/Button/__snapshots__/Button.spec.tsx.snap",
|
|
101
|
+
"src/components/shared/CharactersCount/CharactersCount.spec.tsx",
|
|
102
|
+
"src/components/shared/CharactersCount/CharactersCount.tsx",
|
|
101
103
|
"src/components/shared/Checkbox/CheckboxOption.spec.tsx",
|
|
102
104
|
"src/components/shared/Checkbox/CheckboxOption.tsx",
|
|
103
105
|
"src/components/shared/Checkbox/getShortcutKey.ts",
|
|
@@ -177,6 +179,8 @@
|
|
|
177
179
|
"src/components/shared/Table/Tr.spec.tsx",
|
|
178
180
|
"src/components/shared/Table/Tr.tsx",
|
|
179
181
|
"src/components/shared/Table/index.ts",
|
|
182
|
+
"src/components/shared/utils/getCharactersCountId.test.ts",
|
|
183
|
+
"src/components/shared/utils/getCharactersCountId.ts",
|
|
180
184
|
"src/components/type.ts",
|
|
181
185
|
"src/css/components/Button.scss",
|
|
182
186
|
"src/css/components/CheckboxGroup.scss",
|
|
@@ -640,6 +644,12 @@
|
|
|
640
644
|
"components/shared/Button/IconButton.spec.d.ts",
|
|
641
645
|
"components/shared/Button/IconButton.spec.js",
|
|
642
646
|
"components/shared/Button/IconButton.spec.js.map",
|
|
647
|
+
"components/shared/CharactersCount/CharactersCount.d.ts",
|
|
648
|
+
"components/shared/CharactersCount/CharactersCount.js",
|
|
649
|
+
"components/shared/CharactersCount/CharactersCount.js.map",
|
|
650
|
+
"components/shared/CharactersCount/CharactersCount.spec.d.ts",
|
|
651
|
+
"components/shared/CharactersCount/CharactersCount.spec.js",
|
|
652
|
+
"components/shared/CharactersCount/CharactersCount.spec.js.map",
|
|
643
653
|
"components/shared/Checkbox/CheckboxOption.d.ts",
|
|
644
654
|
"components/shared/Checkbox/CheckboxOption.js",
|
|
645
655
|
"components/shared/Checkbox/CheckboxOption.js.map",
|
|
@@ -874,6 +884,9 @@
|
|
|
874
884
|
"components/shared/Table/index.d.ts",
|
|
875
885
|
"components/shared/Table/index.js",
|
|
876
886
|
"components/shared/Table/index.js.map",
|
|
887
|
+
"components/shared/utils/getCharactersCountId.d.ts",
|
|
888
|
+
"components/shared/utils/getCharactersCountId.js",
|
|
889
|
+
"components/shared/utils/getCharactersCountId.js.map",
|
|
877
890
|
"components/type.d.ts",
|
|
878
891
|
"components/type.js",
|
|
879
892
|
"components/type.js.map",
|
|
@@ -1048,6 +1061,12 @@
|
|
|
1048
1061
|
"esm/components/shared/Button/IconButton.spec.d.ts",
|
|
1049
1062
|
"esm/components/shared/Button/IconButton.spec.js",
|
|
1050
1063
|
"esm/components/shared/Button/IconButton.spec.js.map",
|
|
1064
|
+
"esm/components/shared/CharactersCount/CharactersCount.d.ts",
|
|
1065
|
+
"esm/components/shared/CharactersCount/CharactersCount.js",
|
|
1066
|
+
"esm/components/shared/CharactersCount/CharactersCount.js.map",
|
|
1067
|
+
"esm/components/shared/CharactersCount/CharactersCount.spec.d.ts",
|
|
1068
|
+
"esm/components/shared/CharactersCount/CharactersCount.spec.js",
|
|
1069
|
+
"esm/components/shared/CharactersCount/CharactersCount.spec.js.map",
|
|
1051
1070
|
"esm/components/shared/Checkbox/CheckboxOption.d.ts",
|
|
1052
1071
|
"esm/components/shared/Checkbox/CheckboxOption.js",
|
|
1053
1072
|
"esm/components/shared/Checkbox/CheckboxOption.js.map",
|
|
@@ -1282,6 +1301,9 @@
|
|
|
1282
1301
|
"esm/components/shared/Table/index.d.ts",
|
|
1283
1302
|
"esm/components/shared/Table/index.js",
|
|
1284
1303
|
"esm/components/shared/Table/index.js.map",
|
|
1304
|
+
"esm/components/shared/utils/getCharactersCountId.d.ts",
|
|
1305
|
+
"esm/components/shared/utils/getCharactersCountId.js",
|
|
1306
|
+
"esm/components/shared/utils/getCharactersCountId.js.map",
|
|
1285
1307
|
"esm/components/type.d.ts",
|
|
1286
1308
|
"esm/components/type.js",
|
|
1287
1309
|
"esm/components/type.js.map",
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
2
2
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
3
|
import { Input } from './Input';
|
|
4
|
+
import { CharactersCount } from '../shared/CharactersCount/CharactersCount';
|
|
5
|
+
|
|
6
|
+
vi.mock('../shared/CharactersCount/CharactersCount', () => ({
|
|
7
|
+
CharactersCount: vi.fn(),
|
|
8
|
+
}));
|
|
9
|
+
|
|
4
10
|
describe('Input', () => {
|
|
5
11
|
const mockOnChange = vi.fn();
|
|
6
12
|
const baseProps = {
|
|
@@ -34,6 +40,19 @@ describe('Input', () => {
|
|
|
34
40
|
expect(input).toBeInTheDocument();
|
|
35
41
|
});
|
|
36
42
|
|
|
43
|
+
it('calls CharactersCount component with correct props', () => {
|
|
44
|
+
const props = { ...baseProps, maxLength: 30 };
|
|
45
|
+
render(<Input {...props} />);
|
|
46
|
+
expect(CharactersCount).toHaveBeenCalledWith(
|
|
47
|
+
{
|
|
48
|
+
id: props.id,
|
|
49
|
+
maxLength: props.maxLength,
|
|
50
|
+
value: props.value,
|
|
51
|
+
},
|
|
52
|
+
{}
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
37
56
|
it('calls onChange with parsed value', () => {
|
|
38
57
|
const { container } = render(<Input {...baseProps} errors={undefined} />);
|
|
39
58
|
|
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
} from '../shared/ComponentErrors/ComponentErrors';
|
|
9
9
|
import { Declarations } from '../shared/Declarations/Declarations';
|
|
10
10
|
import type { LunaticError } from '../../use-lunatic/type';
|
|
11
|
+
import { CharactersCount } from '../shared/CharactersCount/CharactersCount';
|
|
12
|
+
import { getCharactersCountId } from '../shared/utils/getCharactersCountId';
|
|
11
13
|
|
|
12
14
|
export function Input({
|
|
13
15
|
handleChanges,
|
|
@@ -47,6 +49,8 @@ export const CustomInput = slottableComponent<CustomProps>('Input', (props) => {
|
|
|
47
49
|
declarations,
|
|
48
50
|
} = props;
|
|
49
51
|
const labelId = `label-${id}`;
|
|
52
|
+
const charactersCountId = getCharactersCountId(id, maxLength);
|
|
53
|
+
|
|
50
54
|
return (
|
|
51
55
|
<div className={classnames('lunatic-input')}>
|
|
52
56
|
<Label htmlFor={id} id={labelId} description={description}>
|
|
@@ -57,24 +61,28 @@ export const CustomInput = slottableComponent<CustomProps>('Input', (props) => {
|
|
|
57
61
|
declarations={declarations}
|
|
58
62
|
id={id}
|
|
59
63
|
/>
|
|
60
|
-
<
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
64
|
+
<div className="field-with-count">
|
|
65
|
+
<input
|
|
66
|
+
id={id}
|
|
67
|
+
aria-labelledby={labelId}
|
|
68
|
+
aria-describedby={maxLength ? charactersCountId : undefined}
|
|
69
|
+
autoComplete="off"
|
|
70
|
+
type="text"
|
|
71
|
+
disabled={disabled}
|
|
72
|
+
readOnly={readOnly}
|
|
73
|
+
value={(value ?? '').toString()}
|
|
74
|
+
title={value ?? ''}
|
|
75
|
+
onChange={(e) => onChange(e.target.value)}
|
|
76
|
+
aria-required={required}
|
|
77
|
+
required={required}
|
|
78
|
+
maxLength={maxLength}
|
|
79
|
+
aria-invalid={!!errors}
|
|
80
|
+
onBlur={(e) => {
|
|
81
|
+
e.target.setSelectionRange(0, 0);
|
|
82
|
+
}}
|
|
83
|
+
/>
|
|
84
|
+
<CharactersCount id={id} maxLength={maxLength} value={value} />
|
|
85
|
+
</div>
|
|
78
86
|
<ComponentErrors errors={errors} />
|
|
79
87
|
</div>
|
|
80
88
|
);
|
|
@@ -16,15 +16,19 @@ exports[`Input > renders without crashing 1`] = `
|
|
|
16
16
|
description
|
|
17
17
|
</span>
|
|
18
18
|
</label>
|
|
19
|
-
<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
<div
|
|
20
|
+
class="field-with-count"
|
|
21
|
+
>
|
|
22
|
+
<input
|
|
23
|
+
aria-invalid="false"
|
|
24
|
+
aria-labelledby="label-input"
|
|
25
|
+
autocomplete="off"
|
|
26
|
+
id="input"
|
|
27
|
+
title="input"
|
|
28
|
+
type="text"
|
|
29
|
+
value="input"
|
|
30
|
+
/>
|
|
31
|
+
</div>
|
|
28
32
|
</div>
|
|
29
33
|
</div>
|
|
30
34
|
`;
|
|
@@ -34,16 +38,20 @@ exports[`Input > should handle readOnly 1`] = `
|
|
|
34
38
|
<div
|
|
35
39
|
class="lunatic-input"
|
|
36
40
|
>
|
|
37
|
-
<
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
<div
|
|
42
|
+
class="field-with-count"
|
|
43
|
+
>
|
|
44
|
+
<input
|
|
45
|
+
aria-invalid="false"
|
|
46
|
+
aria-labelledby="label-number"
|
|
47
|
+
autocomplete="off"
|
|
48
|
+
id="number"
|
|
49
|
+
readonly=""
|
|
50
|
+
title="toto"
|
|
51
|
+
type="text"
|
|
52
|
+
value="toto"
|
|
53
|
+
/>
|
|
54
|
+
</div>
|
|
47
55
|
</div>
|
|
48
56
|
</div>
|
|
49
57
|
`;
|
|
@@ -31,16 +31,27 @@ exports[`RosterForLoop > renders the right number of columns 1`] = `
|
|
|
31
31
|
<div
|
|
32
32
|
class="lunatic-input"
|
|
33
33
|
>
|
|
34
|
-
<
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
<div
|
|
35
|
+
class="field-with-count"
|
|
36
|
+
>
|
|
37
|
+
<input
|
|
38
|
+
aria-describedby="characters-count-jrc3ye5q-QOP-lo6tcvvx-0"
|
|
39
|
+
aria-invalid="false"
|
|
40
|
+
aria-labelledby="label-jrc3ye5q-QOP-lo6tcvvx-0"
|
|
41
|
+
autocomplete="off"
|
|
42
|
+
id="jrc3ye5q-QOP-lo6tcvvx-0"
|
|
43
|
+
maxlength="249"
|
|
44
|
+
title="azeaze"
|
|
45
|
+
type="text"
|
|
46
|
+
value="azeaze"
|
|
47
|
+
/>
|
|
48
|
+
<span
|
|
49
|
+
class="characters-count"
|
|
50
|
+
id="characters-count-jrc3ye5q-QOP-lo6tcvvx-0"
|
|
51
|
+
>
|
|
52
|
+
6/249
|
|
53
|
+
</span>
|
|
54
|
+
</div>
|
|
44
55
|
</div>
|
|
45
56
|
</div>
|
|
46
57
|
</div>
|
|
@@ -61,16 +72,27 @@ exports[`RosterForLoop > renders the right number of columns 1`] = `
|
|
|
61
72
|
<div
|
|
62
73
|
class="lunatic-input"
|
|
63
74
|
>
|
|
64
|
-
<
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
75
|
+
<div
|
|
76
|
+
class="field-with-count"
|
|
77
|
+
>
|
|
78
|
+
<input
|
|
79
|
+
aria-describedby="characters-count-jrc3ye5q-QOP-lo6tcvvx-1"
|
|
80
|
+
aria-invalid="false"
|
|
81
|
+
aria-labelledby="label-jrc3ye5q-QOP-lo6tcvvx-1"
|
|
82
|
+
autocomplete="off"
|
|
83
|
+
id="jrc3ye5q-QOP-lo6tcvvx-1"
|
|
84
|
+
maxlength="249"
|
|
85
|
+
title="azeaze"
|
|
86
|
+
type="text"
|
|
87
|
+
value="azeaze"
|
|
88
|
+
/>
|
|
89
|
+
<span
|
|
90
|
+
class="characters-count"
|
|
91
|
+
id="characters-count-jrc3ye5q-QOP-lo6tcvvx-1"
|
|
92
|
+
>
|
|
93
|
+
6/249
|
|
94
|
+
</span>
|
|
95
|
+
</div>
|
|
74
96
|
</div>
|
|
75
97
|
</div>
|
|
76
98
|
</div>
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { render } from '@testing-library/react';
|
|
2
2
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
3
|
import { Textarea } from './Textarea';
|
|
4
|
+
import { CharactersCount } from '../shared/CharactersCount/CharactersCount';
|
|
5
|
+
|
|
6
|
+
vi.mock('../shared/CharactersCount/CharactersCount', () => ({
|
|
7
|
+
CharactersCount: vi.fn(),
|
|
8
|
+
}));
|
|
4
9
|
|
|
5
10
|
describe('Textarea', () => {
|
|
6
11
|
const mockOnChange = vi.fn();
|
|
@@ -22,6 +27,19 @@ describe('Textarea', () => {
|
|
|
22
27
|
expect(container).toMatchSnapshot();
|
|
23
28
|
});
|
|
24
29
|
|
|
30
|
+
it('calls CharactersCount component with correct props', () => {
|
|
31
|
+
const props = { ...baseProps, maxLength: 30 };
|
|
32
|
+
render(<Textarea {...props} />);
|
|
33
|
+
expect(CharactersCount).toHaveBeenCalledWith(
|
|
34
|
+
{
|
|
35
|
+
id: props.id,
|
|
36
|
+
maxLength: props.maxLength,
|
|
37
|
+
value: props.value,
|
|
38
|
+
},
|
|
39
|
+
{}
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
25
43
|
it('should handle readOnly', () => {
|
|
26
44
|
const { container } = render(
|
|
27
45
|
<Textarea
|
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
import { slottableComponent } from '../shared/HOC/slottableComponent';
|
|
8
8
|
import { Declarations } from '../shared/Declarations/Declarations';
|
|
9
9
|
import type { LunaticError } from '../../use-lunatic/type';
|
|
10
|
+
import { CharactersCount } from '../shared/CharactersCount/CharactersCount';
|
|
11
|
+
import { getCharactersCountId } from '../shared/utils/getCharactersCountId';
|
|
10
12
|
|
|
11
13
|
export function Textarea({
|
|
12
14
|
handleChanges,
|
|
@@ -51,6 +53,7 @@ export const CustomTextarea = slottableComponent<CustomProps>(
|
|
|
51
53
|
placeHolder,
|
|
52
54
|
} = props;
|
|
53
55
|
const labelId = `label-${id}`;
|
|
56
|
+
const charactersCountId = getCharactersCountId(id, maxLength);
|
|
54
57
|
|
|
55
58
|
return (
|
|
56
59
|
<div className="lunatic-textarea">
|
|
@@ -62,19 +65,24 @@ export const CustomTextarea = slottableComponent<CustomProps>(
|
|
|
62
65
|
declarations={declarations}
|
|
63
66
|
id={id}
|
|
64
67
|
/>
|
|
65
|
-
<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
68
|
+
<div className="field-with-count">
|
|
69
|
+
<textarea
|
|
70
|
+
required={required}
|
|
71
|
+
disabled={disabled}
|
|
72
|
+
id={id}
|
|
73
|
+
aria-labelledby={labelId}
|
|
74
|
+
aria-describedby={maxLength ? charactersCountId : undefined}
|
|
75
|
+
rows={rows}
|
|
76
|
+
maxLength={maxLength}
|
|
77
|
+
cols={cols}
|
|
78
|
+
onChange={(e) => onChange(e.target.value)}
|
|
79
|
+
value={value ?? ''}
|
|
80
|
+
placeholder={placeHolder}
|
|
81
|
+
readOnly={readOnly}
|
|
82
|
+
aria-invalid={!!errors}
|
|
83
|
+
/>
|
|
84
|
+
<CharactersCount id={id} maxLength={maxLength} value={value} />
|
|
85
|
+
</div>
|
|
78
86
|
<ComponentErrors errors={errors} />
|
|
79
87
|
</div>
|
|
80
88
|
);
|
|
@@ -5,12 +5,17 @@ exports[`Textarea > renders without crashing 1`] = `
|
|
|
5
5
|
<div
|
|
6
6
|
class="lunatic-textarea"
|
|
7
7
|
>
|
|
8
|
-
<
|
|
9
|
-
|
|
10
|
-
id="input"
|
|
8
|
+
<div
|
|
9
|
+
class="field-with-count"
|
|
11
10
|
>
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
<textarea
|
|
12
|
+
aria-invalid="false"
|
|
13
|
+
aria-labelledby="label-input"
|
|
14
|
+
id="input"
|
|
15
|
+
>
|
|
16
|
+
input
|
|
17
|
+
</textarea>
|
|
18
|
+
</div>
|
|
14
19
|
</div>
|
|
15
20
|
</div>
|
|
16
21
|
`;
|
|
@@ -20,13 +25,18 @@ exports[`Textarea > should handle readOnly 1`] = `
|
|
|
20
25
|
<div
|
|
21
26
|
class="lunatic-textarea"
|
|
22
27
|
>
|
|
23
|
-
<
|
|
24
|
-
|
|
25
|
-
id="textarea"
|
|
26
|
-
readonly=""
|
|
28
|
+
<div
|
|
29
|
+
class="field-with-count"
|
|
27
30
|
>
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
<textarea
|
|
32
|
+
aria-invalid="false"
|
|
33
|
+
aria-labelledby="label-textarea"
|
|
34
|
+
id="textarea"
|
|
35
|
+
readonly=""
|
|
36
|
+
>
|
|
37
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
|
38
|
+
</textarea>
|
|
39
|
+
</div>
|
|
30
40
|
</div>
|
|
31
41
|
</div>
|
|
32
42
|
`;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { CharactersCount } from './CharactersCount';
|
|
3
|
+
import { render } from '@testing-library/react';
|
|
4
|
+
|
|
5
|
+
describe('CharactersCount', () => {
|
|
6
|
+
const defaultProps = {
|
|
7
|
+
id: 'test-id',
|
|
8
|
+
maxLength: 20,
|
|
9
|
+
value: 'Hello',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
it('renders correctly characters count if there is a maximum length', () => {
|
|
13
|
+
const { getByText } = render(<CharactersCount {...defaultProps} />);
|
|
14
|
+
|
|
15
|
+
expect(getByText('5/20')).toBeInTheDocument();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should apply the max-length-reached class when limit is reached', () => {
|
|
19
|
+
const props = { ...defaultProps, maxLength: 5 };
|
|
20
|
+
const { getByText } = render(<CharactersCount {...props} />);
|
|
21
|
+
|
|
22
|
+
expect(getByText('5/5')).toHaveClass('max-length-reached');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should not render anything when maxLength is not provided', () => {
|
|
26
|
+
const props = { ...defaultProps, maxLength: undefined };
|
|
27
|
+
const { container } = render(<CharactersCount {...props} />);
|
|
28
|
+
|
|
29
|
+
expect(container.firstChild).toBeNull();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { getCharactersCountId } from '../utils/getCharactersCountId';
|
|
3
|
+
|
|
4
|
+
type Props = {
|
|
5
|
+
id: string;
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
value?: string | null;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function CharactersCount({ id, maxLength, value }: Readonly<Props>) {
|
|
11
|
+
if (!maxLength) return null;
|
|
12
|
+
|
|
13
|
+
const currentLength = value?.length ?? 0;
|
|
14
|
+
const charactersCountId = getCharactersCountId(id, maxLength);
|
|
15
|
+
const hasReachedMaxLength = currentLength === maxLength;
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
<span
|
|
19
|
+
id={charactersCountId}
|
|
20
|
+
className={classNames('characters-count', {
|
|
21
|
+
'max-length-reached': hasReachedMaxLength,
|
|
22
|
+
})}
|
|
23
|
+
>
|
|
24
|
+
{`${currentLength}/${maxLength}`}
|
|
25
|
+
</span>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { getCharactersCountId } from './getCharactersCountId';
|
|
3
|
+
|
|
4
|
+
describe('getCharacterCountId', () => {
|
|
5
|
+
it('should return the correct ID when maxLength is provided', () => {
|
|
6
|
+
expect(getCharactersCountId('test-id', 100)).toBe(
|
|
7
|
+
'characters-count-test-id'
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should return undefined when maxLength is not provided', () => {
|
|
12
|
+
expect(getCharactersCountId('test-id')).toBeUndefined();
|
|
13
|
+
});
|
|
14
|
+
});
|