@konoma-development/react-components 0.2.3 → 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.
@@ -2524,28 +2524,31 @@ function Form({
2524
2524
  }) {
2525
2525
  const [errors, setErrors] = useState({});
2526
2526
  const [submitAttempted, setSubmitAttempted] = useState(false);
2527
- const validate = useCallback(async () => {
2528
- let invalid = false;
2529
- const newErrors = {};
2530
- Object.entries(data).forEach(([name, value]) => {
2531
- if (!validators[name]) {
2532
- return;
2533
- }
2534
- newErrors[name] = validators[name].map((validator) => {
2535
- const validation = validator(value);
2536
- if (validation) {
2537
- 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;
2538
2534
  }
2539
- return validation;
2540
- }).filter((v) => v);
2541
- });
2542
- setErrors(newErrors);
2543
- onValidation(newErrors);
2544
- if (process.env.NODE_ENV === "development") {
2545
- console.info("Form errors", newErrors);
2546
- }
2547
- return invalid;
2548
- }, [data]);
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
+ );
2549
2552
  useEffect(() => {
2550
2553
  if (submitAttempted) {
2551
2554
  validate();
@@ -2553,7 +2556,7 @@ function Form({
2553
2556
  }, [validate, submitAttempted]);
2554
2557
  async function submit(event) {
2555
2558
  event.preventDefault();
2556
- const invalid = await validate();
2559
+ const invalid = await validate(true);
2557
2560
  if (invalid) {
2558
2561
  setSubmitAttempted(true);
2559
2562
  return;