Skip to content

Commit

Permalink
use shared multiselect component
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Oct 16, 2024
1 parent ba4be9b commit 32da626
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 76 deletions.
9 changes: 3 additions & 6 deletions editor.planx.uk/src/@planx/components/Checklist/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import InputRow from "ui/shared/InputRow";
import InputRowItem from "ui/shared/InputRowItem";

import { BaseNodeData, Option, parseBaseNodeData } from "../shared";
import FlagsSelect from "../shared/FlagsSelect";
import { FlagsSelect } from "../shared/FlagsSelect";
import { ICONS } from "../ui";
import type { Category, Checklist, Group } from "./model";
import { toggleExpandableChecklist } from "./model";
Expand Down Expand Up @@ -91,17 +91,16 @@ const OptionEditor: React.FC<{
/>

<FlagsSelect
value={props.value.data.flag || ""}
value={props.value.data.flag && Array.from(props.value.data.flag)}
onChange={(ev) => {
props.onChange({
...props.value,
data: {
...props.value.data,
flag: ev.target.value as string,
flag: ev,
},
});
}}
sx={{ width: { md: "160px" }, maxWidth: "160px" }}
/>

{typeof props.index !== "undefined" &&
Expand Down Expand Up @@ -193,7 +192,6 @@ const Options: React.FC<{ formik: FormikHookReturn }> = ({ formik }) => {
text: "",
description: "",
val: "",
flag: "",
},
}) as Option
}
Expand Down Expand Up @@ -267,7 +265,6 @@ const Options: React.FC<{ formik: FormikHookReturn }> = ({ formik }) => {
text: "",
description: "",
val: "",
flag: "",
},
}) as Option
}
Expand Down
10 changes: 3 additions & 7 deletions editor.planx.uk/src/@planx/components/Question/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import InputRow from "ui/shared/InputRow";
import InputRowItem from "ui/shared/InputRowItem";

import { BaseNodeData, Option, parseBaseNodeData } from "../shared";
import FlagsSelect from "../shared/FlagsSelect";
import { FlagsSelect } from "../shared/FlagsSelect";
import { ICONS, InternalNotes, MoreInformation } from "../ui";

interface Props {
Expand Down Expand Up @@ -103,21 +103,18 @@ const OptionEditor: React.FC<{
/>
</InputRow>
)}
<InputRow>
<FlagsSelect
value={props.value.data.flag || ""}
value={props.value.data.flag && Array.from(props.value.data.flag)}
onChange={(ev) => {
props.onChange({
...props.value,
data: {
...props.value.data,
flag: ev.target.value as string,
flag: ev,
},
});
}}
sx={{ margin: 0 }}
/>
</InputRow>
</div>
);

Expand Down Expand Up @@ -218,7 +215,6 @@ export const Question: React.FC<Props> = (props) => {
text: "",
description: "",
val: "",
flag: "",
},
}) as Option
}
Expand Down
110 changes: 49 additions & 61 deletions editor.planx.uk/src/@planx/components/shared/FlagsSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,70 +1,58 @@
import { fallbackHttpConfig } from "@apollo/client";
import FormControl from "@mui/material/FormControl";
import InputLabel from "@mui/material/InputLabel";
import ListSubheader from "@mui/material/ListSubheader";
import MenuItem from "@mui/material/MenuItem";
import Select, { SelectChangeEvent } from "@mui/material/Select";
import { AutocompleteProps } from "@mui/material/Autocomplete";
import Chip from "@mui/material/Chip";
import ListItem from "@mui/material/ListItem";
import { Flag, flatFlags } from "@opensystemslab/planx-core/types";
import groupBy from "lodash/groupBy";
import React from "react";
import type { Props as SelectInputProps } from "ui/editor/SelectInput";
import SelectInput from "ui/editor/SelectInput";
import { SelectMultiple } from "ui/shared/SelectMultiple";
import InputRow from "ui/shared/InputRow";
import { CustomCheckbox, SelectMultiple } from "ui/shared/SelectMultiple";

const flags = groupBy(flatFlags, (f) => f.category);
interface Props {
value?: string[];
onChange: (values: string[]) => void;
}

const FlagsSelect: React.FC<SelectInputProps> = (props) => {
const initialFlagValues = props?.value ? [props.value as string] : [];
const [flagValue, setFlagValue] = React.useState<string[]>(initialFlagValues);
const renderOptions: AutocompleteProps<
Flag,
true,
true,
false,
"div"
>["renderOption"] = (props, flag, { selected }) => (
<ListItem {...props}>
<CustomCheckbox aria-hidden="true" className={selected ? "selected" : ""} sx={{ backgroundColor: `${flag.bgColor}` }} />
{flag.text}
</ListItem>
);

const handleChange = (event: SelectChangeEvent<typeof flagValue>) => {
const {
target: { value },
} = event;
setFlagValue(
// On autofill we get a stringified value.
typeof value === 'string' ? value.split(',') : value,
);
};

const flagMenuItems = Object.entries(flags).flatMap(
([category, categoryFlags]) => [
<ListSubheader key={category}>
{category}
</ListSubheader>,
categoryFlags.map((flag) => (
<MenuItem
key={flag.value}
value={flag.value}
style={{
borderLeft: `1em solid ${flag.bgColor || "transparent"}`,
fontSize: "1rem",
}}
>
{flag.text}
</MenuItem>
)),
],
);
const renderTags: AutocompleteProps<
Flag,
true,
true,
false,
"div"
>["renderTags"] = (value, getFlagProps) =>
value.map((flag, index) => (
<Chip
{...getFlagProps({ index })}
key={flag.value}
label={flag.text}
sx={{ backgroundColor: flag.bgColor, color: flag.color }}
/>
));

export const FlagsSelect: React.FC<Props> = ({ value, onChange }) => {
return (
<FormControl>
<InputLabel htmlFor="grouped-select">Flags</InputLabel>
<Select
id="grouped-select"
label="Flags"
multiple
value={flagValue}
onChange={handleChange}
// defaultValue=""
>
<MenuItem value="">
{"None"}
</MenuItem>
{flagMenuItems}
</Select>
</FormControl>
<InputRow>
<SelectMultiple
label="Flags"
options={flatFlags}
getOptionLabel={(flag) => flag.text}
groupBy={(flag) => flag.category}
onChange={(_e, value) => onChange(value)}
value={value}
renderOption={renderOptions}
renderTags={renderTags}
/>
</InputRow>
);
};

export default FlagsSelect;
2 changes: 1 addition & 1 deletion editor.planx.uk/src/@planx/components/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface Option {
id: string;
data: {
description?: string;
flag?: string;
flag?: string[];
img?: string;
text: string;
val?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ const Option: React.FC<any> = (props) => {
let color = "#000";

try {
// If there are many flags, only show the color band for the first flag
const flag = flatFlags.find(({ value }) =>
[props.data?.flag, props.data?.val].filter(Boolean).includes(value),
[
// Support both new "Options" that have array of flags and old ones with single flag
Array.isArray(props.data?.flag) ? props.data?.flag?.[0].value : props.data?.flag,
props.data?.val,
].filter(Boolean).includes(value),
);
background = flag?.bgColor || background;
color = flag?.color || color;
Expand Down

0 comments on commit 32da626

Please sign in to comment.