1
- import React , { ChangeEventHandler , forwardRef , InputHTMLAttributes } from 'react'
1
+ import React , { ChangeEventHandler , forwardRef , InputHTMLAttributes , useState } from 'react'
2
2
3
3
import classNames from 'classnames'
4
4
import PropTypes from 'prop-types'
5
5
6
6
import { CFormControlWrapper , CFormControlWrapperProps } from './CFormControlWrapper'
7
+ import { useEffect } from 'react'
7
8
8
9
export interface CFormInputProps
9
10
extends CFormControlWrapperProps ,
@@ -12,6 +13,10 @@ export interface CFormInputProps
12
13
* A string of all className you want applied to the component.
13
14
*/
14
15
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
15
20
/**
16
21
* Toggle the disabled state for the component.
17
22
*/
@@ -49,13 +54,15 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
49
54
{
50
55
children,
51
56
className,
57
+ delay = false ,
52
58
feedback,
53
59
feedbackInvalid,
54
60
feedbackValid,
55
61
floatingLabel,
56
62
id,
57
63
invalid,
58
64
label,
65
+ onChange,
59
66
plainText,
60
67
size,
61
68
text,
@@ -66,6 +73,17 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
66
73
} ,
67
74
ref ,
68
75
) => {
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
+
69
87
const _className = classNames (
70
88
plainText ? 'form-control-plaintext' : 'form-control' ,
71
89
{
@@ -76,6 +94,7 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
76
94
} ,
77
95
className ,
78
96
)
97
+
79
98
return (
80
99
< CFormControlWrapper
81
100
describedby = { rest [ 'aria-describedby' ] }
@@ -90,7 +109,14 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
90
109
tooltipFeedback = { tooltipFeedback }
91
110
valid = { valid }
92
111
>
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
+ >
94
120
{ children }
95
121
</ input >
96
122
</ CFormControlWrapper >
@@ -101,6 +127,7 @@ export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
101
127
CFormInput . propTypes = {
102
128
className : PropTypes . string ,
103
129
id : PropTypes . string ,
130
+ delay : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . number ] ) ,
104
131
plainText : PropTypes . bool ,
105
132
size : PropTypes . oneOf ( [ 'sm' , 'lg' ] ) ,
106
133
type : PropTypes . oneOfType ( [ PropTypes . oneOf ( [ 'color' , 'file' , 'text' ] ) , PropTypes . string ] ) ,
0 commit comments