import { createContext, useContext, useEffect, useState } from 'react'; import { Form, Input, Row, Col, Select, DatePicker, Space, Button } from 'antd'; import { objectMapper, at, isEmpty } from '@/utils/commons'; import { DATE_FORMAT } from '@/config'; import useFormStore from '@/stores/Form'; import usePresets from '@/hooks/usePresets'; import { useTranslation } from 'react-i18next'; const { RangePicker } = DatePicker; const SearchForm = ({ initialValue, onSubmit, onReset, ...props }) => { const { t } = useTranslation(); const presets = usePresets(); const [formValues, setFormValues] = useFormStore((state) => [state.formValues, state.setFormValues]); const [formValuesToSub, setFormValuesToSub] = useFormStore((state) => [state.formValuesToSub, state.setFormValuesToSub]); const [form] = Form.useForm(); const { sort, hides, shows, fieldProps } = { sort: '', // initialValue: '', fieldProps: '', hides: [], shows: [], ...props.defaultValue, }; const { confirmText } = props; const readValues = { ...initialValue, ...formValues }; const formValuesMapper = (values) => { const destinationObject = { 'referenceNo': { key: 'referenceNo' }, 'dates': [ { key: 'startdate', transform: (arrVal) => (arrVal ? arrVal[0].format(DATE_FORMAT) : '') }, { key: 'enddate', transform: (arrVal) => (arrVal ? arrVal[1].format(DATE_FORMAT) : '') }, ], 'invoiceStatus': { key: 'invoiceStatus', transform: (value) => value?.value || value?.key || '', default: '' }, }; let dest = {}; const { dates, ...omittedValue } = values; dest = { ...omittedValue, ...objectMapper(values, destinationObject) }; for (const key in dest) { if (Object.prototype.hasOwnProperty.call(dest, key)) { dest[key] = typeof dest[key] === 'string' ? (dest[key] || '').trim() : dest[key]; } } // omit empty Object.keys(dest).forEach((key) => (dest[key] == null || dest[key] === '' || dest[key].length === 0) && delete dest[key]); return dest; }; useEffect(() => { const dest = formValuesMapper(formValues); setFormValuesToSub(dest); return () => {}; }, []); const onFinish = (values) => { console.log('Received values of form, origin form value: ', values); const dest = formValuesMapper(values); console.log('form value send to onSubmit:', dest); const str = new URLSearchParams(dest).toString(); setFormValues(values); setFormValuesToSub(dest); if (typeof onSubmit === 'function') { onSubmit(null, dest, values, str); } }; const handleReset = () => { form.setFieldsValue({ // 'DateType': undefined, }); if (typeof onReset === 'function') { onReset(); } }; const onValuesChange = (changedValues, allValues) => { const dest = formValuesMapper(allValues); setFormValues(allValues); setFormValuesToSub(dest); // console.log('form onValuesChange', Object.keys(changedValues), args); }; return ( <>
> ); }; function getFields(props) { const { fieldProps, form, presets, t } = props; const bigCol = 4 * 2; const midCol = 6; const layoutProps = { // gutter: { xs: 8, sm: 8, lg: 16 }, lg: { span: 4 }, md: { span: 8 }, sm: { span: 12 }, xs: { span: 24 }, }; const item = (name, sort = 0, render, col) => { const customCol = col || 4; const mdCol = customCol * 2; return { 'key': '', sort, name, render, 'hide': false, 'col': { lg: { span: customCol }, md: { span: mdCol < 8 ? 10 : mdCol }, flex: mdCol < 8 ? '1 0' : '' }, }; }; let baseChildren = []; baseChildren = [ item( 'referenceNo', 99,