|
| 1 | +# CustomSelectControl |
| 2 | + |
| 3 | +`CustomSelectControl` allows users to select an item from a single-option menu just like [`SelectControl`](/packages/components/src/select-control/readme.md), with the addition of being able to provide custom styles for each item in the menu. This means it does not use a native `<select>`, so should only be used if the custom styling is necessary. |
| 4 | + |
| 5 | +## Table of contents |
| 6 | + |
| 7 | +1. [Design guidelines](#design-guidelines) |
| 8 | +2. [Development guidelines](#development-guidelines) |
| 9 | +3. [Related components](#related-components) |
| 10 | + |
| 11 | +## Design guidelines |
| 12 | + |
| 13 | +These are the same as [the ones for `SelectControl`s](/packages/components/src/select-control/readme.md#design-guidelines). |
| 14 | + |
| 15 | +## Development guidelines |
| 16 | + |
| 17 | +### Usage |
| 18 | + |
| 19 | +```jsx |
| 20 | +/** |
| 21 | + * WordPress dependencies |
| 22 | + */ |
| 23 | +import { CustomSelectControl } from "@wordpress/components"; |
| 24 | +import { useState } from "@wordpress/compose"; |
| 25 | + |
| 26 | +const options = [ |
| 27 | + { |
| 28 | + key: 'small', |
| 29 | + name: 'Small', |
| 30 | + style: { fontSize: '50%' }, |
| 31 | + }, |
| 32 | + { |
| 33 | + key: 'normal', |
| 34 | + name: 'Normal', |
| 35 | + style: { fontSize: '100%' }, |
| 36 | + }, |
| 37 | + { |
| 38 | + key: 'large', |
| 39 | + name: 'Large', |
| 40 | + style: { fontSize: '200%' }, |
| 41 | + }, |
| 42 | + { |
| 43 | + key: 'huge', |
| 44 | + name: 'Huge', |
| 45 | + style: { fontSize: '300%' }, |
| 46 | + }, |
| 47 | +]; |
| 48 | + |
| 49 | +function MyCustomSelectControl() { |
| 50 | + const [ , setFontSize ] = useState(); |
| 51 | + return ( |
| 52 | + <CustomSelectControl |
| 53 | + label="Font Size" |
| 54 | + options={ options } |
| 55 | + onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) } |
| 56 | + /> |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +function MyControlledCustomSelectControl() { |
| 61 | + const [ fontSize, setFontSize ] = useState( options[ 0 ] ); |
| 62 | + return ( |
| 63 | + <CustomSelectControl |
| 64 | + label="Font Size" |
| 65 | + options={ options } |
| 66 | + onChange={ ( { selectedItem } ) => setFontSize( selectedItem ) } |
| 67 | + value={ options.find( ( option ) => option.key === fontSize.key ) } |
| 68 | + /> |
| 69 | + ); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Props |
| 74 | + |
| 75 | +#### className |
| 76 | + |
| 77 | +A custom class name to append to the outer `<div>`. |
| 78 | +- Type: `String` |
| 79 | +- Required: No |
| 80 | + |
| 81 | +#### hideLabelFromVision |
| 82 | + |
| 83 | +Used to visually hide the label. It will always be visible to screen readers. |
| 84 | +- Type: `Boolean` |
| 85 | +- Required: No |
| 86 | + |
| 87 | +#### label |
| 88 | + |
| 89 | +The label for the control. |
| 90 | +- Type: `String` |
| 91 | +- Required: Yes |
| 92 | + |
| 93 | +#### options |
| 94 | + |
| 95 | +The options that can be chosen from. |
| 96 | +- Type: `Array<{ key: String, name: String, style: ?{}, ...rest }>` |
| 97 | +- Required: Yes |
| 98 | + |
| 99 | +#### onChange |
| 100 | + |
| 101 | +Function called with the control's internal state changes. The `selectedItem` property contains the next selected item. |
| 102 | +- Type: `Function` |
| 103 | +- Required: No |
| 104 | + |
| 105 | +#### value |
| 106 | + |
| 107 | +Can be used to externally control the value of the control, like in the `MyControlledCustomSelectControl` example above. |
| 108 | +- Type: `Object` |
| 109 | +- Required: No |
| 110 | + |
| 111 | +## Related components |
| 112 | + |
| 113 | +- Like this component, but implemented using a native `<select>` for when custom styling is not necessary, the `SelectControl` component. |
| 114 | + |
| 115 | +- To select one option from a set, when you want to show all the available options at once, use the `Radio` component. |
| 116 | +- To select one or more items from a set, use the `CheckboxControl` component. |
| 117 | +- To toggle a single setting on or off, use the `ToggleControl` component. |
0 commit comments