Skip to content

[material-ui][Autocomplete] Improve Autocomplete with multiple values such that the entire user input will be displayed #45080

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

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
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: pnpm install && pnpm run build
command: pnpm run start


123 changes: 115 additions & 8 deletions docs/data/material/components/autocomplete/Tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,169 @@ import Chip from '@mui/material/Chip';
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';
import Stack from '@mui/material/Stack';
import { styled } from '@mui/material/styles';

// Styled wrapper for tags
const StyledTagsContainer = styled('div')(({ theme }) => ({
display: 'flex',
flexWrap: 'wrap',
gap: theme.spacing(0.5),
width: '100%',
'& .MuiChip-root': {
margin: 0,
flexGrow: 0,
},
}));

export default function Tags() {
return (
<Stack spacing={3} sx={{ width: 500 }}>
{/* Standard Autocomplete */}
<Autocomplete
multiple
id="tags-standard"
options={top100Films}
getOptionLabel={(option) => option.title}
defaultValue={[top100Films[13]]}
renderTags={(tagValue, getTagProps) => (
<StyledTagsContainer>
{tagValue.map((option, index) => (
<Chip
{...getTagProps({ index })}
key={index}
label={option.title}
size="small"
sx={{
maxWidth: 'calc(100% - 8px)',
'.MuiChip-label': {
whiteSpace: 'normal',
},
}}
/>
))}
</StyledTagsContainer>
)}
slotProps={{
paper: {
sx: { width: 'auto' },
},
}}
renderInput={(params) => (
<TextField
{...params}
variant="standard"
label="Multiple values"
placeholder="Favorites"
multiline
sx={{
'& .MuiInputBase-root': {
flexWrap: 'wrap',
gap: 0.5,
'& .MuiInputBase-input': {
width: '100%',
},
},
}}
/>
)}
/>

{/* Outlined Autocomplete */}
<Autocomplete
multiple
id="tags-outlined"
options={top100Films}
getOptionLabel={(option) => option.title}
defaultValue={[top100Films[13]]}
filterSelectedOptions
renderTags={(value, getTagProps) => (
<div
style={{
display: 'flex',
flexWrap: 'wrap',
alignItems: 'flex-start',
gap: '4px',
width: '100%',
}}
>
{value.map((option, index) => (
<Chip {...getTagProps({ index })} key={index} label={option.title} />
))}
</div>
)}
renderInput={(params) => (
<TextField
{...params}
label="filterSelectedOptions"
placeholder="Favorites"
multiline
sx={{
'& .MuiInputBase-root': {
display: 'flex',
flexDirection: 'column',
padding: '8px',
'& .MuiInputBase-input': {
order: 2,
width: '100%',
padding: '0px',
marginTop: '8px',
},
},
}}
/>
)}
/>

{/* Free Solo Autocomplete */}
<Autocomplete
multiple
id="tags-filled"
options={top100Films.map((option) => option.title)}
defaultValue={[top100Films[13].title]}
freeSolo
renderTags={(value, getTagProps) =>
value.map((option, index) => {
const { key, ...tagProps } = getTagProps({ index });
return (
<Chip variant="outlined" label={option} key={key} {...tagProps} />
);
})
}
renderTags={(value, getTagProps) => (
<div
style={{
display: 'flex',
flexWrap: 'wrap',
gap: '4px',
alignItems: 'flex-start',
width: '100%',
}}
>
{value.map((option, index) => {
const { key, ...tagProps } = getTagProps({ index });
return (
<Chip variant="outlined" label={option} key={key} {...tagProps} />
);
})}
</div>
)}
renderInput={(params) => (
<TextField
{...params}
variant="filled"
label="freeSolo"
placeholder="Favorites"
multiline
sx={{
'& .MuiInputBase-root': {
display: 'flex',
flexDirection: 'column',
padding: '8px',
'& .MuiInputBase-input': {
order: 2,
width: '100%',
padding: '0px',
marginTop: '8px',
},
},
}}
/>
)}
/>

{/* Read-Only Autocomplete */}
<Autocomplete
multiple
id="tags-readOnly"
Expand All @@ -74,6 +180,7 @@ export default function Tags() {
);
}


// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
Expand Down
105 changes: 96 additions & 9 deletions docs/data/material/components/autocomplete/Tags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import Chip from '@mui/material/Chip';
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';
import Stack from '@mui/material/Stack';
import { styled } from '@mui/material/styles';

// Styled wrapper for tags
const StyledTagsContainer = styled('div')(({ theme }) => ({
display: 'flex',
flexWrap: 'wrap',
gap: theme.spacing(0.5),
width: '100%',
'& .MuiChip-root': {
margin: 0,
flexGrow: 0
}
}));

export default function Tags() {
return (
Expand All @@ -13,54 +26,127 @@ export default function Tags() {
options={top100Films}
getOptionLabel={(option) => option.title}
defaultValue={[top100Films[13]]}
renderTags={(tagValue, getTagProps) => (
<StyledTagsContainer>
{tagValue.map((option, index) => (
<Chip
{...getTagProps({ index })}
key={index}
label={option.title}
size="small"
sx={{
maxWidth: 'calc(100% - 8px)',
'.MuiChip-label': {
whiteSpace: 'normal'
}
}}
/>
))}
</StyledTagsContainer>
)}
slotProps={{
paper: {
sx: { width: 'auto' }
}
}}
renderInput={(params) => (
<TextField
{...params}
variant="standard"
label="Multiple values"
placeholder="Favorites"
multiline
sx={{
'& .MuiInputBase-root': {
flexWrap: 'wrap',
gap: 0.5,
'& .MuiInputBase-input': {
width: '100%'
}
}
}}
/>
)}
/>

<Autocomplete
multiple
id="tags-outlined"
options={top100Films}
getOptionLabel={(option) => option.title}
defaultValue={[top100Films[13]]}
filterSelectedOptions
renderTags={(value, getTagProps) => (
<div style={{ display: 'flex', flexWrap: 'wrap',alignItems: 'flex-start', gap: '4px', width: '100%' }}>
{value.map((option, index) => (
<Chip {...getTagProps({ index })} key={index} label={option.title} />
))}
</div>
)}
renderInput={(params) => (
<TextField
{...params}
label="filterSelectedOptions"
placeholder="Favorites"
multiline
sx={{
'& .MuiInputBase-root': {
display: 'flex',
flexDirection: 'column',
padding: '8px',
'& .MuiInputBase-input': {
order: 2,
width: '100%',
padding: '0px',
marginTop: '8px'
}
}
}}
/>
)}
/>

<Autocomplete
multiple
id="tags-filled"
options={top100Films.map((option) => option.title)}
defaultValue={[top100Films[13].title]}
freeSolo
renderTags={(value: readonly string[], getTagProps) =>
value.map((option: string, index: number) => {
const { key, ...tagProps } = getTagProps({ index });
return (
<Chip variant="outlined" label={option} key={key} {...tagProps} />
);
})
}
renderTags={(value, getTagProps) => (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '4px',alignItems: 'flex-start',width: '100%' }}>
{value.map((option, index) => {
const { key, ...tagProps } = getTagProps({ index });
return (
<Chip variant="outlined" label={option} key={key} {...tagProps} />
);
})}
</div>
)}
renderInput={(params) => (
<TextField
{...params}
variant="filled"
label="freeSolo"
placeholder="Favorites"
multiline
sx={{
'& .MuiInputBase-root': {
display: 'flex',
flexDirection: 'column',
padding: '8px',
'& .MuiInputBase-input': {
order: 2,
width: '100%',
padding: '0px',
marginTop: '8px'
}
}
}}
/>
)}
/>
<Autocomplete

<Autocomplete
multiple
id="tags-readOnly"
options={top100Films.map((option) => option.title)}
Expand All @@ -74,6 +160,7 @@ export default function Tags() {
);
}


// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
Expand Down
Loading