-
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 2 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
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,35 +7,57 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
Close, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Star, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} from '@nutui/icons-react-taro' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Icon } from '@tarojs/components' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { harmony } from '@/utils/platform-taro' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
const Demo5 = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// TODO:harmony 下图标为了适配展示使用,待icon适配之后统一移除 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const isHarmony = harmony() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<SearchBar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
left={ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<ArrowLeft size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Close size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
isHarmony ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Icon type="waiting" size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Icon type="cancel" size={20} color="#c2c4cc" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) : ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<ArrowLeft size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Close size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
right={ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Star | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
size={20} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
style={{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
color: 'var(--nutui-color-primary)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<More size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
isHarmony ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Icon type="cancel" size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Icon type="info" size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) : ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Star | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
size={20} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
style={{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
color: 'var(--nutui-color-primary)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<More size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
rightIn={ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Photograph | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
size={16} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
onClick={() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log('Photograph right in') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
isHarmony ? ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Icon type="download" size={20} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) : ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Photograph | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
size={16} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
onClick={() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log('Photograph right in') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. 点击事件处理需要适配鸿蒙平台 鸿蒙平台下的图标点击事件处理缺失,需要保持与其他平台行为一致。 建议修改为: rightIn={
isHarmony ? (
- <Icon type="download" size={20} />
+ <Icon
+ type="download"
+ size={20}
+ onClick={() => {
+ console.log('Photograph right in')
+ }}
+ />
) : (
<Photograph
size={16}
onClick={() => {
console.log('Photograph right in')
}}
/>
)
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
</> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,13 @@ | ||||||||||||||||||||||||||||||||||||||||||
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, Icon } 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' | ||||||||||||||||||||||||||||||||||||||||||
import { harmony } from '@/utils/platform-taro' | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
export interface SearchBarProps extends BasicComponent { | ||||||||||||||||||||||||||||||||||||||||||
value?: number | string | ||||||||||||||||||||||||||||||||||||||||||
value?: string | ||||||||||||||||||||||||||||||||||||||||||
placeholder?: string | ||||||||||||||||||||||||||||||||||||||||||
shape?: 'square' | 'round' | ||||||||||||||||||||||||||||||||||||||||||
disabled?: boolean | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -27,6 +28,9 @@ export interface SearchBarProps extends BasicComponent { | |||||||||||||||||||||||||||||||||||||||||
onInputClick?: (event: MouseEvent<HTMLInputElement>) => void | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// TODO:harmony 下图标为了适配展示使用,待icon适配之后统一移除 | ||||||||||||||||||||||||||||||||||||||||||
const isHarmony = harmony() | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
const defaultProps = { | ||||||||||||||||||||||||||||||||||||||||||
...ComponentDefaults, | ||||||||||||||||||||||||||||||||||||||||||
placeholder: '', | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -40,7 +44,7 @@ const defaultProps = { | |||||||||||||||||||||||||||||||||||||||||
left: '', | ||||||||||||||||||||||||||||||||||||||||||
right: '', | ||||||||||||||||||||||||||||||||||||||||||
rightIn: '', | ||||||||||||||||||||||||||||||||||||||||||
leftIn: <Search size="16" />, | ||||||||||||||||||||||||||||||||||||||||||
leftIn: isHarmony ? <Icon type="search" size={16} /> : <Search size="16" />, | ||||||||||||||||||||||||||||||||||||||||||
} as SearchBarProps | ||||||||||||||||||||||||||||||||||||||||||
export const SearchBar: FunctionComponent< | ||||||||||||||||||||||||||||||||||||||||||
Partial<SearchBarProps> & | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -87,21 +91,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() | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+90
to
+95
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 onInput = (event: any) => {
const eventValue = event?.detail?.value
if (value === eventValue) return
- onChange && onChange?.(eventValue, event)
+ onChange?.(eventValue, event)
setValue(eventValue)
eventValue === '' && forceFocus()
} 另外,建议添加对 const onInput = (event: ITouchEvent) => { 🧰 Tools🪛 Biome[error] 93-93: Change to an optional chain. Unsafe fix: Change to an optional chain. (lint/complexity/useOptionalChain) |
||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
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) | ||||||||||||||||||||||||||||||||||||||||||
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. 都判断过了,是不是不需要在写? 了。 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.
已修改 |
||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
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 +114,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 = () => { | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -167,7 +171,11 @@ export const SearchBar: FunctionComponent< | |||||||||||||||||||||||||||||||||||||||||
className={`${classPrefix}-clear ${classPrefix}-icon`} | ||||||||||||||||||||||||||||||||||||||||||
onClick={(e: any) => clearaVal(e)} | ||||||||||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||||||||||
<MaskClose size={16} /> | ||||||||||||||||||||||||||||||||||||||||||
{isHarmony ? ( | ||||||||||||||||||||||||||||||||||||||||||
<Icon type="cancel" size={16} color="#c2c4cc" /> | ||||||||||||||||||||||||||||||||||||||||||
) : ( | ||||||||||||||||||||||||||||||||||||||||||
<MaskClose size={16} /> | ||||||||||||||||||||||||||||||||||||||||||
)} | ||||||||||||||||||||||||||||||||||||||||||
</View> | ||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -182,13 +190,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.
这个简化一下。
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.
已修改