fix(FE-42): fix validation supplier form and multi select component

This commit is contained in:
randy-ar
2025-10-13 11:25:39 +07:00
parent 24b702548d
commit 9b53c75f2f
3 changed files with 135 additions and 103 deletions
@@ -2,7 +2,10 @@ import * as Yup from 'yup';
export const SupplierFormSchema = Yup.object({
name: Yup.string().required('Nama wajib diisi!'),
alias: Yup.string().required('Alias wajib diisi!'),
alias: Yup.string()
.matches(/^[A-Za-z0-9]+$/, 'Alias hanya boleh berisi huruf dan angka tanpa spasi atau simbol!')
.max(5, 'Alias maksimal 5 karakter!')
.required('Alias wajib diisi!'),
pic: Yup.string().required('PIC wajib diisi!'),
type: Yup.object({
value: Yup.string().required(),
@@ -21,7 +21,6 @@ import SelectInput, { OptionType } from '@/components/input/SelectInput';
import { Icon } from '@iconify/react';
import Button from '@/components/Button';
import TextInput from '@/components/input/TextInput';
import TagInput from '@/components/input/TagInput';
import TextArea from '@/components/input/TextArea';
import { cn } from '@/lib/helper';
import ConfirmationModal from '@/components/modal/ConfirmationModal';
@@ -42,9 +41,7 @@ const SupplierForm = ({
// Setup State
const [supplierFormErrorMessage, setSupplierFormErrorMessage] = useState('');
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
const [typeSelectInputValue, setTypeSelectInputValue] = useState('');
const [categorySelectInputValue, setCategorySelectInputValue] = useState('');
const [hatcheryTagInputValue, setHatcheryTagInputValue] = useState('');
const [hatcheryOptionsValues, setHatcheryOptionValues] = useState<OptionType[]>([]);
// -- Options data mapping
const typeOptions = TYPE_OPTIONS;
@@ -108,8 +105,6 @@ const SupplierForm = ({
};
// Memo
console.log('Memo');
console.log(initialValues);
const formikInitialValues = useMemo<SupplierFormValues>(() => {
return {
name: initialValues?.name ?? '',
@@ -125,7 +120,7 @@ const SupplierForm = ({
account_number: initialValues?.account_number ?? '',
due_date: initialValues?.due_date ?? 1,
};
}, [initialValues]);
}, [initialValues, typeOptions, categoryOptions]);
// Formik
const formik = useFormik<SupplierFormValues>({
@@ -172,8 +167,22 @@ const SupplierForm = ({
// Initialize Formik
useEffect(() => {
formikSetValues(formikInitialValues);
setHatcheryTagInputValue(formikInitialValues.hatchery);
}, [formikSetValues, formikInitialValues, hatcheryTagInputValue]);
if(formType != 'add'){
const hatcheryArrays = formikInitialValues.hatchery.split(',');
const hatcheryCreatedOptions = hatcheryArrays.map((item) => ({
value: item,
label: item,
}));
setHatcheryOptionValues(hatcheryCreatedOptions);
}
}, [formikSetValues, formikInitialValues, setHatcheryOptionValues]);
useEffect(() => {
const commaSeparatedValues = hatcheryOptionsValues.map((item) => item.value).join(',');
formikSetValues({
...formik.values,
hatchery: commaSeparatedValues,
})
}, [hatcheryOptionsValues, formikSetValues]);
// Option Handler
const typeChangeHandler = (val: OptionType | OptionType[] | null) => {
@@ -260,7 +269,6 @@ const SupplierForm = ({
}
onChange={typeChangeHandler}
options={typeOptions}
onInputChange={setTypeSelectInputValue}
isError={formik.touched.type && Boolean(formik.errors.type)}
errorMessage={formik.errors.type as string}
isDisabled={formType === 'detail'}
@@ -278,7 +286,6 @@ const SupplierForm = ({
}
onChange={categoryChangeHandler}
options={categoryOptions}
onInputChange={setCategorySelectInputValue}
isError={
formik.touched.category && Boolean(formik.errors.category)
}
@@ -287,17 +294,25 @@ const SupplierForm = ({
isClearable
isSearchable={true}
/>
<TagInput
name='hatchery'
<SelectInput
isMulti
createables
required
placeholder='Pilih Hatchery'
label='Hatchery'
value={hatcheryTagInputValue}
onChange={(value) => formik.setFieldValue('hatchery', value)}
isError={
formik.touched.hatchery && Boolean(formik.errors.hatchery)
}
errorMessage={formik.errors.hatchery}
readOnly={formType === 'detail'}
value={hatcheryOptionsValues}
onChange={(val) => {
console.log(val); // pastikan val = array of { value, label }
setHatcheryOptionValues(val as OptionType[]);
}}
isError={formik.touched.hatchery && Boolean(formik.errors.hatchery)}
errorMessage={formik.errors.hatchery as string}
isDisabled={formType === 'detail'}
isClearable
isSearchable={true}
options={[]}
/>
<TextInput
required
label='Nomor Telepon'