-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[joy-ui][Select] Support selection of
multiple
options (#39454)
- Loading branch information
Showing
16 changed files
with
463 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as React from 'react'; | ||
import Select from '@mui/joy/Select'; | ||
import Option from '@mui/joy/Option'; | ||
|
||
export default function SelectMultiple() { | ||
const handleChange = (event, newValue) => { | ||
console.log(`You have choosen "${newValue}"`); | ||
}; | ||
return ( | ||
<Select | ||
defaultValue={['dog']} | ||
multiple | ||
onChange={handleChange} | ||
sx={{ | ||
minWidth: '13rem', | ||
}} | ||
slotProps={{ | ||
listbox: { | ||
sx: { | ||
width: '100%', | ||
}, | ||
}, | ||
}} | ||
> | ||
<Option value="dog">Dog</Option> | ||
<Option value="cat">Cat</Option> | ||
<Option value="fish">Fish</Option> | ||
<Option value="bird">Bird</Option> | ||
</Select> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as React from 'react'; | ||
import Select from '@mui/joy/Select'; | ||
import Option from '@mui/joy/Option'; | ||
|
||
export default function SelectMultiple() { | ||
const handleChange = ( | ||
event: React.SyntheticEvent | null, | ||
newValue: Array<string> | null, | ||
) => { | ||
console.log(`You have choosen "${newValue}"`); | ||
}; | ||
return ( | ||
<Select | ||
defaultValue={['dog']} | ||
multiple | ||
onChange={handleChange} | ||
sx={{ | ||
minWidth: '13rem', | ||
}} | ||
slotProps={{ | ||
listbox: { | ||
sx: { | ||
width: '100%', | ||
}, | ||
}, | ||
}} | ||
> | ||
<Option value="dog">Dog</Option> | ||
<Option value="cat">Cat</Option> | ||
<Option value="fish">Fish</Option> | ||
<Option value="bird">Bird</Option> | ||
</Select> | ||
); | ||
} |
37 changes: 37 additions & 0 deletions
37
docs/data/joy/components/select/SelectMultipleAppearance.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as React from 'react'; | ||
import Select from '@mui/joy/Select'; | ||
import Option from '@mui/joy/Option'; | ||
import { Box, Chip } from '@mui/joy'; | ||
|
||
export default function SelectMultipleAppearance() { | ||
return ( | ||
<Select | ||
multiple | ||
defaultValue={['dog', 'cat']} | ||
renderValue={(selected) => ( | ||
<Box sx={{ display: 'flex', gap: '0.25rem' }}> | ||
{selected.map((selectedOption) => ( | ||
<Chip variant="soft" color="primary"> | ||
{selectedOption.label} | ||
</Chip> | ||
))} | ||
</Box> | ||
)} | ||
sx={{ | ||
minWidth: '15rem', | ||
}} | ||
slotProps={{ | ||
listbox: { | ||
sx: { | ||
width: '100%', | ||
}, | ||
}, | ||
}} | ||
> | ||
<Option value="dog">Dog</Option> | ||
<Option value="cat">Cat</Option> | ||
<Option value="fish">Fish</Option> | ||
<Option value="bird">Bird</Option> | ||
</Select> | ||
); | ||
} |
37 changes: 37 additions & 0 deletions
37
docs/data/joy/components/select/SelectMultipleAppearance.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as React from 'react'; | ||
import Select from '@mui/joy/Select'; | ||
import Option from '@mui/joy/Option'; | ||
import { Box, Chip } from '@mui/joy'; | ||
|
||
export default function SelectMultipleAppearance() { | ||
return ( | ||
<Select | ||
multiple | ||
defaultValue={['dog', 'cat']} | ||
renderValue={(selected) => ( | ||
<Box sx={{ display: 'flex', gap: '0.25rem' }}> | ||
{selected.map((selectedOption) => ( | ||
<Chip variant="soft" color="primary"> | ||
{selectedOption.label} | ||
</Chip> | ||
))} | ||
</Box> | ||
)} | ||
sx={{ | ||
minWidth: '15rem', | ||
}} | ||
slotProps={{ | ||
listbox: { | ||
sx: { | ||
width: '100%', | ||
}, | ||
}, | ||
}} | ||
> | ||
<Option value="dog">Dog</Option> | ||
<Option value="cat">Cat</Option> | ||
<Option value="fish">Fish</Option> | ||
<Option value="bird">Bird</Option> | ||
</Select> | ||
); | ||
} |
36 changes: 36 additions & 0 deletions
36
docs/data/joy/components/select/SelectMultipleFormSubmission.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/joy/Button'; | ||
import Select from '@mui/joy/Select'; | ||
import Option from '@mui/joy/Option'; | ||
import Stack from '@mui/joy/Stack'; | ||
|
||
export default function SelectMultipleFormSubmission() { | ||
return ( | ||
<form | ||
onSubmit={(event) => { | ||
event.preventDefault(); | ||
const formData = new FormData(event.currentTarget); | ||
const formJson = Object.fromEntries(formData.entries()); | ||
const selectedPets = JSON.parse(formJson.pets); | ||
alert(JSON.stringify(selectedPets)); | ||
}} | ||
> | ||
<Stack spacing={2} alignItems="flex-start"> | ||
<Select | ||
placeholder="Select a pet" | ||
name="pets" | ||
required | ||
multiple | ||
defaultValue={['dog', 'cat']} | ||
sx={{ minWidth: 200 }} | ||
> | ||
<Option value="dog">Dog</Option> | ||
<Option value="cat">Cat</Option> | ||
<Option value="fish">Fish</Option> | ||
<Option value="bird">Bird</Option> | ||
</Select> | ||
<Button type="submit">Submit</Button> | ||
</Stack> | ||
</form> | ||
); | ||
} |
36 changes: 36 additions & 0 deletions
36
docs/data/joy/components/select/SelectMultipleFormSubmission.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as React from 'react'; | ||
import Button from '@mui/joy/Button'; | ||
import Select from '@mui/joy/Select'; | ||
import Option from '@mui/joy/Option'; | ||
import Stack from '@mui/joy/Stack'; | ||
|
||
export default function SelectMultipleFormSubmission() { | ||
return ( | ||
<form | ||
onSubmit={(event) => { | ||
event.preventDefault(); | ||
const formData = new FormData(event.currentTarget); | ||
const formJson = Object.fromEntries((formData as any).entries()); | ||
const selectedPets = JSON.parse(formJson.pets); | ||
alert(JSON.stringify(selectedPets)); | ||
}} | ||
> | ||
<Stack spacing={2} alignItems="flex-start"> | ||
<Select | ||
placeholder="Select a pet" | ||
name="pets" | ||
required | ||
multiple | ||
defaultValue={['dog', 'cat']} | ||
sx={{ minWidth: 200 }} | ||
> | ||
<Option value="dog">Dog</Option> | ||
<Option value="cat">Cat</Option> | ||
<Option value="fish">Fish</Option> | ||
<Option value="bird">Bird</Option> | ||
</Select> | ||
<Button type="submit">Submit</Button> | ||
</Stack> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.