@inseefr/lunatic 3.5.1-rc.1743691949561 → 3.5.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.
- package/components/Datepicker/Datepicker.js +3 -60
- package/components/Datepicker/Datepicker.js.map +1 -1
- package/components/Datepicker/DatepickerFields.d.ts +8 -0
- package/components/Datepicker/DatepickerFields.js +60 -0
- package/components/Datepicker/DatepickerFields.js.map +1 -0
- package/components/shared/HOC/slottableComponent.d.ts +2 -0
- package/components/shared/HOC/slottableComponent.js.map +1 -1
- package/esm/components/Datepicker/Datepicker.js +3 -60
- package/esm/components/Datepicker/Datepicker.js.map +1 -1
- package/esm/components/Datepicker/DatepickerFields.d.ts +8 -0
- package/esm/components/Datepicker/DatepickerFields.js +57 -0
- package/esm/components/Datepicker/DatepickerFields.js.map +1 -0
- package/esm/components/shared/HOC/slottableComponent.d.ts +2 -0
- package/esm/components/shared/HOC/slottableComponent.js.map +1 -1
- package/package.json +8 -1
- package/src/components/Datepicker/Datepicker.tsx +4 -117
- package/src/components/Datepicker/DatepickerFields.tsx +127 -0
- package/src/components/shared/HOC/slottableComponent.tsx +4 -0
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { slottableComponent } from '../shared/HOC/slottableComponent';
|
|
3
|
+
import type { LunaticComponentProps } from '../type';
|
|
4
|
+
import { DatepickerField } from './DatepickerField';
|
|
5
|
+
import { LunaticError } from '../../use-lunatic/type';
|
|
6
|
+
|
|
7
|
+
type CustomProps = Omit<
|
|
8
|
+
LunaticComponentProps<'Datepicker'>,
|
|
9
|
+
'response' | 'handleChanges' | 'errors'
|
|
10
|
+
> & {
|
|
11
|
+
onChange: (s: string | null) => void;
|
|
12
|
+
errors?: LunaticError[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const CustomDatepickerFields = slottableComponent<CustomProps>(
|
|
16
|
+
'DatepickerFields',
|
|
17
|
+
(props) => {
|
|
18
|
+
const {
|
|
19
|
+
disabled,
|
|
20
|
+
readOnly,
|
|
21
|
+
value = '',
|
|
22
|
+
dateFormat = 'YYYY-MM-DD',
|
|
23
|
+
id,
|
|
24
|
+
onChange,
|
|
25
|
+
} = props;
|
|
26
|
+
|
|
27
|
+
const showDay = dateFormat.includes('DD');
|
|
28
|
+
const showMonth = dateFormat.includes('MM');
|
|
29
|
+
|
|
30
|
+
// Raw state, we allow invalid dates to be typed
|
|
31
|
+
const [numbers, setNumbers] = useState(() =>
|
|
32
|
+
numbersFromDateString(value ?? undefined)
|
|
33
|
+
);
|
|
34
|
+
const setNumber = (index: number) => (value: number) => {
|
|
35
|
+
const newNumbers = [...numbers] as typeof numbers;
|
|
36
|
+
newNumbers[index] = value;
|
|
37
|
+
setNumbers(newNumbers);
|
|
38
|
+
onNumbersChange(newNumbers);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const onNumbersChange = (numbers: [number, number, number]) => {
|
|
42
|
+
const formatParts = dateFormat.split('-');
|
|
43
|
+
const hasNaNIndex = numbers.findIndex((v) => Number.isNaN(v));
|
|
44
|
+
|
|
45
|
+
// Date is not valid, or date has a missing part
|
|
46
|
+
if (
|
|
47
|
+
(dateFormat === 'YYYY-MM-DD' && !isDateValid(numbers)) ||
|
|
48
|
+
(hasNaNIndex > -1 && hasNaNIndex <= formatParts.length - 1)
|
|
49
|
+
) {
|
|
50
|
+
onChange(null);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const result = formatParts
|
|
55
|
+
.map((v, k) => numbers[k].toString().padStart(v.length, '0'))
|
|
56
|
+
.join('-');
|
|
57
|
+
onChange(result);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const extraProps = {
|
|
61
|
+
readOnly,
|
|
62
|
+
disabled,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div className="lunaticDatepickerFields">
|
|
67
|
+
{showDay && (
|
|
68
|
+
<DatepickerField
|
|
69
|
+
id={id + 'day'}
|
|
70
|
+
label="Jour"
|
|
71
|
+
description="Exemple: 14"
|
|
72
|
+
max={31}
|
|
73
|
+
value={numbers[2]}
|
|
74
|
+
onChange={setNumber(2)}
|
|
75
|
+
{...extraProps}
|
|
76
|
+
/>
|
|
77
|
+
)}
|
|
78
|
+
{showMonth && (
|
|
79
|
+
<DatepickerField
|
|
80
|
+
id={id + 'month'}
|
|
81
|
+
label="Mois"
|
|
82
|
+
description="Exemple: 7"
|
|
83
|
+
max={12}
|
|
84
|
+
value={numbers[1]}
|
|
85
|
+
onChange={setNumber(1)}
|
|
86
|
+
{...extraProps}
|
|
87
|
+
/>
|
|
88
|
+
)}
|
|
89
|
+
<DatepickerField
|
|
90
|
+
id={id + 'year'}
|
|
91
|
+
label="Année"
|
|
92
|
+
description="Exemple: 2023"
|
|
93
|
+
value={numbers[0]}
|
|
94
|
+
max={9999}
|
|
95
|
+
onChange={setNumber(0)}
|
|
96
|
+
{...extraProps}
|
|
97
|
+
/>
|
|
98
|
+
</div>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
function numbersFromDateString(s?: string): [number, number, number] {
|
|
104
|
+
if (!s) {
|
|
105
|
+
return [NaN, NaN, NaN];
|
|
106
|
+
}
|
|
107
|
+
const [year, month, day] = s.split('-').map((part) => parseInt(part, 10));
|
|
108
|
+
return [year, month, day];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Check if the date provided by the user is valid (e.g. not 2001/02/29)
|
|
113
|
+
*/
|
|
114
|
+
function isDateValid(dateArray: [number, number, number]) {
|
|
115
|
+
const [year, month, day] = dateArray;
|
|
116
|
+
|
|
117
|
+
// do not set the date directly on new Date(), to avoid transformation on year between 0 and 99.
|
|
118
|
+
//See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#year
|
|
119
|
+
const date = new Date();
|
|
120
|
+
date.setFullYear(year, month - 1, day);
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
date.getFullYear() === year &&
|
|
124
|
+
date.getMonth() === month - 1 &&
|
|
125
|
+
date.getDate() === day
|
|
126
|
+
);
|
|
127
|
+
}
|
|
@@ -43,6 +43,7 @@ import type { SummaryResponses, SummaryTitle } from '../../Summary/Summary';
|
|
|
43
43
|
import type { LunaticComponentProps } from '../../type';
|
|
44
44
|
import type { MarkdownLink } from '../MDLabel/MarkdownLink';
|
|
45
45
|
import type { Accordion } from '../../Accordion/Accordion';
|
|
46
|
+
import type { CustomDatepickerFields } from '../../Datepicker/DatepickerFields';
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
49
|
* Contain the type of every customizable components.
|
|
@@ -80,6 +81,9 @@ export type LunaticSlotComponents = {
|
|
|
80
81
|
ComboboxClearButton: typeof ComboboxClearButton;
|
|
81
82
|
ComboboxLabelSelection: typeof ComboboxLabelSelection;
|
|
82
83
|
|
|
84
|
+
// Datepicker
|
|
85
|
+
DatepickerFields: typeof CustomDatepickerFields;
|
|
86
|
+
|
|
83
87
|
// Roundabout
|
|
84
88
|
Roundabout: typeof CustomRoundabout;
|
|
85
89
|
|