From 8c6c5afa188e2f0b96cf2795a79553af16efc2ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dudak?= Date: Thu, 4 May 2023 18:40:11 -0700 Subject: [PATCH 1/5] [docs] Improve the Select docs --- docs/data/base/components/select/select.md | 74 +++++++++++++--------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/docs/data/base/components/select/select.md b/docs/data/base/components/select/select.md index 0c550540352b71..af0ff2532edf0b 100644 --- a/docs/data/base/components/select/select.md +++ b/docs/data/base/components/select/select.md @@ -43,8 +43,9 @@ import Option from '@mui/base/Option'; export default function MyApp() { return ( ); } @@ -70,7 +71,7 @@ See the [Object values](#object-values) section to learn how to do it. #### TypeScript caveat -Select accepts generic props. +Select's props are generic. Due to TypeScript limitations, this may cause unexpected behavior when wrapping the component in `forwardRef` (or other higher-order components). In such cases, the generic argument will be defaulted to `unknown` and type suggestions will be incomplete. @@ -93,10 +94,51 @@ For the sake of brevity, the rest of the demos throughout this doc will not use ### Multi-select The Select component lets your users select multiple options from the list. +In constrast to the single-selection mode, the options popup does not close after an item is selected, allowing users to continue choosing mor e options. + Set the `multiple` prop to turn on the multi-selection mode. {{"demo": "UnstyledSelectMultiple.js", "defaultCodeOpen": false}} +Note that in the multiple selection mode, the `value` prop (and `defaultValue`) is an array. + +### Controlled select + +Select can be used as an uncontrolled or controlled component: + +{{"demo": "UnstyledSelectControlled.js", "defaultCodeOpen": false}} + +To set the value of the controlled Select, use the `value` prop. +In the other case the `defaultValue` can be used to set the initial value of the component. +In any case, if you wish to deselect all values pass `null` to the respective prop. + +:::warning +This is one of the differences with Material UI's Select API. +There, an empty string was used to deselect all values. +In Base UI, use `null` to achieve this. +::: + +### Object values + +The Select component can be used with non-string values: + +{{"demo": "UnstyledSelectObjectValues.js", "defaultCodeOpen": false}} + +If you use a Select with object values in a form and post the form contents to a server, the selected value will be converted to JSON. +You can change this behavior with the help of the `getSerializedValue` prop. + +{{"demo": "UnstyledSelectObjectValuesForm.js", "defaultCodeOpen": false}} + +### Grouping options + +Options can be grouped, similarly to how the native ``, groups can be nested. + +The following demo shows how to group options with the Option Group component: + +{{"demo": "UnstyledSelectGrouping.js", "defaultCodeOpen": false}} + + ### Anatomy The Select component is composed of a root `