@pagamio/frontend-commons-lib 0.8.352 → 0.8.353

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.
@@ -47,31 +47,36 @@ const FormEngine = ({ fields, onSubmit, initialValues, layout = 'vertical', isNo
47
47
  }
48
48
  return initialValues;
49
49
  };
50
- const { control, handleSubmit, watch, formState: { errors, isSubmitting }, reset, setValue, trigger, } = useForm({ mode: 'onBlur', defaultValues: getEffectiveInitialValues() });
51
- const allFields = watch();
52
- const password = watch('password');
53
- const confirmPassword = watch('confirmPassword');
54
- //re-validate confirmPassword when password changes
50
+ const { control, handleSubmit, watch, formState: { errors, isSubmitting }, reset, setValue, trigger, getValues, } = useForm({ mode: 'onBlur', defaultValues: getEffectiveInitialValues() });
51
+ // Subscribe to field changes via the callback form of `watch` so this
52
+ // component does NOT re-render on every keystroke / select change. The
53
+ // previous `const allFields = watch()` (no args) caused the entire
54
+ // FormEngine subtree including all Radix Select popovers — to re-render
55
+ // synchronously inside the field's onChange. That race produced the
56
+ // "click twice to commit" symptom on Select inputs: the first click's
57
+ // outside-dismiss fired during the parent re-render and the popover lost
58
+ // its open state. Subscribing via callback keeps RHF state updates
59
+ // isolated to the affected Controller subtree.
55
60
  useEffect(() => {
56
- if (password && confirmPassword) {
57
- trigger('confirmPassword');
58
- }
59
- }, [password, trigger, confirmPassword]);
60
- useEffect(() => {
61
- if (getFieldValues) {
62
- getFieldValues(allFields);
63
- }
64
- }, [allFields, getFieldValues]);
65
- // Save form data when fields change (for persistence)
66
- useEffect(() => {
67
- if (persistenceKey && allFields && Object.keys(allFields).length > 0) {
68
- // Only save if there are actual field values (not just empty object)
69
- const hasValues = Object.values(allFields).some((value) => value !== undefined && value !== null && value !== '');
70
- if (hasValues) {
71
- saveFormData(allFields, true);
61
+ const subscription = watch((values, info) => {
62
+ if (info?.name === 'password') {
63
+ const cp = values?.confirmPassword;
64
+ if (values?.password && cp) {
65
+ trigger('confirmPassword');
66
+ }
72
67
  }
73
- }
74
- }, [allFields, persistenceKey, saveFormData]);
68
+ if (getFieldValues) {
69
+ getFieldValues(values);
70
+ }
71
+ if (persistenceKey) {
72
+ const hasValues = Object.values(values ?? {}).some((value) => value !== undefined && value !== null && value !== '');
73
+ if (hasValues) {
74
+ saveFormData(values, true);
75
+ }
76
+ }
77
+ });
78
+ return () => subscription.unsubscribe();
79
+ }, [watch, trigger, getFieldValues, persistenceKey, saveFormData]);
75
80
  // Expose form control methods via ref
76
81
  useEffect(() => {
77
82
  if (formRef && 'current' in formRef) {
@@ -109,11 +114,12 @@ const FormEngine = ({ fields, onSubmit, initialValues, layout = 'vertical', isNo
109
114
  const validateConfirmPassword = (value) => {
110
115
  if (!value)
111
116
  return 'Please confirm your password';
112
- if (!password)
117
+ const currentPassword = getValues('password');
118
+ if (!currentPassword)
113
119
  return 'Please enter a password first';
114
- if (password.length < 8)
120
+ if (currentPassword.length < 8)
115
121
  return 'Please enter a valid password first (minimum 8 characters)';
116
- if (value !== password)
122
+ if (value !== currentPassword)
117
123
  return 'Passwords do not match';
118
124
  return true;
119
125
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pagamio/frontend-commons-lib",
3
3
  "description": "Pagamio library for Frontend reusable components like the form engine and table container",
4
- "version": "0.8.352",
4
+ "version": "0.8.353",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "provenance": false