diff --git a/Grid/GridControlProperties.tsx b/Grid/GridControlProperties.tsx index dfb1e15..f14ebb8 100644 --- a/Grid/GridControlProperties.tsx +++ b/Grid/GridControlProperties.tsx @@ -1,102 +1,120 @@ import Select from '@components/Select'; -import { colors } from '@lib/theme'; +import theme, { colors } from '@lib/theme'; import { gridUnits } from '@lib/utils'; -import { Grid } from '@primer/components'; import { GrabberIcon, XIcon } from '@primer/octicons-react'; import { useShiftKeyPressed } from '@ui/useShftKeyPressed'; -import React, { FC, memo, useCallback } from 'react'; -import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; +import { Entry } from 'css-grid-template-parser'; +import React, { CSSProperties, memo, useCallback } from 'react'; +import { SortableElement, SortableHandle } from 'react-sortable-hoc'; +import { useRecoilState, useSetRecoilState } from 'recoil'; import { GridControlObjKey } from './GridControlId'; import { gridControlState, selectedControlState } from './gridState'; type Handler = ( e: React.ChangeEvent ) => void; -const GridControlProperties: FC<{ +type GridControlPropertiesProps = Entry & { id: string | `${GridControlObjKey}.${number}`; -}> = ({ id }) => { - const { canDelete, ...control } = useRecoilValue(gridControlState(id)); - const [selectedIds, setSelectedIds] = useRecoilState(selectedControlState); - const setControl = useSetRecoilState(gridControlState(id)); - const shiftKeyPressed = useShiftKeyPressed(); - const handleChange: Handler = useCallback( - (e) => { - setControl((prev) => ({ - ...prev, - [e.currentTarget.name]: e.currentTarget.value, - })); - }, - [setControl] - ); - const isSelected = selectedIds.includes(id); - const style = isSelected - ? id.split('.').includes('rows') - ? { - background: colors.yellow[4], - boxShadow: `0 2px 0 0 ${colors.yellow[8]}`, - } - : { - background: colors.blue[4], - boxShadow: `0 2px 0 0 ${colors.blue[8]}`, - } - : { background: 'transparent' }; + canDelete: boolean; +}; - const onEnter = useCallback(() => { - setSelectedIds((ids) => { - // Do nothing if the element is already selected - if (isSelected) return ids; +const GridControlProperties = SortableElement( + ({ id, amount, unit, canDelete }: GridControlPropertiesProps) => { + const [selectedIds, setSelectedIds] = useRecoilState(selectedControlState); + const setControl = useSetRecoilState(gridControlState(id)); + const shiftKeyPressed = useShiftKeyPressed(); + const handleChange: Handler = useCallback( + (e) => { + setControl((prev) => ({ + ...prev, + [e.currentTarget.name]: e.currentTarget.value, + })); + }, + [setControl] + ); + const isSelected = selectedIds.includes(id); + const style: CSSProperties = isSelected + ? id.split('.').includes('rows') + ? { + background: colors.yellow[4], + boxShadow: `0 2px 0 0 ${colors.yellow[8]}`, + } + : { + background: colors.blue[4], + boxShadow: `0 2px 0 0 ${colors.blue[8]}`, + } + : { background: 'transparent' }; - // Add this element to the selection if shift is pressed - if (shiftKeyPressed) return [...ids, id]; + const onEnter = useCallback(() => { + setSelectedIds((ids) => { + if (isSelected) return ids; + if (shiftKeyPressed) return [...ids, id]; + return [id]; + }); + }, [id, isSelected, setSelectedIds, shiftKeyPressed]); - // Otherwise, make this one the only selected element - return [id]; - }); - }, [id, isSelected, setSelectedIds, shiftKeyPressed]); - const onLeave = useCallback(() => { - if (!shiftKeyPressed) setSelectedIds([]); - }, [setSelectedIds, shiftKeyPressed]); - return ( - -
- -
+ const onLeave = useCallback(() => { + if (!shiftKeyPressed) setSelectedIds([]); + }, [setSelectedIds, shiftKeyPressed]); - - + { - if (event.target === event.currentTarget) - setGridGap({ - ...gridGap, - [event.currentTarget.name]: event.currentTarget.value, - }); + setGridGap({ + ...gridGap, + [event.currentTarget.name]: event.currentTarget.value, + }); }} /> -
+ ); }; diff --git a/Grid/gridState.tsx b/Grid/gridState.tsx index 1ac002a..9ee496b 100644 --- a/Grid/gridState.tsx +++ b/Grid/gridState.tsx @@ -38,9 +38,9 @@ export const gridControlState = selectorFamily({ key: 'gridControlState', get: (id) => ({ get }) => { const [key, index] = id.split('.'); - const stack = get(gridState)[key]; - const control = stack[index]; - return { ...control, canDelete: stack.length <= 1 }; + const stack = get(gridState)?.[key]; + const control = stack?.[index]; + return control; }, set: (id) => ({ set }, newValue) => { const [key, index] = id.split('.'); diff --git a/components/Select.tsx b/components/Select.tsx index 6042fc0..1b466c9 100644 --- a/components/Select.tsx +++ b/components/Select.tsx @@ -12,7 +12,6 @@ const Select: FC = ({ options, style, ...props }) => { return (