From bde5bf10fcc177a7ec4813f22d0244763faf40e8 Mon Sep 17 00:00:00 2001 From: Vivek Macha Date: Tue, 21 Jan 2025 19:52:33 +0000 Subject: [PATCH] ui improved autocomplete using multiple option --- .gitpod.yml | 11 ++ .../material/components/autocomplete/Tags.js | 123 ++++++++++++++++-- .../material/components/autocomplete/Tags.tsx | 105 +++++++++++++-- 3 files changed, 222 insertions(+), 17 deletions(-) create mode 100644 .gitpod.yml diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 00000000000000..50d74aebccb13c --- /dev/null +++ b/.gitpod.yml @@ -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 + + diff --git a/docs/data/material/components/autocomplete/Tags.js b/docs/data/material/components/autocomplete/Tags.js index 05467cceca296b..d51a125a6c987e 100644 --- a/docs/data/material/components/autocomplete/Tags.js +++ b/docs/data/material/components/autocomplete/Tags.js @@ -3,25 +3,74 @@ 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 ( + {/* Standard Autocomplete */} option.title} defaultValue={[top100Films[13]]} + renderTags={(tagValue, getTagProps) => ( + + {tagValue.map((option, index) => ( + + ))} + + )} + slotProps={{ + paper: { + sx: { width: 'auto' }, + }, + }} renderInput={(params) => ( )} /> + + {/* Outlined Autocomplete */} option.title} defaultValue={[top100Films[13]]} filterSelectedOptions + renderTags={(value, getTagProps) => ( +
+ {value.map((option, index) => ( + + ))} +
+ )} renderInput={(params) => ( )} /> + + {/* Free Solo Autocomplete */} option.title)} defaultValue={[top100Films[13].title]} freeSolo - renderTags={(value, getTagProps) => - value.map((option, index) => { - const { key, ...tagProps } = getTagProps({ index }); - return ( - - ); - }) - } + renderTags={(value, getTagProps) => ( +
+ {value.map((option, index) => { + const { key, ...tagProps } = getTagProps({ index }); + return ( + + ); + })} +
+ )} renderInput={(params) => ( )} /> + + {/* Read-Only Autocomplete */} ({ + display: 'flex', + flexWrap: 'wrap', + gap: theme.spacing(0.5), + width: '100%', + '& .MuiChip-root': { + margin: 0, + flexGrow: 0 + } +})); export default function Tags() { return ( @@ -13,15 +26,49 @@ export default function Tags() { options={top100Films} getOptionLabel={(option) => option.title} defaultValue={[top100Films[13]]} + renderTags={(tagValue, getTagProps) => ( + + {tagValue.map((option, index) => ( + + ))} + + )} + slotProps={{ + paper: { + sx: { width: 'auto' } + } + }} renderInput={(params) => ( )} /> + option.title} defaultValue={[top100Films[13]]} filterSelectedOptions + renderTags={(value, getTagProps) => ( +
+ {value.map((option, index) => ( + + ))} +
+ )} renderInput={(params) => ( )} /> + option.title)} defaultValue={[top100Films[13].title]} freeSolo - renderTags={(value: readonly string[], getTagProps) => - value.map((option: string, index: number) => { - const { key, ...tagProps } = getTagProps({ index }); - return ( - - ); - }) - } + renderTags={(value, getTagProps) => ( +
+ {value.map((option, index) => { + const { key, ...tagProps } = getTagProps({ index }); + return ( + + ); + })} +
+ )} renderInput={(params) => ( )} /> - option.title)} @@ -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 },