Skip to content

form.reset #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
usmankhanXislabs opened this issue Jan 7, 2025 · 1 comment
Open

form.reset #41

usmankhanXislabs opened this issue Jan 7, 2025 · 1 comment

Comments

@usmankhanXislabs
Copy link

usmankhanXislabs commented Jan 7, 2025

I am working on a React-based customer form component where a user can create or edit customer details, including assigning customer groups using a MultiSelect component. The data for customerGroupList is fetched from an API, and the selected groups are dynamically mapped to the form.

The issue is that when editing a customer, the selectedGroups array is sometimes empty, even though the customerForEdit.customerGroup contains valid data. This results in the form not pre-filling the MultiSelect with the correct options.

'use client';
import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { useSelector, useDispatch } from 'react-redux';
import * as customersActions from '../../app/redux/cutomers/cutomersActions';
import { useCustomerGroupList } from '@/config/customerGroupsSearchHook';
import { MultiSelect } from '../multi-select';

export const CustomerForm: React.FC = () => {
const dispatch = useDispatch();
const { customerGroupList } = useCustomerGroupList();
const [selectedCustomerGroups, setSelectedCustomerGroups] = useState<string[]>([]);

const form = useForm({
defaultValues: {
_id: '',
name: '',
email: '',
address: '',
mobile: '',
zip: '',
businessName: '',
customerGroup: [],
},
});

const {
customers: { customerForEdit },
}: any = useSelector((state: RootState) => ({
customers: state.customers,
}));

useEffect(() => {
if (customerForEdit && customerForEdit?._id) {
const selectedGroups = customerGroupList.filter((group) =>
customerForEdit.customerGroup.includes(group.value)
);

  const updatedData = {
    _id: customerForEdit._id,
    name: customerForEdit.name,
    address: customerForEdit.address,
    businessName: customerForEdit.businessName,
    zip: customerForEdit.zip,
    mobile: customerForEdit.mobile,
    email: customerForEdit.email,
    customerGroup: selectedGroups.map((item) => item.value),
  };

  form.reset(updatedData);
  setSelectedCustomerGroups(updatedData.customerGroup);
} else {
  form.reset({
    _id: '',
    name: '',
    email: '',
    address: '',
    mobile: '',
    zip: '',
    businessName: '',
    customerGroup: [],
  });
}

}, [customerForEdit, customerGroupList]);

const onSubmit = async (data) => {
const updatedData = {
...data,
customerGroup: selectedCustomerGroups,
};
console.log('Form submitted:', updatedData);
};

return (


{/* Other fields */}

Submit

);
};

@s3f5
Copy link

s3f5 commented Feb 9, 2025

Don't think its a Multiselect issue, might be more a form state thing. Please share a repo or a sandbox to reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants