1- import React , { ChangeEventHandler , forwardRef , InputHTMLAttributes } from 'react'
1+ import React , { ChangeEventHandler , forwardRef , InputHTMLAttributes , useState } from 'react'
22
33import classNames from 'classnames'
44import PropTypes from 'prop-types'
55
66import { CFormControlWrapper , CFormControlWrapperProps } from './CFormControlWrapper'
7+ import { useEffect } from 'react'
78
89export interface CFormInputProps
910 extends CFormControlWrapperProps ,
@@ -12,6 +13,10 @@ export interface CFormInputProps
1213 * A string of all className you want applied to the component.
1314 */
1415 className ?: string
16+ /**
17+ * Delay onChange event while typing. If set to true onChange event will be delayed 500ms, you can also provide the number of milliseconds you want to delay the onChange event.
18+ */
19+ delay ?: boolean | number
1520 /**
1621 * Toggle the disabled state for the component.
1722 */
@@ -49,13 +54,15 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
4954 {
5055 children,
5156 className,
57+ delay = false ,
5258 feedback,
5359 feedbackInvalid,
5460 feedbackValid,
5561 floatingLabel,
5662 id,
5763 invalid,
5864 label,
65+ onChange,
5966 plainText,
6067 size,
6168 text,
@@ -66,6 +73,17 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
6673 } ,
6774 ref ,
6875 ) => {
76+ const [ value , setValue ] = useState < React . ChangeEvent < HTMLInputElement > > ( )
77+
78+ useEffect ( ( ) => {
79+ const timeOutId = setTimeout (
80+ ( ) => value && onChange && onChange ( value ) ,
81+ typeof delay === 'number' ? delay : 500 ,
82+ )
83+
84+ return ( ) => clearTimeout ( timeOutId )
85+ } , [ value ] )
86+
6987 const _className = classNames (
7088 plainText ? 'form-control-plaintext' : 'form-control' ,
7189 {
@@ -76,6 +94,7 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
7694 } ,
7795 className ,
7896 )
97+
7998 return (
8099 < CFormControlWrapper
81100 describedby = { rest [ 'aria-describedby' ] }
@@ -90,7 +109,14 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
90109 tooltipFeedback = { tooltipFeedback }
91110 valid = { valid }
92111 >
93- < input className = { _className } id = { id } type = { type } { ...rest } ref = { ref } >
112+ < input
113+ className = { _className }
114+ id = { id }
115+ type = { type }
116+ onChange = { ( event ) => ( delay ? setValue ( event ) : onChange && onChange ( event ) ) }
117+ { ...rest }
118+ ref = { ref }
119+ >
94120 { children }
95121 </ input >
96122 </ CFormControlWrapper >
@@ -101,6 +127,7 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
101127CFormInput . propTypes = {
102128 className : PropTypes . string ,
103129 id : PropTypes . string ,
130+ delay : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . number ] ) ,
104131 plainText : PropTypes . bool ,
105132 size : PropTypes . oneOf ( [ 'sm' , 'lg' ] ) ,
106133 type : PropTypes . oneOfType ( [ PropTypes . oneOf ( [ 'color' , 'file' , 'text' ] ) , PropTypes . string ] ) ,
0 commit comments