-
Notifications
You must be signed in to change notification settings - Fork 271
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(searchbar): 多端适配 #2657
fix(searchbar): 多端适配 #2657
Changes from 11 commits
511e1f6
ee3f33b
07665ae
f478704
e122e64
2e9cc4f
d4adac8
8c714cb
847c2ed
a4f2168
2f0fde6
c7fa09c
6709e07
dc9241b
35530d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,12 @@ | ||||||||||||||||||||||||||||||||||||||||||
import React, { FunctionComponent, useEffect, useRef, useState } from 'react' | ||||||||||||||||||||||||||||||||||||||||||
import type { ChangeEvent, FocusEvent, MouseEvent } from 'react' | ||||||||||||||||||||||||||||||||||||||||||
import { View, ITouchEvent } from '@tarojs/components' | ||||||||||||||||||||||||||||||||||||||||||
import { View, ITouchEvent, Input as TaroInput } from '@tarojs/components' | ||||||||||||||||||||||||||||||||||||||||||
import { MaskClose, Search, ArrowLeft } from '@nutui/icons-react-taro' | ||||||||||||||||||||||||||||||||||||||||||
import { useConfig } from '@/packages/configprovider/configprovider.taro' | ||||||||||||||||||||||||||||||||||||||||||
import { BasicComponent, ComponentDefaults } from '@/utils/typings' | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
export interface SearchBarProps extends BasicComponent { | ||||||||||||||||||||||||||||||||||||||||||
value?: number | string | ||||||||||||||||||||||||||||||||||||||||||
value?: string | ||||||||||||||||||||||||||||||||||||||||||
placeholder?: string | ||||||||||||||||||||||||||||||||||||||||||
shape?: 'square' | 'round' | ||||||||||||||||||||||||||||||||||||||||||
disabled?: boolean | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -87,21 +87,20 @@ export const SearchBar: FunctionComponent< | |||||||||||||||||||||||||||||||||||||||||
const searchSelf: HTMLInputElement | null = searchRef.current | ||||||||||||||||||||||||||||||||||||||||||
searchSelf && searchSelf.focus() | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const change = (event: ChangeEvent<HTMLInputElement>) => { | ||||||||||||||||||||||||||||||||||||||||||
if (value === event.target.value) return | ||||||||||||||||||||||||||||||||||||||||||
onChange && onChange?.(event.target.value, event) | ||||||||||||||||||||||||||||||||||||||||||
setValue(event.target.value) | ||||||||||||||||||||||||||||||||||||||||||
event.target.value === '' && forceFocus() | ||||||||||||||||||||||||||||||||||||||||||
const onInput = (event: any) => { | ||||||||||||||||||||||||||||||||||||||||||
const eventValue = event?.detail?.value | ||||||||||||||||||||||||||||||||||||||||||
if (value === eventValue) return | ||||||||||||||||||||||||||||||||||||||||||
onChange && onChange?.(eventValue, event) | ||||||||||||||||||||||||||||||||||||||||||
setValue(eventValue) | ||||||||||||||||||||||||||||||||||||||||||
eventValue === '' && forceFocus() | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const focus = (event: FocusEvent<HTMLInputElement>) => { | ||||||||||||||||||||||||||||||||||||||||||
const { value } = event.target | ||||||||||||||||||||||||||||||||||||||||||
onFocus && onFocus?.(value, event) | ||||||||||||||||||||||||||||||||||||||||||
const focus = (event: any) => { | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any 类型处理下~ |
||||||||||||||||||||||||||||||||||||||||||
onFocus && onFocus(event?.detail?.value, event) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const blur = (event: FocusEvent<HTMLInputElement>) => { | ||||||||||||||||||||||||||||||||||||||||||
const blur = (event: any) => { | ||||||||||||||||||||||||||||||||||||||||||
const searchSelf: HTMLInputElement | null = searchRef.current | ||||||||||||||||||||||||||||||||||||||||||
searchSelf && searchSelf.blur() | ||||||||||||||||||||||||||||||||||||||||||
const { value } = event.target | ||||||||||||||||||||||||||||||||||||||||||
onBlur && onBlur?.(value, event) | ||||||||||||||||||||||||||||||||||||||||||
onBlur && onBlur(event?.detail?.value, event) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||
setValue(outerValue || '') | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -111,26 +110,27 @@ export const SearchBar: FunctionComponent< | |||||||||||||||||||||||||||||||||||||||||
}, [autoFocus]) | ||||||||||||||||||||||||||||||||||||||||||
const renderField = () => { | ||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||
<input | ||||||||||||||||||||||||||||||||||||||||||
<TaroInput | ||||||||||||||||||||||||||||||||||||||||||
className={`${classPrefix}-input ${ | ||||||||||||||||||||||||||||||||||||||||||
clearable ? `${classPrefix}-input-clear` : '' | ||||||||||||||||||||||||||||||||||||||||||
}`} | ||||||||||||||||||||||||||||||||||||||||||
ref={searchRef} | ||||||||||||||||||||||||||||||||||||||||||
style={style} | ||||||||||||||||||||||||||||||||||||||||||
value={value || ''} | ||||||||||||||||||||||||||||||||||||||||||
placeholder={placeholder || locale.placeholder} | ||||||||||||||||||||||||||||||||||||||||||
disabled={disabled} | ||||||||||||||||||||||||||||||||||||||||||
readOnly={readOnly} | ||||||||||||||||||||||||||||||||||||||||||
maxLength={maxLength} | ||||||||||||||||||||||||||||||||||||||||||
onKeyPress={onKeypress} | ||||||||||||||||||||||||||||||||||||||||||
onChange={(e) => change(e)} | ||||||||||||||||||||||||||||||||||||||||||
onFocus={(e) => focus(e)} | ||||||||||||||||||||||||||||||||||||||||||
onBlur={(e) => blur(e)} | ||||||||||||||||||||||||||||||||||||||||||
onClick={(e) => clickInput(e)} | ||||||||||||||||||||||||||||||||||||||||||
disabled={disabled || readOnly} | ||||||||||||||||||||||||||||||||||||||||||
maxlength={maxLength} | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. readonly 和 disabled 在表现上样式不一样~~ 这个怎么区分呢 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
taro的input只支持disabled, 我参考我们已适配的input组件处理的 |
||||||||||||||||||||||||||||||||||||||||||
// @ts-ignore | ||||||||||||||||||||||||||||||||||||||||||
// onKeyDown={onKeypress} | ||||||||||||||||||||||||||||||||||||||||||
onInput={onInput} | ||||||||||||||||||||||||||||||||||||||||||
onFocus={focus} | ||||||||||||||||||||||||||||||||||||||||||
onBlur={blur} | ||||||||||||||||||||||||||||||||||||||||||
onClick={clickInput} | ||||||||||||||||||||||||||||||||||||||||||
onConfirm={onConfirm} | ||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const clickInput = (e: MouseEvent<HTMLInputElement>) => { | ||||||||||||||||||||||||||||||||||||||||||
const clickInput = (e: any) => { | ||||||||||||||||||||||||||||||||||||||||||
onInputClick && onInputClick(e) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const renderLeftIn = () => { | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -182,13 +182,16 @@ export const SearchBar: FunctionComponent< | |||||||||||||||||||||||||||||||||||||||||
onChange && onChange?.('') | ||||||||||||||||||||||||||||||||||||||||||
onClear && onClear(event) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const onKeypress = (e: any) => { | ||||||||||||||||||||||||||||||||||||||||||
if (e.key === 'Enter' || e.keyCode === 13) { | ||||||||||||||||||||||||||||||||||||||||||
if (typeof e.cancelable !== 'boolean' || e.cancelable) { | ||||||||||||||||||||||||||||||||||||||||||
e.preventDefault() | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
onSearch && onSearch(value as string) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
// const onKeypress = (event: any) => { | ||||||||||||||||||||||||||||||||||||||||||
// if (event?.detail?.keyCode === 13) { | ||||||||||||||||||||||||||||||||||||||||||
// if (typeof event.cancelable !== 'boolean' || event.cancelable) { | ||||||||||||||||||||||||||||||||||||||||||
// event.preventDefault() | ||||||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||||||
// onSearch && onSearch(value as string) | ||||||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||||||
// } | ||||||||||||||||||||||||||||||||||||||||||
const onConfirm = () => { | ||||||||||||||||||||||||||||||||||||||||||
onSearch && onSearch(value as string) | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+185
to
+194
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 搜索确认逻辑优化
- const onConfirm = () => {
- onSearch && onSearch(value as string)
+ const onConfirm = (event: ITouchEvent) => {
+ onSearch?.(value as string)
} 注释说明:移除已注释的 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome[error] 194-194: Change to an optional chain. Unsafe fix: Change to an optional chain. (lint/complexity/useOptionalChain) |
||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||
<View | ||||||||||||||||||||||||||||||||||||||||||
|
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
优化事件处理和空值检查
建议使用可选链操作符来简化代码并提高可读性:
另外,建议添加对
event
参数的类型定义:🧰 Tools
🪛 Biome
[error] 93-93: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)