diff --git a/components/Group/Form/Fields/AreaCheckbox.jsx b/components/Group/Form/Fields/AreaCheckbox.jsx index ccfaa3b6..17feb88c 100644 --- a/components/Group/Form/Fields/AreaCheckbox.jsx +++ b/components/Group/Form/Fields/AreaCheckbox.jsx @@ -1,3 +1,4 @@ +import { useEffect, useState } from 'react'; import Box from '@mui/material/Box'; import FormControlLabel from '@mui/material/FormControlLabel'; import Checkbox from '@mui/material/Checkbox'; @@ -9,27 +10,78 @@ export default function AreaCheckbox({ itemValue, name, value, - onChange, + control, }) { + const [isPhysicalArea, setIsPhysicalArea] = useState(false); + + const getPhysicalArea = (data) => + options.find((option) => data.includes(option.name)); + + const handleChange = (val) => + control.onChange({ target: { name, value: val } }); + + const physicalAreaValue = getPhysicalArea(value)?.name || ''; + + const toggleIsPhysicalArea = () => { + const updatedValue = value.filter((v) => !getPhysicalArea([v])); + handleChange(updatedValue); + setIsPhysicalArea((pre) => !pre); + }; + + const handleCheckboxChange = (_value) => { + const updatedValue = value.includes(_value) + ? value.filter((v) => v !== _value) + : [...value, _value]; + handleChange(updatedValue); + }; + + const handlePhysicalAreaChange = ({ target }) => { + const updatedValue = value + .filter((v) => !getPhysicalArea([v])) + .concat(target.value); + handleChange(updatedValue); + }; + + const physicalAreaControl = { + onChange: handlePhysicalAreaChange, + onBlur: handlePhysicalAreaChange, + }; + + useEffect(() => { + if (value.find((v) => getPhysicalArea([v]))) setIsPhysicalArea(true); + }, [value]); + return ( <> - } label="實體活動" /> + } + label="實體活動" + checked={isPhysicalArea} + /> { - if (value.length > max) setError(errorMessage); - else setError(''); - }, [max, value]); - return ( <> {error} diff --git a/components/Group/Form/Fields/Upload.jsx b/components/Group/Form/Fields/Upload.jsx index e9baeeea..75309471 100644 --- a/components/Group/Form/Fields/Upload.jsx +++ b/components/Group/Form/Fields/Upload.jsx @@ -5,8 +5,8 @@ import DeleteSvg from '@/public/assets/icons/delete.svg'; import { StyledUpload } from '../Form.styled'; import UploadSvg from './UploadSvg'; -export default function Upload({ name, onChange }) { - const [preview, setPreview] = useState(''); +export default function Upload({ name, value, control }) { + const [preview, setPreview] = useState(value || ''); const [error, setError] = useState(''); const inputRef = useRef(); @@ -17,17 +17,25 @@ export default function Upload({ name, onChange }) { value: file, }, }; - onChange(event); + control.onChange(event); }; const handleFile = (file) => { const imageType = /image.*/; + const maxSize = 500 * 1024; // 500 KB + + setPreview(''); setError(''); if (!file.type.match(imageType)) { setError('僅支援上傳圖片唷!'); return; } + if (file.size > maxSize) { + setError('圖片最大限制 500 KB'); + return; + } + const reader = new FileReader(); reader.onload = (e) => setPreview(e.target.result); reader.readAsDataURL(file); diff --git a/components/Group/Form/Fields/index.jsx b/components/Group/Form/Fields/index.jsx index 202c1f25..f8383205 100644 --- a/components/Group/Form/Fields/index.jsx +++ b/components/Group/Form/Fields/index.jsx @@ -1,56 +1,34 @@ +import { useId } from 'react'; import AreaCheckbox from './AreaCheckbox'; import Select from './Select'; import TagsField from './TagsField'; import TextField from './TextField'; import Upload from './Upload'; import Wrapper from './Wrapper'; -import useWrapperProps from './useWrapperProps'; -const Fields = {}; +const withWrapper = (Component) => (props) => { + const id = useId(); + const formItemId = `form-item-${id}`; + const { required, label, tooltip } = props; -Fields.AreaCheckbox = (props) => { - const wrapperProps = useWrapperProps(props); return ( - - + + ); }; -Fields.Select = (props) => { - const wrapperProps = useWrapperProps(props); - return ( - -