From fa32ab17dee6185cad8795651a22f65a7963b0ea Mon Sep 17 00:00:00 2001 From: Vishesh 'ironeagle' Bangotra Date: Sun, 24 May 2026 14:35:39 +0530 Subject: [PATCH] sorted options only when selecting again --- react-openapi/components/FilterBar.tsx | 87 ++++++++++++++++++-------- 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/react-openapi/components/FilterBar.tsx b/react-openapi/components/FilterBar.tsx index 447a166..4e28b7e 100644 --- a/react-openapi/components/FilterBar.tsx +++ b/react-openapi/components/FilterBar.tsx @@ -11,6 +11,63 @@ import DoneIcon from "@mui/icons-material/Done"; import FilterListIcon from "@mui/icons-material/FilterList"; import { ResourceField, ResourceMode } from "../types/config"; +function FilterAutocomplete({ + options, + value, + label, + onChange, +}: { + options: string[]; + value: string[]; + label: string; + onChange: (val: string[]) => void; +}) { + const listboxRef = React.useRef(null); + const scrollPosRef = React.useRef(0); + const [frozenValue, setFrozenValue] = React.useState(value); + + const sortedOptions = React.useMemo(() => { + const sel = new Set(frozenValue); + const picked: string[] = []; + const rest: string[] = []; + for (const o of options) { + if (sel.has(o)) picked.push(o); + else rest.push(o); + } + return [...picked, ...rest]; + }, [options, frozenValue]); + + return ( + option} + onChange={(_, val) => onChange(val.length > 0 ? val : [])} + onOpen={() => setFrozenValue(value)} + onClose={() => setFrozenValue(value)} + ListboxProps={{ + ref: listboxRef, + onScroll: (e) => { scrollPosRef.current = (e.target as HTMLUListElement).scrollTop; }, + }} + renderOption={(props, option, { selected }) => { + const { key, ...rest } = props; + return ( +
  • + {selected ? : } + {option} +
  • + ); + }} + renderInput={(params) => } + sx={{ '& .MuiOutlinedInput-root': { minHeight: '3rem', py: 0.5 } }} + /> + ); +} + function extractOptions( fieldName: string, field: ResourceField, @@ -94,35 +151,13 @@ function renderFilterInput( } const selected = Array.isArray(value) ? value : []; - // const sortedOptions = React.useMemo(() => { - // const sel = new Set(selected); - // const picked: string[] = []; - // const rest: string[] = []; - // for (const o of options) { - // if (sel.has(o)) picked.push(o); - // else rest.push(o); - // } - // return [...picked, ...rest]; - // }, [options, selected]); return ( - onChange("value", val.length > 0 ? val : undefined)} - renderOption={(props, option, { selected }) => ( -
  • - {selected ? : } - {option} -
  • - )} - renderInput={(params) => } - sx={{ '& .MuiOutlinedInput-root': { minHeight: '3rem', py: 0.5 } }} + value={selected} + label={field.label} + onChange={(val) => onChange("value", val.length > 0 ? val : undefined)} /> ); }