@@ -2,6 +2,11 @@ import React, { ChangeEventHandler, forwardRef, InputHTMLAttributes } from 'reac
22import PropTypes from 'prop-types'
33import classNames from 'classnames'
44
5+ type Option = {
6+ disabled ?: boolean
7+ label ?: string
8+ value ?: string
9+ }
510export 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
3850export 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}
0 commit comments