@inseefr/lunatic 3.4.18 → 3.4.20

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 (68) hide show
  1. package/components/Duration/Duration.js +3 -2
  2. package/components/Duration/Duration.js.map +1 -1
  3. package/components/Input/Input.js +6 -3
  4. package/components/Input/Input.js.map +1 -1
  5. package/components/Input/Input.spec.js +13 -0
  6. package/components/Input/Input.spec.js.map +1 -1
  7. package/components/Textarea/Textarea.js +4 -1
  8. package/components/Textarea/Textarea.js.map +1 -1
  9. package/components/Textarea/Textarea.spec.js +13 -0
  10. package/components/Textarea/Textarea.spec.js.map +1 -1
  11. package/components/shared/CharactersCount/CharactersCount.d.ts +7 -0
  12. package/components/shared/CharactersCount/CharactersCount.js +20 -0
  13. package/components/shared/CharactersCount/CharactersCount.js.map +1 -0
  14. package/components/shared/CharactersCount/CharactersCount.spec.d.ts +1 -0
  15. package/components/shared/CharactersCount/CharactersCount.spec.js +28 -0
  16. package/components/shared/CharactersCount/CharactersCount.spec.js.map +1 -0
  17. package/components/shared/utils/getCharactersCountId.d.ts +4 -0
  18. package/components/shared/utils/getCharactersCountId.js +10 -0
  19. package/components/shared/utils/getCharactersCountId.js.map +1 -0
  20. package/esm/components/Duration/Duration.js +3 -2
  21. package/esm/components/Duration/Duration.js.map +1 -1
  22. package/esm/components/Input/Input.js +6 -3
  23. package/esm/components/Input/Input.js.map +1 -1
  24. package/esm/components/Input/Input.spec.js +13 -0
  25. package/esm/components/Input/Input.spec.js.map +1 -1
  26. package/esm/components/Textarea/Textarea.js +4 -1
  27. package/esm/components/Textarea/Textarea.js.map +1 -1
  28. package/esm/components/Textarea/Textarea.spec.js +13 -0
  29. package/esm/components/Textarea/Textarea.spec.js.map +1 -1
  30. package/esm/components/shared/CharactersCount/CharactersCount.d.ts +7 -0
  31. package/esm/components/shared/CharactersCount/CharactersCount.js +15 -0
  32. package/esm/components/shared/CharactersCount/CharactersCount.js.map +1 -0
  33. package/esm/components/shared/CharactersCount/CharactersCount.spec.d.ts +1 -0
  34. package/esm/components/shared/CharactersCount/CharactersCount.spec.js +26 -0
  35. package/esm/components/shared/CharactersCount/CharactersCount.spec.js.map +1 -0
  36. package/esm/components/shared/utils/getCharactersCountId.d.ts +4 -0
  37. package/esm/components/shared/utils/getCharactersCountId.js +7 -0
  38. package/esm/components/shared/utils/getCharactersCountId.js.map +1 -0
  39. package/esm/main.css +10 -0
  40. package/esm/main.css.map +1 -1
  41. package/esm/use-lunatic/hooks/use-page-has-response.js +2 -1
  42. package/esm/use-lunatic/hooks/use-page-has-response.js.map +1 -1
  43. package/esm/use-lunatic/props/propValue.js +1 -1
  44. package/esm/use-lunatic/props/propValue.js.map +1 -1
  45. package/main.css +10 -0
  46. package/main.css.map +1 -1
  47. package/package.json +24 -1
  48. package/src/components/Duration/Duration.test.tsx +52 -0
  49. package/src/components/Duration/Duration.tsx +17 -4
  50. package/src/components/Input/Input.spec.tsx +19 -0
  51. package/src/components/Input/Input.tsx +26 -18
  52. package/src/components/Input/__snapshots__/Input.spec.tsx.snap +27 -19
  53. package/src/components/RosterForLoop/__snapshots__/RosterForLoop.spec.tsx.snap +42 -20
  54. package/src/components/Textarea/Textarea.spec.tsx +18 -0
  55. package/src/components/Textarea/Textarea.tsx +21 -13
  56. package/src/components/Textarea/__snapshots__/Textarea.spec.tsx.snap +21 -11
  57. package/src/components/shared/CharactersCount/CharactersCount.spec.tsx +31 -0
  58. package/src/components/shared/CharactersCount/CharactersCount.tsx +27 -0
  59. package/src/components/shared/utils/getCharactersCountId.test.ts +14 -0
  60. package/src/components/shared/utils/getCharactersCountId.ts +6 -0
  61. package/src/css/components/Input.scss +10 -0
  62. package/src/use-lunatic/hooks/use-page-has-response.ts +1 -0
  63. package/src/use-lunatic/props/propValue.ts +1 -1
  64. package/tsconfig.build.tsbuildinfo +1 -1
  65. package/use-lunatic/hooks/use-page-has-response.js +2 -1
  66. package/use-lunatic/hooks/use-page-has-response.js.map +1 -1
  67. package/use-lunatic/props/propValue.js +1 -1
  68. package/use-lunatic/props/propValue.js.map +1 -1
@@ -5,12 +5,17 @@ exports[`Textarea > renders without crashing 1`] = `
5
5
  <div
6
6
  class="lunatic-textarea"
7
7
  >
8
- <textarea
9
- aria-invalid="false"
10
- id="input"
8
+ <div
9
+ class="field-with-count"
11
10
  >
12
- input
13
- </textarea>
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
- <textarea
24
- aria-invalid="false"
25
- id="textarea"
26
- readonly=""
28
+ <div
29
+ class="field-with-count"
27
30
  >
28
- 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.
29
- </textarea>
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
+ });
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Get the correct id for characters count div, used for both input and textarea.
3
+ */
4
+ export function getCharactersCountId(id: string, maxLength?: number) {
5
+ return maxLength ? `characters-count-${id}` : undefined;
6
+ }
@@ -43,3 +43,13 @@
43
43
  }
44
44
  }
45
45
  }
46
+
47
+ .characters-count {
48
+ display: block;
49
+ font-size: 12px;
50
+ padding-top: 0.3rem;
51
+ }
52
+
53
+ .max-length-reached {
54
+ color: red;
55
+ }
@@ -77,6 +77,7 @@ function hasOneResponse(
77
77
 
78
78
  // For rosterForLoop we need to inspect child components
79
79
  if (
80
+ component.componentType === 'RosterForLoop' &&
80
81
  'components' in component &&
81
82
  Array.isArray(component.components) &&
82
83
  !isSubComponentsEmpty(component.components, executeExpression)
@@ -28,7 +28,7 @@ export function getValueProp(
28
28
  return args.variables.get(component.response.name, iteration);
29
29
  }
30
30
  // For loop, value will be a map of child component values
31
- if ('components' in component) {
31
+ if (component.componentType === 'Loop') {
32
32
  return getChildResponseValues(component.components, args.variables);
33
33
  }
34
34
  return null;