Skip to content

Commit 311fbc1

Browse files
committed
fix(CFormSelect): add missing options property
1 parent c787e07 commit 311fbc1

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

packages/coreui-react/src/components/form/CFormSelect.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import React, { ChangeEventHandler, forwardRef, InputHTMLAttributes } from 'reac
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44

5+
type Option = {
6+
disabled?: boolean
7+
label?: string
8+
value?: string
9+
}
510
export interface CFormSelectProps extends Omit<InputHTMLAttributes<HTMLSelectElement>, 'size'> {
611
/**
712
* A string of all className you want applied to the component.
@@ -19,6 +24,13 @@ export interface CFormSelectProps extends Omit<InputHTMLAttributes<HTMLSelectEle
1924
* Method called immediately after the `value` prop changes.
2025
*/
2126
onChange?: ChangeEventHandler<HTMLSelectElement>
27+
/**
28+
* Options list of the select component. Available keys: `label`, `value`, `disabled`.
29+
* Examples:
30+
* - `options={[{ value: 'js', label: 'JavaScript' }, { value: 'html', label: 'HTML', disabled: true }]}`
31+
* - `options={['js', 'html']}`
32+
*/
33+
options?: Option[] | string[]
2234
/**
2335
* Size the component small or large.
2436
*/
@@ -36,7 +48,7 @@ export interface CFormSelectProps extends Omit<InputHTMLAttributes<HTMLSelectEle
3648
}
3749

3850
export const CFormSelect = forwardRef<HTMLSelectElement, CFormSelectProps>(
39-
({ children, className, htmlSize, invalid, size, valid, ...rest }, ref) => {
51+
({ children, className, htmlSize, invalid, options, size, valid, ...rest }, ref) => {
4052
const _className = classNames(
4153
'form-select',
4254
{
@@ -48,7 +60,20 @@ export const CFormSelect = forwardRef<HTMLSelectElement, CFormSelectProps>(
4860
)
4961
return (
5062
<select className={_className} size={htmlSize} {...rest} ref={ref}>
51-
{children}
63+
{options
64+
? options.map((option, index) => {
65+
return (
66+
<option
67+
{...(typeof option === 'object' &&
68+
option.disabled && { disabled: option.disabled })}
69+
{...(typeof option === 'object' && option.value && { value: option.value })}
70+
key={index}
71+
>
72+
{typeof option === 'string' ? option : option.label}
73+
</option>
74+
)
75+
})
76+
: children}
5277
</select>
5378
)
5479
},
@@ -59,6 +84,7 @@ CFormSelect.propTypes = {
5984
className: PropTypes.string,
6085
htmlSize: PropTypes.number,
6186
invalid: PropTypes.bool,
87+
options: PropTypes.array,
6288
size: PropTypes.oneOf(['sm', 'lg']),
6389
valid: PropTypes.bool,
6490
}

packages/docs/content/4.0/api/CFormSelect.api.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import CFormSelect from '@coreui/react/src/components/form/CFormSelect'
1111
| **htmlSize** | Specifies the number of visible options in a drop-down list. | `number` | - |
1212
| **invalid** | Set component validation state to invalid. | `boolean` | - |
1313
| **onChange** | Method called immediately after the `value` prop changes. | `ChangeEventHandler<HTMLSelectElement>` | - |
14+
| **options** | Options list of the select component. Available keys: `label`, `value`, `disabled`.<br/>Examples:<br/>- `options={[{ value: 'js', label: 'JavaScript' }, { value: 'html', label: 'HTML', disabled: true }]}`<br/>- `options={['js', 'html']}` | `Option[]` \| `string[]` | - |
1415
| **size** | Size the component small or large. | `'sm'` \| `'lg'` | - |
1516
| **valid** | Set component validation state to valid. | `boolean` | - |
1617
| **value** | The `value` attribute of component.<br/>@controllable onChange | `string` \| `number` \| `string[]` | - |

packages/docs/content/4.0/forms/select.mdx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,34 @@ import {
2020
## Default
2121

2222
<Example>
23-
<CFormSelect aria-label="Default select example">
24-
<option>Open this select menu</option>
25-
<option value="1">One</option>
26-
<option value="2">Two</option>
27-
<option value="3">Three</option>
28-
</CFormSelect>
23+
<CFormSelect
24+
aria-label="Default select example"
25+
options={[
26+
'Open this select menu',
27+
{ label: 'One', value: '1' },
28+
{ label: 'Two', value: '2' },
29+
{ label: 'Three', value: '3', disabled: true }
30+
]}
31+
/>
2932
</Example>
3033

3134
```jsx
35+
<CFormSelect
36+
aria-label="Default select example"
37+
options={[
38+
'Open this select menu',
39+
{ label: 'One', value: '1' },
40+
{ label: 'Two', value: '2' },
41+
{ label: 'Three', value: '3', disabled: true }
42+
]}
43+
/>
44+
45+
// You can also add options manually
3246
<CFormSelect aria-label="Default select example">
3347
<option>Open this select menu</option>
3448
<option value="1">One</option>
3549
<option value="2">Two</option>
36-
<option value="3">Three</option>
50+
<option value="3" disabled>Three</option>
3751
</CFormSelect>
3852
```
3953

0 commit comments

Comments
 (0)