Skip to content

Commit b8c3fd2

Browse files
authored
fix(input): 兼容h5和小程序获取原生input标签 (#3341)
* feat: inputRef获取真实input-dom * feat: 兼容小程序和h5获取input标签 * feat: 取消?问好,出错直接抛出
1 parent 6331fc3 commit b8c3fd2

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/packages/input/input.taro.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,28 @@ export const Input = forwardRef((props: Partial<TaroInputProps>, ref) => {
7777
const inputRef = useRef<HTMLInputElement>(null)
7878
const [active, setActive] = useState(false)
7979

80+
// 兼容H5和小程序获取原生input标签
81+
const getNativeInput = () => {
82+
if (Taro.getEnv() === 'WEB') {
83+
const taroInputCoreEl = inputRef.current as HTMLElement
84+
const inputEl = taroInputCoreEl.querySelector('input')
85+
return inputEl
86+
}
87+
return inputRef.current
88+
}
89+
8090
useImperativeHandle(ref, () => {
8191
return {
8292
clear: () => {
8393
setValue('')
8494
},
8595
focus: () => {
86-
inputRef.current?.focus()
96+
const nativeInput = getNativeInput()
97+
nativeInput?.focus()
8798
},
8899
blur: () => {
89-
inputRef.current?.blur()
100+
const nativeInput = getNativeInput()
101+
nativeInput?.blur()
90102
},
91103
get nativeElement() {
92104
return inputRef.current

0 commit comments

Comments
 (0)