Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(input): controlled mode, content clearing does not work #1912

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions src/packages/input/__test__/__snapshots__/input.spec.tsx.snap

This file was deleted.

42 changes: 40 additions & 2 deletions src/packages/input/__test__/input.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import '@testing-library/jest-dom'
import Input from '@/packages/input'

test('input props test', () => {
const { container } = render(
const blur = jest.fn()
const { container, rerender } = render(
<Input name="text" placeholder="请输入文字" defaultValue="初始文本" />
)
expect(container.querySelector('.nut-input-native')).toHaveAttribute(
Expand All @@ -23,7 +24,44 @@ test('input props test', () => {
'type',
'text'
)
expect(container).toMatchSnapshot()
rerender(
<Input type="number" placeholder="1" defaultValue="1" onBlur={blur} />
)
expect(container.querySelector('.nut-input-native')).toHaveAttribute(
'type',
'tel'
)
const telInput = container.querySelector('.nut-input-native') as HTMLElement
fireEvent.focus(telInput)
fireEvent.blur(telInput)
expect(blur).toBeCalled()

rerender(
<Input type="digit" placeholder="1" defaultValue="1.01" onBlur={blur} />
)
expect(container.querySelector('.nut-input-native')).toHaveAttribute(
'type',
'text'
)
const digitInput = container.querySelector('.nut-input-native') as HTMLElement
fireEvent.focus(digitInput)
fireEvent.blur(digitInput)
expect(blur).toBeCalled()
rerender(
<Input
name="text"
formatTrigger="onBlur"
placeholder="1"
defaultValue="1"
onBlur={blur}
/>
)
const triggerInput = container.querySelector(
'.nut-input-native'
) as HTMLElement
fireEvent.focus(triggerInput)
fireEvent.blur(triggerInput)
expect(blur).toBeCalled()
})

test('password test', () => {
Expand Down
28 changes: 26 additions & 2 deletions src/packages/input/demo.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from 'react'
import Taro from '@tarojs/taro'
import { Tips, Close, Eye, Marshalling } from '@nutui/icons-react-taro'
import { useTranslate } from '@/sites/assets/locale/taro'
import { Input, Button } from '@/packages/nutui.react.taro'
import { Input, Button, Cell } from '@/packages/nutui.react.taro'
import Header from '@/sites/components/header'

const InputDemo = () => {
Expand All @@ -28,6 +28,8 @@ const InputDemo = () => {
readOnly: '输入框只读',
disabled: '输入框禁用',
clear: '显示清除图标',
clearControlled: '受控下的清除',
clearButton: '点我清除',
codeplaceholder: '请输入短信验证码',
sendCode: '获取验证码',
border: '边框',
Expand Down Expand Up @@ -61,6 +63,8 @@ const InputDemo = () => {
readOnly: 'Input box is read-only',
disabled: 'Input box disabled',
clear: 'Show clear icon',
clearControlled: 'Clearing in Controlled Mode',
clearButton: 'Click to Clear',
codeplaceholder: 'Please enter the SMS verification code',
sendCode: 'Get code',
border: 'border',
Expand All @@ -78,7 +82,7 @@ const InputDemo = () => {
const [val, setVal] = useState('NutUI React')
const [inputType, setInputType] = useState('password')
const [currentLength, setCurrentLength] = useState(0)

const [keyword, setKeyword] = useState('')
return (
<>
<Header />
Expand Down Expand Up @@ -117,6 +121,26 @@ const InputDemo = () => {
clearIcon={<Close size={14} />}
placeholder={translated.clear}
/>
<h2>{translated.clearControlled}</h2>
<Cell
title={
<Input
placeholder={translated.clearControlled}
value={keyword}
onChange={setKeyword}
/>
}
extra={
<Button
onClick={() => {
setKeyword('')
}}
>
{translated.clearButton}
</Button>
}
/>

<h2>{translated.wordCount}</h2>
<div
style={{
Expand Down
36 changes: 36 additions & 0 deletions src/packages/input/doc.taro.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,42 @@ export default App;

:::

### 受控下的清除

:::demo

```tsx
import React, { useState } from "react";
import { Input, Cell, Button } from '@nutui/nutui-react-taro';

const App = () => {
const [keyword, setKeyword] = useState('')
return (
<Cell
title={
<Input
placeholder="受控下的清除"
value={keyword}
onChange={setKeyword}
/>
}
extra={
<Button
onClick={() => {
setKeyword('')
}}
>
点我清除
</Button>
}
/>
)
}
export default App;
```

:::

### 格式化输入内容

:::demo
Expand Down
28 changes: 9 additions & 19 deletions src/packages/input/input.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,18 @@ export const Input = forwardRef(
forceUpdate()
}

const handleFocus = (event: Event) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改动,也可以用在 input.tsx 上。

const val: any = (event.target as any).value
onFocus && onFocus(val)
const handleFocus = () => {
onFocus?.(value)
setActive(true)
}

const handleInput = (value: string) => {
updateValue(value, 'onChange')
}

const handleBlur = (event: Event) => {
const val: any = (event.target as any).value
updateValue(val, 'onBlur')
setTimeout(() => {
setActive(false)
}, 50)
const handleBlur = () => {
updateValue(value, 'onBlur')
setActive(false)
}
const inputType = (type: any) => {
if (getEnv() === ENV_TYPE.WEB) {
Expand Down Expand Up @@ -220,12 +216,8 @@ export const Input = forwardRef(
value={value}
focus={autoFocus}
confirmType={confirmType}
onBlur={(e: any) => {
handleBlur(e)
}}
onFocus={(e: any) => {
handleFocus(e)
}}
onBlur={handleBlur}
onFocus={handleFocus}
onInput={(e: any) => {
handleInput(e.currentTarget.value)
}}
Expand All @@ -241,10 +233,8 @@ export const Input = forwardRef(
onClick={(e) => {
e.stopPropagation()
if (!disabled) {
setTimeout(() => {
setValue('')
onClear && onClear('')
}, 50)
setValue('')
onClear?.('')
}
}}
>
Expand Down
22 changes: 9 additions & 13 deletions src/packages/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,18 @@ export const Input = forwardRef(
}
}

const handleFocus = (event: Event) => {
const val: any = (event.target as any).value
onFocus && onFocus(val)
const handleFocus = () => {
onFocus?.(value)
setActive(true)
}

const handleInput = (value: string) => {
updateValue(value, 'onChange')
}

const handleBlur = (event: Event) => {
const val: any = (event.target as any).value
updateValue(val, 'onBlur')
setTimeout(() => {
setActive(false)
}, 200)
const handleBlur = () => {
updateValue(value, 'onBlur')
setActive(false)
}

const inputType = (type: string) => {
Expand Down Expand Up @@ -205,11 +201,11 @@ export const Input = forwardRef(
value={value}
autoFocus={autoFocus}
enterKeyHint={confirmType}
onBlur={(e: any) => {
handleBlur(e)
onBlur={() => {
handleBlur()
}}
onFocus={(e: any) => {
handleFocus(e)
onFocus={() => {
handleFocus()
}}
onChange={(e: any) => {
handleInput(e.currentTarget.value)
Expand Down
Loading