Skip to content

Commit b3ff07c

Browse files
committed
(fix):SearchBar Add Delete Function
1 parent db8f245 commit b3ff07c

File tree

4 files changed

+75
-32
lines changed

4 files changed

+75
-32
lines changed

example/examples/src/routes/SearchBar/index.tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
import React, {useState} from 'react';
2-
import {SafeAreaView} from 'react-native';
3-
import {SearchBar} from '@uiw/react-native';
4-
import {ComProps} from '../../routes';
1+
import React, { useState } from 'react';
2+
import { SafeAreaView } from 'react-native';
3+
import { SearchBar } from '@uiw/react-native';
4+
import { ComProps } from '../../routes';
55
import Layout from '../../Layout';
6-
6+
const datas = [
7+
{ label: '上海', value: 1 },
8+
{ label: '南京', value: 2 },
9+
{ label: '天津', value: 3 },
10+
{ label: '杭州', value: 4 },
11+
{ label: '北京', value: 5 },
12+
]
713
const SearchBarDemo = (props: ComProps) => {
8-
const {Header} = Layout;
9-
const {route} = props;
14+
const { Header } = Layout;
15+
const { route } = props;
1016
const description = route.params.description;
1117
const title = route.params.title;
1218

13-
const [data, setData] = useState([
14-
{label: '上海', value: 1},
15-
{label: '南京', value: 2},
16-
]);
19+
const [data, setData] = useState(datas);
1720
return (
18-
<SafeAreaView style={{flex: 1}}>
21+
<SafeAreaView style={{ flex: 1 }}>
1922
<Header title={title} description={description} />
2023
<SearchBar
24+
onFocus={() => setData(datas)}
25+
onChangeText={(text) => {
26+
let search
27+
if(text){
28+
search = data.filter(item => item.label === text)
29+
}else {
30+
search = datas
31+
}
32+
setData(search)
33+
}}
2134
labelInValue
2235
options={data}
23-
onChange={(val: string) => console.log('val', val)}
36+
onChange={(val) => console.log('val', val)}
2437
/>
2538
</SafeAreaView>
2639
);

packages/core/src/SearchBar/README.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,34 @@ function Demo() {
2323

2424
### props
2525

26-
| 参数 | 说明 | 类型 | 默认值 |
27-
| -------------------- | ------------ | ------- | ------- |
28-
| `labelInValue` |是否把每个选项的 label 包装到 value 中,会把 Select 的 value 类型从 string 变为 { key: string, label: ReactNode } 的格式 | Boolean | `false` |
29-
| `options` | 数据化配置选项内容,相比 jsx 定义会获得更好的渲染性能 | { label, value }[] | [] |
30-
| `onChange` | 时间变化 | void | - |
31-
| `onFocus` | 获得焦点时回调 | void | () => { } |
32-
| `onChangeText` | 搜索框文字变化 | void | - |
33-
| `disabled` | 是否禁用 | boolean | false |
34-
| `placeholder` | 搜索框默认文本 | string | `输入搜索...` |
35-
| `loading` | 加载中状态 | boolean | false |
36-
| `extra` | 图标 | dom | extra : <Icon xml={down} size={18} /> |
26+
```ts
27+
interface OptionsState {
28+
label: string;
29+
value: string | number;
30+
}
31+
32+
interface SearchBarProps {
33+
/** 搜索框文字变化 */
34+
onChangeText?: (value: string) => void;
35+
/** 数据化配置选项内容,相比 jsx 定义会获得更好的渲染性能 */
36+
options?: Array<OptionsState>;
37+
/** 事件变化回调 */
38+
onChange?: (value: string | null) => void;
39+
/** 获得焦点时回调 */
40+
onFocus?: (e: any | string) => void;
41+
/** 是否把每个选项的 label 包装到 value 中,会把 Select 的 value 类型从 string 变为 { key: string, label: ReactNode } 的格式 */
42+
labelInValue?: Boolean;
43+
/** 是否禁用 */
44+
disabled?: Boolean;
45+
/***/
46+
value?: String;
47+
/** 加载中状态 */
48+
loading?: Boolean;
49+
/** 搜索框默认文本 */
50+
placeholder?: String;
51+
/** 图标 */
52+
extra?: JSX.Element;
53+
/** 是否展示清楚图标 */
54+
showClear?: boolean;
55+
}
56+
```

packages/core/src/SearchBar/index.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ import Icon from '../Icon';
1717
interface SearchBarProps {
1818
onChangeText?: (value: string) => void;
1919
options?: Array<OptionsState>;
20-
onChange?: (value: string) => void;
20+
onChange?: (value: string | null) => void;
2121
onFocus?: (e: any | string) => void;
2222
labelInValue?: Boolean;
2323
disabled?: Boolean;
2424
value?: String;
2525
loading?: Boolean;
2626
placeholder?: String;
2727
extra?: JSX.Element;
28+
showClear?: boolean;
2829
}
2930

3031
interface OptionsState {
@@ -44,6 +45,7 @@ export default function SearchBar({
4445
loading,
4546
placeholder,
4647
extra,
48+
showClear = true
4749
}: SearchBarProps) {
4850
const onHandleChangeText = (val: string) => {
4951
onChangeText && onChangeText(val);
@@ -69,7 +71,18 @@ export default function SearchBar({
6971
<Pressable onPress={showSearchBar}>
7072
<View style={[styles.content]}>
7173
<Text style={styles.contentTitle}>{textValue ? textValue : placeholder}</Text>
72-
{React.isValidElement(extra) ? extra : <Icon xml={down} size={18} />}
74+
{React.isValidElement(extra) ? (
75+
extra
76+
) :
77+
curValue && showClear ?
78+
<Pressable onPress={() => {
79+
onChange && onChange(null)
80+
setCurValue(null)
81+
}} style={{ paddingRight: 3 }}>
82+
<Icon name="circle-close-o" size={18} color="#ccc" />
83+
</Pressable> :
84+
<Icon xml={down} size={18} color="#A19EA0" />
85+
}
7386
</View>
7487
</Pressable>
7588
) : (
@@ -110,9 +123,9 @@ export default function SearchBar({
110123
const selectValue:
111124
| any
112125
| {
113-
key: string;
114-
label: string;
115-
} = labelInValue ? { key: itm.value, label: itm.label } : itm.value;
126+
key: string;
127+
label: string;
128+
} = labelInValue ? { key: itm.value, label: itm.label } : itm.value;
116129
onChange && onChange(selectValue);
117130
setCurValue(selectValue);
118131
setVisivble(false);

packages/core/src/SearchBar/svg.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)