-
Notifications
You must be signed in to change notification settings - Fork 298
feat(searchbar): V15适配,新增tag的实现方式 #3214
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba6ee42
feat(searchbar): v15 适配,增加了受控和默认值
xiaoyatong 63ae5b5
docs: doc
xiaoyatong 0e8cd46
feat: taro 版本
xiaoyatong 202063c
feat: 适配鸿蒙
xiaoyatong f174e0d
fix: 主题同步
xiaoyatong ec8d87f
test: fixed
xiaoyatong 94d1c43
fix: demos
xiaoyatong 77ba15b
fix: 删除无用代码
xiaoyatong 0bb4b4c
feat: tags
xiaoyatong e78d326
fix: 修复tag设定异常的情况
xiaoyatong e86b883
fix: 兼容taro h5
xiaoyatong cd5dbab
fix: type
xiaoyatong 08dd01e
test: fixed
xiaoyatong 1937622
test: add tags
xiaoyatong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,89 @@ | ||
| import * as React from 'react' | ||
| import { fireEvent, render } from '@testing-library/react' | ||
| import { render, fireEvent, waitFor } from '@testing-library/react' | ||
| import '@testing-library/jest-dom' | ||
| import { useState } from 'react' | ||
| import SearchBar from '@/packages/searchbar' | ||
|
|
||
| test('basic usage', () => { | ||
| const { container } = render(<SearchBar placeholder="请输入文字" />) | ||
| expect(container.querySelector('.nut-searchbar-input')).toHaveAttribute( | ||
| 'placeholder', | ||
| '请输入文字' | ||
| test('should render with placeholder', () => { | ||
| const { getByPlaceholderText } = render( | ||
| <SearchBar placeholder="请输入文字" /> | ||
| ) | ||
| expect(getByPlaceholderText('请输入文字')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('should limit maxlength of input value when using maxlength prop', () => { | ||
| const { container } = render(<SearchBar shape="round" maxLength={5} />) | ||
| expect(container.querySelector('.nut-searchbar-input')).toHaveAttribute( | ||
| 'maxlength', | ||
| '5' | ||
| ) | ||
| expect(container.querySelector('.nut-searchbar-content')).toHaveClass( | ||
| 'nut-searchbar-round' | ||
| ) | ||
| const input = container.querySelector('.nut-searchbar-input') | ||
| expect(input).toHaveAttribute('maxlength', '5') | ||
| expect(input?.parentNode).toHaveClass('nut-searchbar-round') | ||
| }) | ||
|
|
||
| test('Search box text settings', () => { | ||
| test('should display left and right text', () => { | ||
| const { container } = render(<SearchBar left="文本" right="确定" />) | ||
| expect(container.querySelector('.nut-searchbar-left')?.innerHTML).toBe('文本') | ||
| expect(container.querySelector('.nut-searchbar-right')?.innerHTML).toBe( | ||
| '确定' | ||
| ) | ||
| }) | ||
|
|
||
| test('Search clear & change', () => { | ||
| const change = vi.fn() | ||
| const { container } = render( | ||
| <SearchBar value="123" onChange={change} maxLength={10} /> | ||
| ) | ||
| const input = container.querySelector('.nut-searchbar-input') | ||
| expect(input?.getAttribute('value')).toBe('123') | ||
| const clear = container.querySelector('.nut-searchbar-clear') | ||
| fireEvent.click(clear as Element) | ||
| expect(change).toBeCalledWith('') | ||
| expect(input?.getAttribute('value')).toBe('') | ||
| test('should render with tags', () => { | ||
| const { container } = render(<SearchBar tag value="add,add3" />) | ||
| const dvalues = container.querySelectorAll('.nut-searchbar-value') | ||
| expect(dvalues.length).toBe(2) | ||
| }) | ||
|
|
||
| test('should render right-in element', () => { | ||
| const { container, rerender } = render(<SearchBar rightIn="搜索" />) | ||
| const rightin = container.querySelectorAll('.nut-searchbar-rightin') | ||
| expect(rightin.length).toBe(1) | ||
| rerender(<SearchBar rightIn={<div className="test">搜索</div>} />) | ||
| const test = container.querySelectorAll('.test') | ||
| expect(test.length).toBe(1) | ||
| }) | ||
|
|
||
| test('should handle all events correctly', async () => { | ||
| const handleChange = vi.fn() | ||
| const handleFocus = vi.fn() | ||
| const handleBlur = vi.fn() | ||
| const handleClick = vi.fn() | ||
| const handleClear = vi.fn() | ||
| const Demo = () => { | ||
| const [value, setValue] = useState('奶茶') | ||
| const onChange = (newValue: string) => { | ||
| setValue(newValue) // 更新状态 | ||
| handleChange(newValue) // 调用传入的 onChange 处理函数 | ||
| } | ||
| return ( | ||
| <SearchBar | ||
| value={value} | ||
| autoFocus | ||
| onChange={onChange} | ||
| onFocus={handleFocus} | ||
| onBlur={handleBlur} | ||
| onInputClick={handleClick} | ||
| onClear={handleClear} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| const { container } = render(<Demo />) | ||
| const inputEl = container.querySelector('.nut-searchbar-input') as Element | ||
| expect(inputEl).toHaveValue('奶茶') | ||
| fireEvent.click(inputEl) | ||
| expect(handleClick).toHaveBeenCalledTimes(1) | ||
| fireEvent.change(inputEl, { target: { value: '冰激凌' } }) | ||
|
|
||
| await waitFor(() => { | ||
| expect(handleFocus).toHaveBeenCalledTimes(1) | ||
| expect(handleChange).toHaveBeenCalledTimes(1) | ||
| expect(inputEl).toHaveValue('冰激凌') | ||
| fireEvent.blur(inputEl) | ||
| expect(handleBlur).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| const clear = container.querySelector('.nut-searchbar-clear') as Element | ||
| fireEvent.click(clear) | ||
| await waitFor(() => { | ||
| expect(inputEl).toHaveValue('') | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import React, { useState } from 'react' | ||
| import { SearchBar, Button } from '@nutui/nutui-react' | ||
| import { Photograph, Category } from '@nutui/icons-react' | ||
|
|
||
| const Demo = () => { | ||
| const [value, setValue] = useState('醋溜土豆丝') | ||
| const [value1, setValue1] = useState('西红柿,铁皮') | ||
| return ( | ||
| <> | ||
| <SearchBar | ||
| backable | ||
| leftIn={null} | ||
| value={value1} | ||
| tag | ||
| onItemClick={(val: string) => { | ||
| console.log('click', val) | ||
| const arr = value1.split(',') | ||
| const newArr = arr.filter((item: string) => item !== val) | ||
| const newVal = newArr.length > 1 ? newArr.join(',') : newArr.join('') | ||
| setValue1(newVal) | ||
| }} | ||
| onFocus={(val: string) => { | ||
| console.log('focus value', val) | ||
| setValue1(val.split(',').join('')) | ||
| }} | ||
| onChange={(val) => { | ||
| console.log('onChange', val) | ||
| setValue1(val) | ||
| }} | ||
| rightIn={ | ||
| <div style={{ display: 'flex', alignItems: 'center' }}> | ||
| <Photograph color="#505259" /> | ||
| </div> | ||
| } | ||
| right={<Category />} | ||
| /> | ||
| <SearchBar | ||
| backable | ||
| leftIn={null} | ||
| value={value} | ||
| onChange={(val) => { | ||
| setValue(val) | ||
| }} | ||
| autoFocus | ||
| rightIn={ | ||
| <div style={{ display: 'flex', alignItems: 'center' }}> | ||
| <Photograph color="#505259" style={{ marginRight: '12px' }} /> | ||
| <Button type="primary" size="mini"> | ||
| 搜索 | ||
| </Button> | ||
| </div> | ||
| } | ||
| /> | ||
| </> | ||
| ) | ||
| } | ||
| export default Demo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
事件处理测试需要细化和完善
这个测试综合验证了多个事件,但有几点可以改进:
autoFocus属性但没有断言它是否生效waitFor块中验证多个不同时间发生的事件,可能导致测试不稳定建议添加以下测试:
另外,建议将事件处理测试细分为多个独立的
waitFor块:fireEvent.change(inputEl, { target: { value: '冰激凌' } }) await waitFor(() => { expect(handleFocus).toHaveBeenCalledTimes(1) expect(handleChange).toHaveBeenCalledTimes(1) expect(inputEl).toHaveValue('冰激凌') - fireEvent.blur(inputEl) - expect(handleBlur).toHaveBeenCalled() }) + +fireEvent.blur(inputEl) +await waitFor(() => { + expect(handleBlur).toHaveBeenCalled() +})📝 Committable suggestion