| | |
| | | import { useWatch, useFormContext } from "react-hook-form"; |
| | | import { Stack, Grid, Box, Typography, ToggleButton, ToggleButtonGroup } from '@mui/material'; |
| | | import { alpha } from '@mui/material/styles'; |
| | | import * as Common from '@/utils/common'; |
| | | import { EDIT_MODE } from '@/config/setting'; |
| | | import EditBaseAside from "../components/EditBaseAside"; |
| | | import CustomerTopToolBar from "../components/EditTopToolBar"; |
| | |
| | | } |
| | | |
| | | const DIR_RULE_ANGLES = [0, 90, 180, 270]; |
| | | const DEFAULT_DIR_RULES = DIR_RULE_ANGLES.map(angle => ({ angle, enabled: true })); |
| | | |
| | | const normalizeDirRuleValue = (value) => { |
| | | let parsed = []; |
| | |
| | | |
| | | const DirectionRuleInput = () => { |
| | | const translate = useTranslate(); |
| | | const record = useRecordContext(); |
| | | const { register, setValue } = useFormContext(); |
| | | const dirRuleValue = useWatch({ name: 'dirRule' }); |
| | | |
| | |
| | | register('dirRule'); |
| | | }, [register]); |
| | | |
| | | const rules = useMemo(() => normalizeDirRuleValue(dirRuleValue), [dirRuleValue]); |
| | | const normalizedRulesString = useMemo(() => JSON.stringify(rules), [rules]); |
| | | const initialDirRuleString = useMemo(() => { |
| | | const source = record?.dirRule ?? DEFAULT_DIR_RULES; |
| | | return JSON.stringify(normalizeDirRuleValue(source)); |
| | | }, [record]); |
| | | |
| | | useEffect(() => { |
| | | if (dirRuleValue !== normalizedRulesString) { |
| | | setValue('dirRule', normalizedRulesString, { shouldDirty: false, shouldTouch: false }); |
| | | if (dirRuleValue === undefined || dirRuleValue === null || dirRuleValue === '') { |
| | | setValue('dirRule', initialDirRuleString, { shouldDirty: false, shouldTouch: false }); |
| | | return; |
| | | } |
| | | }, [dirRuleValue, normalizedRulesString, setValue]); |
| | | |
| | | const normalizedString = JSON.stringify(normalizeDirRuleValue(dirRuleValue)); |
| | | if (dirRuleValue !== normalizedString) { |
| | | setValue('dirRule', normalizedString, { shouldDirty: false, shouldTouch: false }); |
| | | } |
| | | }, [dirRuleValue, initialDirRuleString, setValue]); |
| | | |
| | | const rules = useMemo( |
| | | () => normalizeDirRuleValue(dirRuleValue ?? initialDirRuleString), |
| | | [dirRuleValue, initialDirRuleString] |
| | | ); |
| | | |
| | | const disabledAngle = useMemo(() => { |
| | | const disabled = rules.find(rule => !rule.enabled); |
| | |
| | | > |
| | | {rules.map(rule => { |
| | | const isDisabled = !rule.enabled; |
| | | const disabledStyles = { |
| | | color: (theme) => theme.palette.error.main, |
| | | borderColor: (theme) => theme.palette.error.main, |
| | | bgcolor: (theme) => alpha(theme.palette.error.main, 0.08), |
| | | '&:hover': { |
| | | bgcolor: (theme) => alpha(theme.palette.error.main, 0.16), |
| | | }, |
| | | }; |
| | | return ( |
| | | <ToggleButton |
| | | key={rule.angle} |
| | |
| | | flexDirection: 'column', |
| | | gap: 0.5, |
| | | py: 1.5, |
| | | '&.Mui-selected': { |
| | | color: (theme) => theme.palette.error.main, |
| | | borderColor: (theme) => theme.palette.error.main, |
| | | bgcolor: (theme) => alpha(theme.palette.error.main, 0.08), |
| | | '&:hover': { |
| | | bgcolor: (theme) => alpha(theme.palette.error.main, 0.16), |
| | | }, |
| | | }, |
| | | ...(isDisabled ? disabledStyles : {}), |
| | | '&.Mui-selected': disabledStyles, |
| | | '& .dirRuleStatus': { |
| | | color: isDisabled ? 'error.main' : 'text.secondary', |
| | | }, |