@konoma-development/react-components 0.2.2 → 0.2.4
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.
|
@@ -2415,6 +2415,7 @@ function Checkbox({
|
|
|
2415
2415
|
name,
|
|
2416
2416
|
className: "h-0 w-0 appearance-none",
|
|
2417
2417
|
defaultChecked: !!defaultValue,
|
|
2418
|
+
"data-testid": dataTestId,
|
|
2418
2419
|
onChange: (e) => onChange(e.target.checked, e),
|
|
2419
2420
|
disabled
|
|
2420
2421
|
}
|
|
@@ -2434,6 +2435,7 @@ function Checkbox({
|
|
|
2434
2435
|
value: 0,
|
|
2435
2436
|
className: "h-0 w-0 appearance-none",
|
|
2436
2437
|
defaultChecked: !!defaultValue,
|
|
2438
|
+
"data-testid": dataTestId,
|
|
2437
2439
|
onChange: (e) => onChange(e.target.checked, e),
|
|
2438
2440
|
disabled
|
|
2439
2441
|
}
|
|
@@ -2522,28 +2524,31 @@ function Form({
|
|
|
2522
2524
|
}) {
|
|
2523
2525
|
const [errors, setErrors] = useState({});
|
|
2524
2526
|
const [submitAttempted, setSubmitAttempted] = useState(false);
|
|
2525
|
-
const validate = useCallback(
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
newErrors[name] = validators[name].map((validator) => {
|
|
2533
|
-
const validation = validator(value);
|
|
2534
|
-
if (validation) {
|
|
2535
|
-
invalid = true;
|
|
2527
|
+
const validate = useCallback(
|
|
2528
|
+
async (triggeredBySubmit) => {
|
|
2529
|
+
let invalid = false;
|
|
2530
|
+
const newErrors = {};
|
|
2531
|
+
Object.entries(data).forEach(([name, value]) => {
|
|
2532
|
+
if (!validators[name]) {
|
|
2533
|
+
return;
|
|
2536
2534
|
}
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2535
|
+
newErrors[name] = validators[name].map((validator) => {
|
|
2536
|
+
const validation = validator(value);
|
|
2537
|
+
if (validation) {
|
|
2538
|
+
invalid = true;
|
|
2539
|
+
}
|
|
2540
|
+
return validation;
|
|
2541
|
+
}).filter((v) => v);
|
|
2542
|
+
});
|
|
2543
|
+
setErrors(newErrors);
|
|
2544
|
+
onValidation(newErrors, triggeredBySubmit);
|
|
2545
|
+
if (process.env.NODE_ENV === "development") {
|
|
2546
|
+
console.info("Form errors", newErrors);
|
|
2547
|
+
}
|
|
2548
|
+
return invalid;
|
|
2549
|
+
},
|
|
2550
|
+
[data]
|
|
2551
|
+
);
|
|
2547
2552
|
useEffect(() => {
|
|
2548
2553
|
if (submitAttempted) {
|
|
2549
2554
|
validate();
|
|
@@ -2551,7 +2556,7 @@ function Form({
|
|
|
2551
2556
|
}, [validate, submitAttempted]);
|
|
2552
2557
|
async function submit(event) {
|
|
2553
2558
|
event.preventDefault();
|
|
2554
|
-
const invalid = await validate();
|
|
2559
|
+
const invalid = await validate(true);
|
|
2555
2560
|
if (invalid) {
|
|
2556
2561
|
setSubmitAttempted(true);
|
|
2557
2562
|
return;
|