ublo-lib 1.0.32 → 1.0.33
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.
|
@@ -5,6 +5,7 @@ import Attachment from "./attachment";
|
|
|
5
5
|
import Input from "dt-design-system/es/input";
|
|
6
6
|
import Textarea from "dt-design-system/es/textarea";
|
|
7
7
|
import Select from "dt-design-system/es/select";
|
|
8
|
+
import MultipleSelect from "dt-design-system/es/multiple-select";
|
|
8
9
|
import Checkbox from "dt-design-system/es/checkbox";
|
|
9
10
|
import NumberPicker from "dt-design-system/es/number-picker";
|
|
10
11
|
import * as Fetcher from "../../utils/fetcher";
|
|
@@ -12,20 +13,22 @@ import { FIELD_TESTS_ERROR_CODES, validate } from "./utils";
|
|
|
12
13
|
import * as Messages from "./messages";
|
|
13
14
|
import styles from "./field.module.css";
|
|
14
15
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
15
|
-
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
16
16
|
|
|
17
17
|
const getTag = type => {
|
|
18
|
-
switch (
|
|
19
|
-
case
|
|
18
|
+
switch (type) {
|
|
19
|
+
case "textarea":
|
|
20
20
|
return Textarea;
|
|
21
21
|
|
|
22
|
-
case
|
|
22
|
+
case "multiple-select":
|
|
23
|
+
return MultipleSelect;
|
|
24
|
+
|
|
25
|
+
case "select":
|
|
23
26
|
return Select;
|
|
24
27
|
|
|
25
|
-
case
|
|
28
|
+
case "checkbox":
|
|
26
29
|
return Checkbox;
|
|
27
30
|
|
|
28
|
-
case
|
|
31
|
+
case "number":
|
|
29
32
|
return NumberPicker;
|
|
30
33
|
|
|
31
34
|
default:
|
|
@@ -49,6 +52,15 @@ const getProps = (type, name, className, data, required, placeholder, autoSizing
|
|
|
49
52
|
};
|
|
50
53
|
}
|
|
51
54
|
|
|
55
|
+
if (type === "multiple-select") {
|
|
56
|
+
return {
|
|
57
|
+
onChange,
|
|
58
|
+
...commonProps,
|
|
59
|
+
values: commonProps.value,
|
|
60
|
+
onBlur: undefined
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
52
64
|
if (type === "select") {
|
|
53
65
|
return {
|
|
54
66
|
onValueChange: onChange,
|
|
@@ -71,29 +83,6 @@ const getProps = (type, name, className, data, required, placeholder, autoSizing
|
|
|
71
83
|
};
|
|
72
84
|
};
|
|
73
85
|
|
|
74
|
-
const Checkboxes = ({
|
|
75
|
-
checkboxes,
|
|
76
|
-
data,
|
|
77
|
-
name,
|
|
78
|
-
onChange,
|
|
79
|
-
disabled
|
|
80
|
-
}) => {
|
|
81
|
-
if (!checkboxes.length) return null;
|
|
82
|
-
return checkboxes.map((checkbox, i) => {
|
|
83
|
-
const {
|
|
84
|
-
value = checkbox,
|
|
85
|
-
label = checkbox
|
|
86
|
-
} = checkbox;
|
|
87
|
-
const checked = data[name].value.includes(value);
|
|
88
|
-
return _jsx(Checkbox, {
|
|
89
|
-
label: label,
|
|
90
|
-
onValueChange: onChange,
|
|
91
|
-
value: checked,
|
|
92
|
-
disabled: disabled
|
|
93
|
-
}, i);
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
|
|
97
86
|
const getLabel = (lang, label, required) => {
|
|
98
87
|
if (!required) return `${label} (${Messages.get(lang, "optionnal")})`;
|
|
99
88
|
return label;
|
|
@@ -128,29 +117,10 @@ const Inner = ({
|
|
|
128
117
|
[className]: className,
|
|
129
118
|
[classes]: classes
|
|
130
119
|
});
|
|
131
|
-
const fieldsetRef = React.useRef();
|
|
132
120
|
const Tag = getTag(type);
|
|
133
|
-
const onChange = React.useCallback(
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
if (fieldsetRef.current) {
|
|
137
|
-
const values = Array.from(fieldsetRef.current.querySelectorAll("input")).reduce((acc, checkbox) => {
|
|
138
|
-
const parent = checkbox.closest("label");
|
|
139
|
-
const value = parent.textContent.trim();
|
|
140
|
-
const {
|
|
141
|
-
checked
|
|
142
|
-
} = checkbox;
|
|
143
|
-
return checked ? [...acc, value] : acc;
|
|
144
|
-
}, []);
|
|
145
|
-
setData(data => ({ ...data,
|
|
146
|
-
[name]: {
|
|
147
|
-
value: values,
|
|
148
|
-
valid: true
|
|
149
|
-
}
|
|
150
|
-
}));
|
|
151
|
-
setValid(true);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
121
|
+
const onChange = React.useCallback(eventOrValue => {
|
|
122
|
+
const value = eventOrValue?.target?.value || eventOrValue;
|
|
123
|
+
const isAutoFilled = document.activeElement !== eventOrValue?.target;
|
|
154
124
|
|
|
155
125
|
if (type === "number" || isAutoFilled) {
|
|
156
126
|
const isValid = validate(type, value, required);
|
|
@@ -207,22 +177,6 @@ const Inner = ({
|
|
|
207
177
|
getSubjects();
|
|
208
178
|
}
|
|
209
179
|
}, [kind, name, onChange, site]);
|
|
210
|
-
|
|
211
|
-
if (type === "checkbox-group") {
|
|
212
|
-
return _jsxs("fieldset", {
|
|
213
|
-
ref: fieldsetRef,
|
|
214
|
-
children: [label && _jsx("legend", {
|
|
215
|
-
children: label
|
|
216
|
-
}), _jsx(Checkboxes, {
|
|
217
|
-
checkboxes: options,
|
|
218
|
-
data: data,
|
|
219
|
-
name: name,
|
|
220
|
-
onChange: onChange,
|
|
221
|
-
disabled: disabled
|
|
222
|
-
})]
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
|
|
226
180
|
const fieldLabel = getLabel(lang, label, required);
|
|
227
181
|
const isInputValid = required && activated && valid;
|
|
228
182
|
const errorMessage = required && activated && !valid && Messages.get(lang, FIELD_TESTS_ERROR_CODES[type]);
|
|
@@ -33,6 +33,7 @@ const locales = {
|
|
|
33
33
|
TESTS_PHONE_ERROR: 'Le téléphone doit être au format "0612345678" ou "+33612345678"',
|
|
34
34
|
TESTS_TEXTAREA_ERROR: "Ce champ ne doit pas être vide",
|
|
35
35
|
TESTS_SELECT_ERROR: "Ce champ ne doit pas être vide",
|
|
36
|
+
TESTS_MULTIPLE_SELECT_ERROR: "Ce champ ne doit pas être vide",
|
|
36
37
|
optionnal: "optionnel"
|
|
37
38
|
},
|
|
38
39
|
en: {
|
|
@@ -66,8 +67,9 @@ const locales = {
|
|
|
66
67
|
TESTS_EMAIL_ERROR: 'The email must be in the format "john.doe@domain.com"',
|
|
67
68
|
TESTS_DATE_ERROR: 'The date must be in the following format: "YYYY-MM-DD"',
|
|
68
69
|
TESTS_PHONE_ERROR: 'The date must be in the following formats: "0612345678" or "+33612345678"',
|
|
69
|
-
TESTS_TEXTAREA_ERROR: "
|
|
70
|
-
TESTS_SELECT_ERROR: "
|
|
70
|
+
TESTS_TEXTAREA_ERROR: "This field cannot be empty",
|
|
71
|
+
TESTS_SELECT_ERROR: "This field cannot be empty",
|
|
72
|
+
TESTS_MULTIPLE_SELECT_ERROR: "This field cannot be empty",
|
|
71
73
|
optionnal: "optionnal"
|
|
72
74
|
}
|
|
73
75
|
};
|
|
@@ -13,9 +13,10 @@ export const FIELD_TESTS_ERROR_CODES = {
|
|
|
13
13
|
email: "TESTS_EMAIL_ERROR",
|
|
14
14
|
phone: "TESTS_PHONE_ERROR",
|
|
15
15
|
textarea: "TESTS_TEXTAREA_ERROR",
|
|
16
|
-
select: "TESTS_SELECT_ERROR"
|
|
16
|
+
select: "TESTS_SELECT_ERROR",
|
|
17
|
+
"multiple-select": "TESTS_MULTIPLE_SELECT_ERROR"
|
|
17
18
|
};
|
|
18
|
-
const IGNORED_FIELDS = ["title", "subject", "attachment", "checkbox"
|
|
19
|
+
const IGNORED_FIELDS = ["title", "subject", "attachment", "checkbox"];
|
|
19
20
|
export const getInitialFormState = (fields, presets) => Object.keys(fields).reduce((acc, key) => {
|
|
20
21
|
const field = fields[key];
|
|
21
22
|
const {
|
|
@@ -54,7 +55,7 @@ export const getInitialFormState = (fields, presets) => Object.keys(fields).redu
|
|
|
54
55
|
};
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
if (type === "
|
|
58
|
+
if (type === "multiple-select") {
|
|
58
59
|
const value = presets?.[key].length || [];
|
|
59
60
|
return { ...acc,
|
|
60
61
|
[key]: {
|
|
@@ -74,6 +75,7 @@ export const getInitialFormState = (fields, presets) => Object.keys(fields).redu
|
|
|
74
75
|
}, {});
|
|
75
76
|
export const validate = (type, value, required) => {
|
|
76
77
|
if (IGNORED_FIELDS.includes(type) || !required) return true;
|
|
78
|
+
if (type === "multiple-select") return value.length > 0;
|
|
77
79
|
if (type === "number") return value >= 0;
|
|
78
80
|
if (!value) return false;
|
|
79
81
|
const regex = TESTS[type];
|