diff --git a/src/components/TableToolbar/HiddenFields.tsx b/src/components/TableToolbar/HiddenFields.tsx index 8015f6f5..0a537713 100644 --- a/src/components/TableToolbar/HiddenFields.tsx +++ b/src/components/TableToolbar/HiddenFields.tsx @@ -1,12 +1,26 @@ -import { useEffect, useRef, useMemo, useState } from "react"; -import { useAtom } from "jotai"; +import { + useEffect, + useRef, + useMemo, + useState, + forwardRef, + ChangeEvent, +} from "react"; +import { useAtom, useSetAtom } from "jotai"; import { isEqual } from "lodash-es"; import { colord } from "colord"; +import { + DragDropContext, + Droppable, + Draggable, + DropResult, +} from "react-beautiful-dnd"; import { Box, AutocompleteProps, Theme } from "@mui/material"; import VisibilityIcon from "@mui/icons-material/VisibilityOutlined"; import VisibilityOffIcon from "@mui/icons-material/VisibilityOffOutlined"; import IconSlash from "@src/components/IconSlash"; +import DragIndicatorOutlinedIcon from "@mui/icons-material/DragIndicatorOutlined"; import ColumnSelect, { ColumnItem } from "@src/components/Table/ColumnSelect"; import ButtonWithStatus from "@src/components/ButtonWithStatus"; @@ -16,7 +30,11 @@ import { userSettingsAtom, updateUserSettingsAtom, } from "@src/atoms/projectScope"; -import { tableScope, tableIdAtom } from "@src/atoms/tableScope"; +import { + tableScope, + tableIdAtom, + updateColumnAtom, +} from "@src/atoms/tableScope"; import { formatSubTableName } from "@src/utils/table"; export default function HiddenFields() { @@ -57,6 +75,9 @@ export default function HiddenFields() { } setOpen(false); }; + + // disable drag if search box is not empty + const [disableDrag, setDisableDrag] = useState(false); const renderOption: AutocompleteProps< any, true, @@ -67,49 +88,119 @@ export default function HiddenFields() { colord(theme.palette.background.paper) .mix("#fff", theme.palette.mode === "dark" ? 0.16 : 0) .alpha(1); - return ( -
  • - - + {(provided) => ( +
  • + + + disableDrag ? theme.palette.action.disabledOpacity : 1, }, - ]} - > - - slashColor(theme).toHslString(), - }, - ".Mui-focused & .icon-slash-mask": { - stroke: (theme) => - slashColor(theme) - .mix( - theme.palette.primary.main, - theme.palette.action.selectedOpacity + - theme.palette.action.hoverOpacity - ) - .alpha(1) - .toHslString(), - }, - }, - selected ? { strokeDashoffset: 0 } : {}, - ]} - /> - - -
  • + ]} + /> + + + + + slashColor(theme).toHslString(), + }, + ".Mui-focused & .icon-slash-mask": { + stroke: (theme) => + slashColor(theme) + .mix( + theme.palette.primary.main, + theme.palette.action.selectedOpacity + + theme.palette.action.hoverOpacity + ) + .alpha(1) + .toHslString(), + }, + }, + selected ? { strokeDashoffset: 0 } : {}, + ]} + /> + + + + )} + ); }; + const updateColumn = useSetAtom(updateColumnAtom, tableScope); + + // updates column on drag end + function handleOnDragEnd(result: DropResult) { + if (!result.destination) return; + updateColumn({ + key: result.draggableId, + config: {}, + index: result.destination.index, + }); + } + + // checks whether to disable reordering when search filter is applied + function checkToDisableDrag(e: ChangeEvent) { + setDisableDrag(e.target.value !== ""); + } + + const ListboxComponent = forwardRef(function ListboxComponent( + props: React.HTMLAttributes, + ulRef: any /*React.ForwardedRef*/ + ) { + const { children, ...other } = props; + + return ( + + + {(provided) => ( +
      { + provided.innerRef(ref); + if (ulRef !== null) { + ulRef(ref); + } + }} + > + {props.children} + {provided.placeholder} +
    + )} +
    +
    + ); + }); + return ( <> { + setHiddenFields(updates); + setDisableDrag(false); + }} onClose={handleSave} clearText="Show all" selectAllText="Hide all"