11/* eslint-disable @typescript-eslint/no-empty-function */
2- import React from 'react'
2+ import React , { useEffect , useMemo , useState } from 'react'
33import cx from 'classnames'
44import { View , Input } from '@tarojs/components'
55import {
@@ -24,6 +24,8 @@ export interface HuiInputProps extends ViewProps {
2424 placeholder ?: string
2525 /** 右侧箭头 */
2626 arrow ?: boolean
27+ /** 清除按钮 */
28+ clearable ?: boolean
2729 // withArray?: boolean
2830 /** 报错信息 */
2931 errorMsg ?: React . ReactNode
@@ -48,6 +50,8 @@ export interface HuiInputProps extends ViewProps {
4850 className ?: string
4951 /** 点击事件 */
5052 onClick ?: ( event : ITouchEvent ) => void
53+ /** 清除事件 */
54+ onClear ?: CommonEventFunction < InputProps . inputForceEventDetail >
5155 /** onInput事件 */
5256 onInput ?: CommonEventFunction < InputProps . inputEventDetail >
5357 /** 输入框失获取焦点时触发 */
@@ -74,11 +78,24 @@ const HuiInput: React.FC<HuiInputProps> = (props) => {
7478 className = '' ,
7579 style,
7680 onClick,
81+ onInput = ( ) => { } ,
7782 align = 'left' ,
7883 required = true ,
7984 labelIcon,
85+ clearable,
8086 } = props
8187
88+ const [ innerValue , setInnerValue ] = useState ( value )
89+
90+ useEffect ( ( ) => {
91+ setInnerValue ( value )
92+ } , [ value ] )
93+
94+ const mergedOnInput = ( e ) => {
95+ setInnerValue ( e . detail . value )
96+ onInput ( e )
97+ }
98+
8299 const labelDom = label ? (
83100 < View className = 'label' >
84101 < View > { label } </ View >
@@ -100,15 +117,15 @@ const HuiInput: React.FC<HuiInputProps> = (props) => {
100117 < View
101118 className = { cx (
102119 'display-area' ,
103- { 'none-value' : ! value } ,
120+ { 'none-value' : ! innerValue } ,
104121 { 'right-align' : label && align === 'right' } ,
105122 ) }
106123 >
107- { value || placeholder }
124+ { innerValue || placeholder }
108125 </ View >
109126 ) : (
110127 < Input
111- value = { value }
128+ value = { innerValue }
112129 className = { cx ( 'input' , { 'right-align' : label && align === 'right' } ) }
113130 type = { type }
114131 placeholder = { placeholder }
@@ -118,7 +135,7 @@ const HuiInput: React.FC<HuiInputProps> = (props) => {
118135 adjustPosition = { props . adjustPosition }
119136 confirmType = { props . confirmType || 'done' }
120137 maxlength = { props . maxLength || 140 }
121- onInput = { props . onInput || ( ( ) => { } ) }
138+ onInput = { mergedOnInput }
122139 onBlur = { props . onBlur || ( ( ) => { } ) }
123140 onFocus = { props . onFocus || ( ( ) => { } ) }
124141 onConfirm = { props . onConfirm || ( ( ) => { } ) }
@@ -136,6 +153,30 @@ const HuiInput: React.FC<HuiInputProps> = (props) => {
136153 />
137154 ) : null
138155
156+ const showClear = useMemo (
157+ ( ) => clearable && ! ! innerValue ,
158+ [ clearable , innerValue ] ,
159+ )
160+
161+ // input右侧清除按钮是否展示
162+ const inputClearDom = showClear ? (
163+ < View
164+ style = { { marginLeft : '10px' } }
165+ onClick = { ( e ) => {
166+ e . preventDefault ( )
167+ mergedOnInput ( { detail : { value : '' } } )
168+ props . onClear ?.( e )
169+ } }
170+ >
171+ < HuiIcon
172+ name = '005-close2'
173+ style = { {
174+ color : '#c5c5c5' ,
175+ } }
176+ />
177+ </ View >
178+ ) : null
179+
139180 // 分割线是否展示 & 分割线颜色
140181 const divideLineDom =
141182 errorMsg || divider ? (
@@ -169,6 +210,7 @@ const HuiInput: React.FC<HuiInputProps> = (props) => {
169210 { labelDom }
170211 < View className = 'input-content' >
171212 { inputDom }
213+ { inputClearDom }
172214 { props . unit ? < View className = 'unit' > { props . unit } </ View > : null }
173215 </ View >
174216 { inputArrayDom }
0 commit comments