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: Hide label option for select multiple #3927

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions editor.planx.uk/src/@planx/components/shared/FlagsSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export const FlagsSelect: React.FC<Props> = (props) => {
<SelectMultiple
id="select-multiple-flags"
key="select-multiple-flags"
hideLabel
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
53 changes: 38 additions & 15 deletions editor.planx.uk/src/ui/shared/SelectMultiple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { inputLabelClasses } from "@mui/material/InputLabel";
import { outlinedInputClasses } from "@mui/material/OutlinedInput";
import { styled } from "@mui/material/styles";
import TextField from "@mui/material/TextField";
import { visuallyHidden } from "@mui/utils";
import React from "react";
import { borderedFocusStyle } from "theme";

Expand All @@ -29,23 +30,32 @@ type OptionalAutocompleteProps<T> = Partial<

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

const StyledAutocomplete = styled(Autocomplete)(({ theme }) => ({
marginTop: theme.spacing(2),
"& > div > label": {
paddingRight: theme.spacing(3),
},
[`& .${autocompleteClasses.endAdornment}`]: {
top: "unset",
},
"&:focus-within": {
"& svg": {
color: "black",
interface CustomAutocompleteProps<T>
extends AutocompleteProps<T, true, true, false, "div"> {
hideLabel?: boolean;
}

const StyledAutocomplete = styled(Autocomplete)<CustomAutocompleteProps<any>>(
({ theme, hideLabel }) => ({
...(hideLabel ? {} : { marginTop: theme.spacing(2) }),
"& > div > label": {
paddingRight: theme.spacing(3),
},
},
})) as typeof Autocomplete;
[`& .${autocompleteClasses.endAdornment}`]: {
top: "unset",
},
"&:focus-within": {
"& svg": {
color: "black",
},
},
}),
) as typeof Autocomplete;

const StyledTextField = styled(TextField)(({ theme }) => ({
"&:focus-within": {
Expand Down Expand Up @@ -103,7 +113,13 @@ export const CustomCheckbox = styled("span")(({ theme }) => ({
},
}));

export function SelectMultiple<T>(props: Props<T>) {
export function SelectMultiple<T>({
label,
hideLabel = false,
placeholder,
value = [],
...props
}: Props<T> & { value?: T[] }) {
return (
<FormControl sx={{ display: "flex", flexDirection: "column" }}>
<StyledAutocomplete<T, true, true, false, "div">
Expand All @@ -114,14 +130,21 @@ export function SelectMultiple<T>(props: Props<T>) {
disableCloseOnSelect
multiple
popupIcon={PopupIcon}
value={value}
hideLabel={hideLabel}
renderInput={(params) => (
<StyledTextField
{...params}
InputProps={{
...params.InputProps,
notched: false,
placeholder: value.length === 0 ? placeholder : "",
}}
label={hideLabel ? "" : label}
InputLabelProps={{
...params.InputLabelProps,
sx: hideLabel ? visuallyHidden : {},
}}
label={props.label}
/>
)}
ChipProps={{
Expand Down
Loading