@@ -2,6 +2,11 @@ import React, { ChangeEventHandler, forwardRef, InputHTMLAttributes } from 'reac
2
2
import PropTypes from 'prop-types'
3
3
import classNames from 'classnames'
4
4
5
+ type Option = {
6
+ disabled ?: boolean
7
+ label ?: string
8
+ value ?: string
9
+ }
5
10
export interface CFormSelectProps extends Omit < InputHTMLAttributes < HTMLSelectElement > , 'size' > {
6
11
/**
7
12
* A string of all className you want applied to the component.
@@ -19,6 +24,13 @@ export interface CFormSelectProps extends Omit<InputHTMLAttributes<HTMLSelectEle
19
24
* Method called immediately after the `value` prop changes.
20
25
*/
21
26
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 [ ]
22
34
/**
23
35
* Size the component small or large.
24
36
*/
@@ -36,7 +48,7 @@ export interface CFormSelectProps extends Omit<InputHTMLAttributes<HTMLSelectEle
36
48
}
37
49
38
50
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 ) => {
40
52
const _className = classNames (
41
53
'form-select' ,
42
54
{
@@ -48,7 +60,20 @@ export const CFormSelect = forwardRef<HTMLSelectElement, CFormSelectProps>(
48
60
)
49
61
return (
50
62
< 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 }
52
77
</ select >
53
78
)
54
79
} ,
@@ -59,6 +84,7 @@ CFormSelect.propTypes = {
59
84
className : PropTypes . string ,
60
85
htmlSize : PropTypes . number ,
61
86
invalid : PropTypes . bool ,
87
+ options : PropTypes . array ,
62
88
size : PropTypes . oneOf ( [ 'sm' , 'lg' ] ) ,
63
89
valid : PropTypes . bool ,
64
90
}
0 commit comments