Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow SelectMultiple to have either a label or placeholder prop #3952

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const FlagsSelect: React.FC<Props> = (props) => {
<SelectMultiple
id="select-multiple-flags"
key="select-multiple-flags"
label="Flags (up to one per category)"
placeholder="Flags (up to one per category)"
options={flatFlags}
getOptionLabel={(flag) => flag.text}
groupBy={(flag) => flag.category}
Expand Down
2 changes: 1 addition & 1 deletion editor.planx.uk/src/ui/editor/ListManager/ListManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface Props<T, EditorExtraProps = {}> {

const Item = styled(Box)(({ theme }) => ({
display: "flex",
marginBottom: theme.spacing(1),
marginBottom: theme.spacing(2),
}));

export default function ListManager<T, EditorExtraProps>(
Expand Down
18 changes: 16 additions & 2 deletions editor.planx.uk/src/ui/shared/SelectMultiple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ type OptionalAutocompleteProps<T> = Partial<
Omit<AutocompleteProps<T, true, true, false, "div">, "multiple">
>;

type Props<T> = {
type WithLabel<T> = {
label: string;
placeholder?: never;
} & RequiredAutocompleteProps<T> &
OptionalAutocompleteProps<T>;

type WithPlaceholder<T> = {
label?: never;
placeholder: string;
} & RequiredAutocompleteProps<T> &
OptionalAutocompleteProps<T>;

type Props<T> = WithLabel<T> | WithPlaceholder<T>;

const StyledAutocomplete = styled(Autocomplete)(({ theme }) => ({
marginTop: theme.spacing(2),
"& > div > label": {
paddingRight: theme.spacing(3),
},
Expand Down Expand Up @@ -104,9 +112,14 @@ export const CustomCheckbox = styled("span")(({ theme }) => ({
}));

export function SelectMultiple<T>(props: Props<T>) {
// MUI doesn't pass the Autocomplete value along to the TextField automatically
const isSelectEmpty = !props.value?.length;
const placeholder = isSelectEmpty ? props.placeholder : undefined

return (
<FormControl sx={{ display: "flex", flexDirection: "column" }}>
<StyledAutocomplete<T, true, true, false, "div">
sx={{ mt: props.label ? 2 : 0 }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling this within the styled component is surprisingly complex from a type POV! Managing it here feels like a sensible compromise 👍

role="status"
aria-atomic={true}
aria-live="polite"
Expand All @@ -122,6 +135,7 @@ export function SelectMultiple<T>(props: Props<T>) {
notched: false,
}}
label={props.label}
placeholder={placeholder}
/>
)}
ChipProps={{
Expand Down
Loading