@webbycrown/advanced-fields 1.0.2 → 1.0.3
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/dist/_chunks/en-BPwepUqK.js +84 -0
- package/dist/_chunks/en-CJmJkBE0.mjs +84 -0
- package/dist/_chunks/index-C-A6rX1K.mjs +268 -0
- package/dist/_chunks/index-CCaZAiQR.js +268 -0
- package/{admin/src/components/AdvancedInput/index.jsx → dist/_chunks/index-DTSlHlvg.mjs} +43 -92
- package/dist/_chunks/index-DXVpJkpj.mjs +282 -0
- package/dist/_chunks/index-TmoGbq0j.js +282 -0
- package/dist/_chunks/index-lJs3vGYF.js +130 -0
- package/dist/admin/index.js +867 -0
- package/dist/admin/index.mjs +868 -0
- package/package.json +5 -3
- package/admin/jsconfig.json +0 -10
- package/admin/src/components/AdvancedCheckbox/index.jsx +0 -377
- package/admin/src/components/AdvancedRadio/index.jsx +0 -344
- package/admin/src/components/Initializer.jsx +0 -18
- package/admin/src/components/PluginIcon.jsx +0 -108
- package/admin/src/index.js +0 -787
- package/admin/src/pages/App.jsx +0 -13
- package/admin/src/pluginId.js +0 -3
- package/admin/src/utils/getTranslation.js +0 -5
- /package/{admin/src → dist/admin}/translations/en.json +0 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useIntl } from "react-intl";
|
|
3
|
+
import { Box, Field, Flex } from "@strapi/design-system";
|
|
4
|
+
import { useState, useEffect } from "react";
|
|
5
|
+
const AdvancedCheckbox = ({
|
|
6
|
+
attribute = {},
|
|
7
|
+
description = { id: "", defaultMessage: "" },
|
|
8
|
+
disabled,
|
|
9
|
+
error,
|
|
10
|
+
intlLabel = { id: "", defaultMessage: "" },
|
|
11
|
+
labelAction,
|
|
12
|
+
name,
|
|
13
|
+
onChange,
|
|
14
|
+
required,
|
|
15
|
+
value
|
|
16
|
+
}) => {
|
|
17
|
+
const { formatMessage } = useIntl();
|
|
18
|
+
const {
|
|
19
|
+
checkboxType = "multiple",
|
|
20
|
+
layout = "vertical",
|
|
21
|
+
minChoices = 0,
|
|
22
|
+
maxChoices = 0,
|
|
23
|
+
defaultSelected = "",
|
|
24
|
+
checkboxOptions = "",
|
|
25
|
+
customErrorMessage = "",
|
|
26
|
+
fieldNote = ""
|
|
27
|
+
} = attribute.options || attribute;
|
|
28
|
+
const fieldNoteFromAttribute = attribute.options?.fieldNote || "";
|
|
29
|
+
const getInitialValues = () => {
|
|
30
|
+
if (checkboxType === "single") {
|
|
31
|
+
if (value && Array.isArray(value) && value.length > 0) {
|
|
32
|
+
return value;
|
|
33
|
+
} else if (value && typeof value === "string" && value.trim()) {
|
|
34
|
+
return [value.trim()];
|
|
35
|
+
} else if (defaultSelected && typeof defaultSelected === "string" && defaultSelected.trim()) {
|
|
36
|
+
return [defaultSelected.trim()];
|
|
37
|
+
} else if (defaultSelected && Array.isArray(defaultSelected) && defaultSelected.length > 0) {
|
|
38
|
+
return defaultSelected;
|
|
39
|
+
}
|
|
40
|
+
return [];
|
|
41
|
+
} else {
|
|
42
|
+
if (value && Array.isArray(value)) {
|
|
43
|
+
return value;
|
|
44
|
+
} else if (value && typeof value === "string") {
|
|
45
|
+
return value.split(",").map((v) => v.trim()).filter((v) => v);
|
|
46
|
+
} else if (defaultSelected) {
|
|
47
|
+
return defaultSelected.split(",").map((v) => v.trim()).filter((v) => v);
|
|
48
|
+
}
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const [fieldValue, setFieldValue] = useState(getInitialValues);
|
|
53
|
+
const [validationError, setValidationError] = useState(null);
|
|
54
|
+
const [hasInteracted, setHasInteracted] = useState(false);
|
|
55
|
+
const [isInitialized, setIsInitialized] = useState(false);
|
|
56
|
+
const options = checkboxOptions.split("\n").filter((opt) => opt.trim()).map((opt) => {
|
|
57
|
+
const [value2, label] = opt.split("|");
|
|
58
|
+
return { value: value2?.trim() || "", label: label?.trim() || value2?.trim() || "" };
|
|
59
|
+
});
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
const initialValues = getInitialValues();
|
|
62
|
+
setFieldValue(initialValues);
|
|
63
|
+
const validationResult = validateSelection(initialValues);
|
|
64
|
+
setValidationError(validationResult);
|
|
65
|
+
if (onChange && initialValues.length > 0 && (!value || Array.isArray(value) && value.length === 0) && !isInitialized) {
|
|
66
|
+
setTimeout(() => {
|
|
67
|
+
onChange({
|
|
68
|
+
target: {
|
|
69
|
+
value: initialValues,
|
|
70
|
+
name,
|
|
71
|
+
id: name
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
setIsInitialized(true);
|
|
75
|
+
}, 0);
|
|
76
|
+
} else if (!isInitialized) {
|
|
77
|
+
setIsInitialized(true);
|
|
78
|
+
}
|
|
79
|
+
}, []);
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
if (value && Array.isArray(value) && value.length > 0) {
|
|
82
|
+
setFieldValue(value);
|
|
83
|
+
const validationResult = validateSelection(value);
|
|
84
|
+
setValidationError(validationResult);
|
|
85
|
+
}
|
|
86
|
+
}, [value]);
|
|
87
|
+
const validateSelection = (val) => {
|
|
88
|
+
const values = Array.isArray(val) ? val : [];
|
|
89
|
+
if (required && values.length === 0) {
|
|
90
|
+
return customErrorMessage || "This field is required";
|
|
91
|
+
}
|
|
92
|
+
if (values.length === 0) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
if (checkboxType === "multiple") {
|
|
96
|
+
if (minChoices > 0 && values.length < minChoices) {
|
|
97
|
+
return customErrorMessage || `Please select at least ${minChoices} option${minChoices > 1 ? "s" : ""}`;
|
|
98
|
+
}
|
|
99
|
+
if (maxChoices > 0 && values.length > maxChoices) {
|
|
100
|
+
return customErrorMessage || `Please select at most ${maxChoices} option${maxChoices > 1 ? "s" : ""}`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
};
|
|
105
|
+
const handleCheckboxChange = (optionValue, isChecked) => {
|
|
106
|
+
let newValue;
|
|
107
|
+
if (checkboxType === "single") {
|
|
108
|
+
if (isChecked) {
|
|
109
|
+
newValue = [optionValue];
|
|
110
|
+
} else {
|
|
111
|
+
newValue = [];
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
if (isChecked) {
|
|
115
|
+
newValue = [...fieldValue, optionValue];
|
|
116
|
+
} else {
|
|
117
|
+
newValue = fieldValue.filter((val) => val !== optionValue);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
setFieldValue(newValue);
|
|
121
|
+
setHasInteracted(true);
|
|
122
|
+
const error2 = validateSelection(newValue);
|
|
123
|
+
setValidationError(error2);
|
|
124
|
+
if (onChange) {
|
|
125
|
+
onChange({
|
|
126
|
+
target: {
|
|
127
|
+
value: newValue,
|
|
128
|
+
name,
|
|
129
|
+
id: name
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const displayError = error || hasInteracted && validationError;
|
|
135
|
+
const renderCheckboxes = () => {
|
|
136
|
+
const checkboxStyle = {
|
|
137
|
+
display: "flex",
|
|
138
|
+
alignItems: "center",
|
|
139
|
+
gap: "8px",
|
|
140
|
+
marginBottom: "8px"
|
|
141
|
+
};
|
|
142
|
+
const checkboxInputStyle = {
|
|
143
|
+
width: "16px",
|
|
144
|
+
height: "16px",
|
|
145
|
+
accentColor: "#4945ff",
|
|
146
|
+
margin: "0",
|
|
147
|
+
padding: "0",
|
|
148
|
+
opacity: "1",
|
|
149
|
+
visibility: "visible",
|
|
150
|
+
display: "block",
|
|
151
|
+
position: "relative",
|
|
152
|
+
zIndex: "1"
|
|
153
|
+
};
|
|
154
|
+
const checkboxLabelStyle = {
|
|
155
|
+
fontSize: "14px",
|
|
156
|
+
fontFamily: "inherit",
|
|
157
|
+
cursor: "pointer",
|
|
158
|
+
userSelect: "none"
|
|
159
|
+
};
|
|
160
|
+
if (checkboxType === "single") {
|
|
161
|
+
if (!options || options.length === 0) {
|
|
162
|
+
return /* @__PURE__ */ jsx("div", { style: { padding: "8px", color: "#666", fontStyle: "italic" }, children: formatMessage({
|
|
163
|
+
id: "advanced-fields.checkbox.options.checkboxOptions.description",
|
|
164
|
+
defaultMessage: "Define available options for multiple checkboxes (one per line: value|label)"
|
|
165
|
+
}) });
|
|
166
|
+
}
|
|
167
|
+
if (layout === "horizontal") {
|
|
168
|
+
return /* @__PURE__ */ jsx(Flex, { wrap: "wrap", gap: 2, children: options.map((option) => /* @__PURE__ */ jsxs("div", { style: checkboxStyle, children: [
|
|
169
|
+
/* @__PURE__ */ jsx(
|
|
170
|
+
"input",
|
|
171
|
+
{
|
|
172
|
+
type: "checkbox",
|
|
173
|
+
id: `${name}-${option.value}`,
|
|
174
|
+
checked: fieldValue.includes(option.value),
|
|
175
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
176
|
+
disabled,
|
|
177
|
+
style: checkboxInputStyle
|
|
178
|
+
}
|
|
179
|
+
),
|
|
180
|
+
/* @__PURE__ */ jsx(
|
|
181
|
+
"label",
|
|
182
|
+
{
|
|
183
|
+
htmlFor: `${name}-${option.value}`,
|
|
184
|
+
style: checkboxLabelStyle,
|
|
185
|
+
children: option.label
|
|
186
|
+
}
|
|
187
|
+
)
|
|
188
|
+
] }, option.value)) });
|
|
189
|
+
}
|
|
190
|
+
return /* @__PURE__ */ jsx("div", { children: options.map((option) => /* @__PURE__ */ jsxs("div", { style: checkboxStyle, children: [
|
|
191
|
+
/* @__PURE__ */ jsx(
|
|
192
|
+
"input",
|
|
193
|
+
{
|
|
194
|
+
type: "checkbox",
|
|
195
|
+
id: `${name}-${option.value}`,
|
|
196
|
+
checked: fieldValue.includes(option.value),
|
|
197
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
198
|
+
disabled,
|
|
199
|
+
style: checkboxInputStyle
|
|
200
|
+
}
|
|
201
|
+
),
|
|
202
|
+
/* @__PURE__ */ jsx(
|
|
203
|
+
"label",
|
|
204
|
+
{
|
|
205
|
+
htmlFor: `${name}-${option.value}`,
|
|
206
|
+
style: checkboxLabelStyle,
|
|
207
|
+
children: option.label
|
|
208
|
+
}
|
|
209
|
+
)
|
|
210
|
+
] }, option.value)) });
|
|
211
|
+
}
|
|
212
|
+
if (!options || options.length === 0) {
|
|
213
|
+
return /* @__PURE__ */ jsx("div", { style: { padding: "8px", color: "#666", fontStyle: "italic" }, children: formatMessage({
|
|
214
|
+
id: "advanced-fields.checkbox.options.checkboxOptions.description",
|
|
215
|
+
defaultMessage: "Define available options for multiple checkboxes (one per line: value|label)"
|
|
216
|
+
}) });
|
|
217
|
+
}
|
|
218
|
+
if (layout === "horizontal") {
|
|
219
|
+
return /* @__PURE__ */ jsx(Flex, { wrap: "wrap", gap: 2, children: options.map((option) => /* @__PURE__ */ jsxs("div", { style: checkboxStyle, children: [
|
|
220
|
+
/* @__PURE__ */ jsx(
|
|
221
|
+
"input",
|
|
222
|
+
{
|
|
223
|
+
type: "checkbox",
|
|
224
|
+
id: `${name}-${option.value}`,
|
|
225
|
+
checked: fieldValue.includes(option.value),
|
|
226
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
227
|
+
disabled,
|
|
228
|
+
style: checkboxInputStyle
|
|
229
|
+
}
|
|
230
|
+
),
|
|
231
|
+
/* @__PURE__ */ jsx(
|
|
232
|
+
"label",
|
|
233
|
+
{
|
|
234
|
+
htmlFor: `${name}-${option.value}`,
|
|
235
|
+
style: checkboxLabelStyle,
|
|
236
|
+
children: option.label
|
|
237
|
+
}
|
|
238
|
+
)
|
|
239
|
+
] }, option.value)) });
|
|
240
|
+
}
|
|
241
|
+
return /* @__PURE__ */ jsx("div", { children: options.map((option) => /* @__PURE__ */ jsxs("div", { style: checkboxStyle, children: [
|
|
242
|
+
/* @__PURE__ */ jsx(
|
|
243
|
+
"input",
|
|
244
|
+
{
|
|
245
|
+
type: "checkbox",
|
|
246
|
+
id: `${name}-${option.value}`,
|
|
247
|
+
checked: fieldValue.includes(option.value),
|
|
248
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
249
|
+
disabled,
|
|
250
|
+
style: checkboxInputStyle
|
|
251
|
+
}
|
|
252
|
+
),
|
|
253
|
+
/* @__PURE__ */ jsx(
|
|
254
|
+
"label",
|
|
255
|
+
{
|
|
256
|
+
htmlFor: `${name}-${option.value}`,
|
|
257
|
+
style: checkboxLabelStyle,
|
|
258
|
+
children: option.label
|
|
259
|
+
}
|
|
260
|
+
)
|
|
261
|
+
] }, option.value)) });
|
|
262
|
+
};
|
|
263
|
+
return /* @__PURE__ */ jsx(Box, { col: 6, children: /* @__PURE__ */ jsxs(Field.Root, { name, error: displayError, children: [
|
|
264
|
+
/* @__PURE__ */ jsxs(Field.Label, { children: [
|
|
265
|
+
intlLabel.id ? formatMessage(intlLabel) : intlLabel.defaultMessage || name,
|
|
266
|
+
required && /* @__PURE__ */ jsx("span", { style: { color: "#d02b20", marginLeft: "4px" }, children: "*" })
|
|
267
|
+
] }),
|
|
268
|
+
renderCheckboxes(),
|
|
269
|
+
displayError && /* @__PURE__ */ jsx(Field.Error, { children: displayError }),
|
|
270
|
+
description && (description.id || description.defaultMessage) && /* @__PURE__ */ jsx(Field.Hint, { children: description.id ? formatMessage(description) : description.defaultMessage }),
|
|
271
|
+
(fieldNote || fieldNoteFromAttribute) && /* @__PURE__ */ jsx("span", { style: {
|
|
272
|
+
fontStyle: "italic",
|
|
273
|
+
color: "#666",
|
|
274
|
+
fontSize: "12px",
|
|
275
|
+
display: "block",
|
|
276
|
+
marginTop: "4px"
|
|
277
|
+
}, children: fieldNote || fieldNoteFromAttribute })
|
|
278
|
+
] }) });
|
|
279
|
+
};
|
|
280
|
+
export {
|
|
281
|
+
AdvancedCheckbox as default
|
|
282
|
+
};
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
+
const reactIntl = require("react-intl");
|
|
5
|
+
const designSystem = require("@strapi/design-system");
|
|
6
|
+
const react = require("react");
|
|
7
|
+
const AdvancedCheckbox = ({
|
|
8
|
+
attribute = {},
|
|
9
|
+
description = { id: "", defaultMessage: "" },
|
|
10
|
+
disabled,
|
|
11
|
+
error,
|
|
12
|
+
intlLabel = { id: "", defaultMessage: "" },
|
|
13
|
+
labelAction,
|
|
14
|
+
name,
|
|
15
|
+
onChange,
|
|
16
|
+
required,
|
|
17
|
+
value
|
|
18
|
+
}) => {
|
|
19
|
+
const { formatMessage } = reactIntl.useIntl();
|
|
20
|
+
const {
|
|
21
|
+
checkboxType = "multiple",
|
|
22
|
+
layout = "vertical",
|
|
23
|
+
minChoices = 0,
|
|
24
|
+
maxChoices = 0,
|
|
25
|
+
defaultSelected = "",
|
|
26
|
+
checkboxOptions = "",
|
|
27
|
+
customErrorMessage = "",
|
|
28
|
+
fieldNote = ""
|
|
29
|
+
} = attribute.options || attribute;
|
|
30
|
+
const fieldNoteFromAttribute = attribute.options?.fieldNote || "";
|
|
31
|
+
const getInitialValues = () => {
|
|
32
|
+
if (checkboxType === "single") {
|
|
33
|
+
if (value && Array.isArray(value) && value.length > 0) {
|
|
34
|
+
return value;
|
|
35
|
+
} else if (value && typeof value === "string" && value.trim()) {
|
|
36
|
+
return [value.trim()];
|
|
37
|
+
} else if (defaultSelected && typeof defaultSelected === "string" && defaultSelected.trim()) {
|
|
38
|
+
return [defaultSelected.trim()];
|
|
39
|
+
} else if (defaultSelected && Array.isArray(defaultSelected) && defaultSelected.length > 0) {
|
|
40
|
+
return defaultSelected;
|
|
41
|
+
}
|
|
42
|
+
return [];
|
|
43
|
+
} else {
|
|
44
|
+
if (value && Array.isArray(value)) {
|
|
45
|
+
return value;
|
|
46
|
+
} else if (value && typeof value === "string") {
|
|
47
|
+
return value.split(",").map((v) => v.trim()).filter((v) => v);
|
|
48
|
+
} else if (defaultSelected) {
|
|
49
|
+
return defaultSelected.split(",").map((v) => v.trim()).filter((v) => v);
|
|
50
|
+
}
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const [fieldValue, setFieldValue] = react.useState(getInitialValues);
|
|
55
|
+
const [validationError, setValidationError] = react.useState(null);
|
|
56
|
+
const [hasInteracted, setHasInteracted] = react.useState(false);
|
|
57
|
+
const [isInitialized, setIsInitialized] = react.useState(false);
|
|
58
|
+
const options = checkboxOptions.split("\n").filter((opt) => opt.trim()).map((opt) => {
|
|
59
|
+
const [value2, label] = opt.split("|");
|
|
60
|
+
return { value: value2?.trim() || "", label: label?.trim() || value2?.trim() || "" };
|
|
61
|
+
});
|
|
62
|
+
react.useEffect(() => {
|
|
63
|
+
const initialValues = getInitialValues();
|
|
64
|
+
setFieldValue(initialValues);
|
|
65
|
+
const validationResult = validateSelection(initialValues);
|
|
66
|
+
setValidationError(validationResult);
|
|
67
|
+
if (onChange && initialValues.length > 0 && (!value || Array.isArray(value) && value.length === 0) && !isInitialized) {
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
onChange({
|
|
70
|
+
target: {
|
|
71
|
+
value: initialValues,
|
|
72
|
+
name,
|
|
73
|
+
id: name
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
setIsInitialized(true);
|
|
77
|
+
}, 0);
|
|
78
|
+
} else if (!isInitialized) {
|
|
79
|
+
setIsInitialized(true);
|
|
80
|
+
}
|
|
81
|
+
}, []);
|
|
82
|
+
react.useEffect(() => {
|
|
83
|
+
if (value && Array.isArray(value) && value.length > 0) {
|
|
84
|
+
setFieldValue(value);
|
|
85
|
+
const validationResult = validateSelection(value);
|
|
86
|
+
setValidationError(validationResult);
|
|
87
|
+
}
|
|
88
|
+
}, [value]);
|
|
89
|
+
const validateSelection = (val) => {
|
|
90
|
+
const values = Array.isArray(val) ? val : [];
|
|
91
|
+
if (required && values.length === 0) {
|
|
92
|
+
return customErrorMessage || "This field is required";
|
|
93
|
+
}
|
|
94
|
+
if (values.length === 0) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
if (checkboxType === "multiple") {
|
|
98
|
+
if (minChoices > 0 && values.length < minChoices) {
|
|
99
|
+
return customErrorMessage || `Please select at least ${minChoices} option${minChoices > 1 ? "s" : ""}`;
|
|
100
|
+
}
|
|
101
|
+
if (maxChoices > 0 && values.length > maxChoices) {
|
|
102
|
+
return customErrorMessage || `Please select at most ${maxChoices} option${maxChoices > 1 ? "s" : ""}`;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
};
|
|
107
|
+
const handleCheckboxChange = (optionValue, isChecked) => {
|
|
108
|
+
let newValue;
|
|
109
|
+
if (checkboxType === "single") {
|
|
110
|
+
if (isChecked) {
|
|
111
|
+
newValue = [optionValue];
|
|
112
|
+
} else {
|
|
113
|
+
newValue = [];
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
if (isChecked) {
|
|
117
|
+
newValue = [...fieldValue, optionValue];
|
|
118
|
+
} else {
|
|
119
|
+
newValue = fieldValue.filter((val) => val !== optionValue);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
setFieldValue(newValue);
|
|
123
|
+
setHasInteracted(true);
|
|
124
|
+
const error2 = validateSelection(newValue);
|
|
125
|
+
setValidationError(error2);
|
|
126
|
+
if (onChange) {
|
|
127
|
+
onChange({
|
|
128
|
+
target: {
|
|
129
|
+
value: newValue,
|
|
130
|
+
name,
|
|
131
|
+
id: name
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const displayError = error || hasInteracted && validationError;
|
|
137
|
+
const renderCheckboxes = () => {
|
|
138
|
+
const checkboxStyle = {
|
|
139
|
+
display: "flex",
|
|
140
|
+
alignItems: "center",
|
|
141
|
+
gap: "8px",
|
|
142
|
+
marginBottom: "8px"
|
|
143
|
+
};
|
|
144
|
+
const checkboxInputStyle = {
|
|
145
|
+
width: "16px",
|
|
146
|
+
height: "16px",
|
|
147
|
+
accentColor: "#4945ff",
|
|
148
|
+
margin: "0",
|
|
149
|
+
padding: "0",
|
|
150
|
+
opacity: "1",
|
|
151
|
+
visibility: "visible",
|
|
152
|
+
display: "block",
|
|
153
|
+
position: "relative",
|
|
154
|
+
zIndex: "1"
|
|
155
|
+
};
|
|
156
|
+
const checkboxLabelStyle = {
|
|
157
|
+
fontSize: "14px",
|
|
158
|
+
fontFamily: "inherit",
|
|
159
|
+
cursor: "pointer",
|
|
160
|
+
userSelect: "none"
|
|
161
|
+
};
|
|
162
|
+
if (checkboxType === "single") {
|
|
163
|
+
if (!options || options.length === 0) {
|
|
164
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { padding: "8px", color: "#666", fontStyle: "italic" }, children: formatMessage({
|
|
165
|
+
id: "advanced-fields.checkbox.options.checkboxOptions.description",
|
|
166
|
+
defaultMessage: "Define available options for multiple checkboxes (one per line: value|label)"
|
|
167
|
+
}) });
|
|
168
|
+
}
|
|
169
|
+
if (layout === "horizontal") {
|
|
170
|
+
return /* @__PURE__ */ jsxRuntime.jsx(designSystem.Flex, { wrap: "wrap", gap: 2, children: options.map((option) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: checkboxStyle, children: [
|
|
171
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
172
|
+
"input",
|
|
173
|
+
{
|
|
174
|
+
type: "checkbox",
|
|
175
|
+
id: `${name}-${option.value}`,
|
|
176
|
+
checked: fieldValue.includes(option.value),
|
|
177
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
178
|
+
disabled,
|
|
179
|
+
style: checkboxInputStyle
|
|
180
|
+
}
|
|
181
|
+
),
|
|
182
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
183
|
+
"label",
|
|
184
|
+
{
|
|
185
|
+
htmlFor: `${name}-${option.value}`,
|
|
186
|
+
style: checkboxLabelStyle,
|
|
187
|
+
children: option.label
|
|
188
|
+
}
|
|
189
|
+
)
|
|
190
|
+
] }, option.value)) });
|
|
191
|
+
}
|
|
192
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { children: options.map((option) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: checkboxStyle, children: [
|
|
193
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
194
|
+
"input",
|
|
195
|
+
{
|
|
196
|
+
type: "checkbox",
|
|
197
|
+
id: `${name}-${option.value}`,
|
|
198
|
+
checked: fieldValue.includes(option.value),
|
|
199
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
200
|
+
disabled,
|
|
201
|
+
style: checkboxInputStyle
|
|
202
|
+
}
|
|
203
|
+
),
|
|
204
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
205
|
+
"label",
|
|
206
|
+
{
|
|
207
|
+
htmlFor: `${name}-${option.value}`,
|
|
208
|
+
style: checkboxLabelStyle,
|
|
209
|
+
children: option.label
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
] }, option.value)) });
|
|
213
|
+
}
|
|
214
|
+
if (!options || options.length === 0) {
|
|
215
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { padding: "8px", color: "#666", fontStyle: "italic" }, children: formatMessage({
|
|
216
|
+
id: "advanced-fields.checkbox.options.checkboxOptions.description",
|
|
217
|
+
defaultMessage: "Define available options for multiple checkboxes (one per line: value|label)"
|
|
218
|
+
}) });
|
|
219
|
+
}
|
|
220
|
+
if (layout === "horizontal") {
|
|
221
|
+
return /* @__PURE__ */ jsxRuntime.jsx(designSystem.Flex, { wrap: "wrap", gap: 2, children: options.map((option) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: checkboxStyle, children: [
|
|
222
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
223
|
+
"input",
|
|
224
|
+
{
|
|
225
|
+
type: "checkbox",
|
|
226
|
+
id: `${name}-${option.value}`,
|
|
227
|
+
checked: fieldValue.includes(option.value),
|
|
228
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
229
|
+
disabled,
|
|
230
|
+
style: checkboxInputStyle
|
|
231
|
+
}
|
|
232
|
+
),
|
|
233
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
234
|
+
"label",
|
|
235
|
+
{
|
|
236
|
+
htmlFor: `${name}-${option.value}`,
|
|
237
|
+
style: checkboxLabelStyle,
|
|
238
|
+
children: option.label
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
] }, option.value)) });
|
|
242
|
+
}
|
|
243
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { children: options.map((option) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: checkboxStyle, children: [
|
|
244
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
245
|
+
"input",
|
|
246
|
+
{
|
|
247
|
+
type: "checkbox",
|
|
248
|
+
id: `${name}-${option.value}`,
|
|
249
|
+
checked: fieldValue.includes(option.value),
|
|
250
|
+
onChange: (e) => handleCheckboxChange(option.value, e.target.checked),
|
|
251
|
+
disabled,
|
|
252
|
+
style: checkboxInputStyle
|
|
253
|
+
}
|
|
254
|
+
),
|
|
255
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
256
|
+
"label",
|
|
257
|
+
{
|
|
258
|
+
htmlFor: `${name}-${option.value}`,
|
|
259
|
+
style: checkboxLabelStyle,
|
|
260
|
+
children: option.label
|
|
261
|
+
}
|
|
262
|
+
)
|
|
263
|
+
] }, option.value)) });
|
|
264
|
+
};
|
|
265
|
+
return /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { col: 6, children: /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Field.Root, { name, error: displayError, children: [
|
|
266
|
+
/* @__PURE__ */ jsxRuntime.jsxs(designSystem.Field.Label, { children: [
|
|
267
|
+
intlLabel.id ? formatMessage(intlLabel) : intlLabel.defaultMessage || name,
|
|
268
|
+
required && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: "#d02b20", marginLeft: "4px" }, children: "*" })
|
|
269
|
+
] }),
|
|
270
|
+
renderCheckboxes(),
|
|
271
|
+
displayError && /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, { children: displayError }),
|
|
272
|
+
description && (description.id || description.defaultMessage) && /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, { children: description.id ? formatMessage(description) : description.defaultMessage }),
|
|
273
|
+
(fieldNote || fieldNoteFromAttribute) && /* @__PURE__ */ jsxRuntime.jsx("span", { style: {
|
|
274
|
+
fontStyle: "italic",
|
|
275
|
+
color: "#666",
|
|
276
|
+
fontSize: "12px",
|
|
277
|
+
display: "block",
|
|
278
|
+
marginTop: "4px"
|
|
279
|
+
}, children: fieldNote || fieldNoteFromAttribute })
|
|
280
|
+
] }) });
|
|
281
|
+
};
|
|
282
|
+
exports.default = AdvancedCheckbox;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
+
const reactIntl = require("react-intl");
|
|
5
|
+
const designSystem = require("@strapi/design-system");
|
|
6
|
+
require("@strapi/icons");
|
|
7
|
+
const react = require("react");
|
|
8
|
+
const AdvancedInput = ({
|
|
9
|
+
attribute = {},
|
|
10
|
+
description = { id: "", defaultMessage: "" },
|
|
11
|
+
disabled,
|
|
12
|
+
error,
|
|
13
|
+
intlLabel = { id: "", defaultMessage: "" },
|
|
14
|
+
labelAction,
|
|
15
|
+
name,
|
|
16
|
+
onChange,
|
|
17
|
+
required,
|
|
18
|
+
value
|
|
19
|
+
}) => {
|
|
20
|
+
const { formatMessage } = reactIntl.useIntl();
|
|
21
|
+
const [inputValue, setInputValue] = react.useState(value || "");
|
|
22
|
+
const [validationError, setValidationError] = react.useState(null);
|
|
23
|
+
const [hasInteracted, setHasInteracted] = react.useState(false);
|
|
24
|
+
const {
|
|
25
|
+
minLength = 0,
|
|
26
|
+
maxLength = 0,
|
|
27
|
+
min = 0,
|
|
28
|
+
max = 0,
|
|
29
|
+
step = 1,
|
|
30
|
+
rows = 4,
|
|
31
|
+
options = {}
|
|
32
|
+
} = attribute;
|
|
33
|
+
const {
|
|
34
|
+
placeholder = "",
|
|
35
|
+
defaultValue = "",
|
|
36
|
+
customErrorMessage = "",
|
|
37
|
+
regex = "",
|
|
38
|
+
fieldNote = ""
|
|
39
|
+
} = options;
|
|
40
|
+
const fieldNoteFromAttribute = attribute.options?.fieldNote || "";
|
|
41
|
+
react.useEffect(() => {
|
|
42
|
+
const initialValue = value === void 0 ? defaultValue : value;
|
|
43
|
+
setInputValue(initialValue);
|
|
44
|
+
if (error) {
|
|
45
|
+
setValidationError(error);
|
|
46
|
+
}
|
|
47
|
+
if (onChange) {
|
|
48
|
+
onChange({ target: { value: initialValue } });
|
|
49
|
+
}
|
|
50
|
+
}, [value, defaultValue, onChange, error]);
|
|
51
|
+
const validateInput = (val) => {
|
|
52
|
+
if (required && (!val || val.toString().trim().length === 0)) {
|
|
53
|
+
return customErrorMessage || "This field is required";
|
|
54
|
+
}
|
|
55
|
+
if (!val || val.toString().trim().length === 0) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const stringValue = val.toString().trim();
|
|
59
|
+
if (minLength > 0 && stringValue.length < minLength) {
|
|
60
|
+
return customErrorMessage || `Minimum length is ${minLength} characters`;
|
|
61
|
+
}
|
|
62
|
+
if (maxLength > 0 && stringValue.length > maxLength) {
|
|
63
|
+
return customErrorMessage || `Maximum length is ${maxLength} characters`;
|
|
64
|
+
}
|
|
65
|
+
if (regex && stringValue) {
|
|
66
|
+
try {
|
|
67
|
+
const regexPattern = new RegExp(regex);
|
|
68
|
+
if (!regexPattern.test(stringValue)) {
|
|
69
|
+
return customErrorMessage || "Invalid format";
|
|
70
|
+
}
|
|
71
|
+
} catch (e) {
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
};
|
|
76
|
+
const handleChange = (e) => {
|
|
77
|
+
const newValue = e.target.value;
|
|
78
|
+
setInputValue(newValue);
|
|
79
|
+
setHasInteracted(true);
|
|
80
|
+
const error2 = validateInput(newValue);
|
|
81
|
+
setValidationError(error2);
|
|
82
|
+
if (onChange) {
|
|
83
|
+
onChange(e);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const displayError = error || hasInteracted && validationError;
|
|
87
|
+
const renderInput = () => {
|
|
88
|
+
const commonProps = {
|
|
89
|
+
name,
|
|
90
|
+
value: inputValue,
|
|
91
|
+
onChange: handleChange,
|
|
92
|
+
disabled,
|
|
93
|
+
placeholder: placeholder || (intlLabel.id ? formatMessage(intlLabel) : intlLabel.defaultMessage || "")
|
|
94
|
+
};
|
|
95
|
+
const inputStyle = {
|
|
96
|
+
width: "100%",
|
|
97
|
+
padding: "8px 12px",
|
|
98
|
+
border: `1px solid ${displayError ? "#d02b20" : "#dcdce4"}`,
|
|
99
|
+
borderRadius: "4px",
|
|
100
|
+
fontSize: "14px",
|
|
101
|
+
fontFamily: "inherit",
|
|
102
|
+
backgroundColor: disabled ? "#f6f6f9" : "#ffffff"
|
|
103
|
+
};
|
|
104
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
105
|
+
"input",
|
|
106
|
+
{
|
|
107
|
+
...commonProps,
|
|
108
|
+
type: "text",
|
|
109
|
+
style: inputStyle
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
return /* @__PURE__ */ jsxRuntime.jsx(designSystem.Box, { col: 6, children: /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Field.Root, { name, error: displayError, children: [
|
|
114
|
+
/* @__PURE__ */ jsxRuntime.jsxs(designSystem.Field.Label, { children: [
|
|
115
|
+
intlLabel.id ? formatMessage(intlLabel) : intlLabel.defaultMessage || name,
|
|
116
|
+
required && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: "#d02b20", marginLeft: "4px" }, children: "*" })
|
|
117
|
+
] }),
|
|
118
|
+
renderInput(),
|
|
119
|
+
displayError && /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Error, { children: displayError }),
|
|
120
|
+
description && (description.id || description.defaultMessage) && /* @__PURE__ */ jsxRuntime.jsx(designSystem.Field.Hint, { children: description.id ? formatMessage(description) : description.defaultMessage }),
|
|
121
|
+
(fieldNote || fieldNoteFromAttribute) && /* @__PURE__ */ jsxRuntime.jsx("span", { style: {
|
|
122
|
+
fontStyle: "italic",
|
|
123
|
+
color: "#666",
|
|
124
|
+
fontSize: "12px",
|
|
125
|
+
display: "block",
|
|
126
|
+
marginTop: "4px"
|
|
127
|
+
}, children: fieldNote || fieldNoteFromAttribute })
|
|
128
|
+
] }) });
|
|
129
|
+
};
|
|
130
|
+
exports.default = AdvancedInput;
|