-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[joy-ui][Select] Support selection of multiple
options
#39454
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
3b56f58
add mutiple in Props.ts
sai6855 d653462
add mutiple in select.tsx
sai6855 7723f22
fix types
sai6855 6a7961b
fix types
sai6855 b212f08
replace TValue to OptionValue
sai6855 04a8a1c
fix types
sai6855 73e2427
add multiple class
sai6855 a5799b3
fix types
sai6855 db9b321
Merge branch 'master' into select-multiple-joy
sai6855 d48d931
fix build
sai6855 37b5f89
add demo
sai6855 1dba21a
ran static scripts
sai6855 b6ffd08
docs:api
sai6855 15dd0ee
Merge branch 'master' into select-multiple-joy
sai6855 ca191f6
fix types
sai6855 3b63d41
add ts tests
sai6855 da543e5
add ts tests
sai6855 e17201b
change name
sai6855 12067ac
fix tests
sai6855 e99db84
add more ts tests
sai6855 eee7558
add more ts tests
sai6855 6398e3e
proptypes
sai6855 a0bd5e4
add-demo
sai6855 41c2327
add ts tests
sai6855 0110e9e
add ts tests
sai6855 1a5f3f2
update demo color
sai6855 ddac565
add form submission demo
sai6855 f54b1ab
convert form data from string to array
sai6855 5ba3592
remove null,2
sai6855 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,27 @@ const App = () => ( | |
); | ||
``` | ||
|
||
### Multiple selections | ||
|
||
Set the `multiple` prop to let your users select multiple options from the list. | ||
In contrast with single-selection mode, the options popup doesn't close after an item is selected, which enables users to continue choosing more options. | ||
|
||
Note that in multiple selection mode, the `value` prop (and `defaultValue`) is an array. | ||
|
||
{{"demo": "SelectMultiple.js"}} | ||
|
||
#### Selected value appearance | ||
|
||
Use the `renderValue` prop to customize the display of the selected options. | ||
|
||
{{"demo": "SelectMultipleAppearance.js"}} | ||
|
||
#### Form submission | ||
|
||
The `Select` component supports `name` and `required` props that will be used when submitting the form. | ||
|
||
{{"demo": "SelectMultipleFormSubmission.js"}} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to add a couple of more demos: #### Selected value appearance
Display avatars of selected users end with "x friends".
#### Form submission
Show what the value looks like after submitted a form. |
||
### Listbox | ||
|
||
#### Maximum height | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is strange that
formJson.pets
is a string, not an array. Is this expected?I got
"[\"dog\"\"
instead of["dog"]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you console.logOn loggingformJson.pets
it indeed printsarray
, so issue you described maybe have to do something withalert
typeof formJson.pets
i'm getting type asstring
. Ideally It should beobject
, let me checkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is expected because if
Select
hasmultiple
prop thenvalue
ofhidden input
is stringified. so users need to doJSON.parse
to convert it toarray
.material-ui/packages/mui-base/src/useSelect/useSelect.ts
Lines 51 to 57 in a731e86
So I've modified demo and parsed
string
toarray
. Now demo is looking goodThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.