Skip to content

Commit dafa729

Browse files
committed
fix:Update SearchBar & SearchInputBar
1 parent 55e7f1d commit dafa729

File tree

5 files changed

+159
-119
lines changed

5 files changed

+159
-119
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@ import {SafeAreaView} from 'react-native';
33
import {SearchBar} from '@uiw/react-native';
44
import {ComProps} from '../../routes';
55
import Layout from '../../Layout';
6+
interface Columns {
7+
label: string;
8+
value: number | string;
9+
}
10+
611
const datas = [
712
{label: '上海', value: 1},
813
{label: '南京', value: 2},
914
{label: '天津', value: 3},
1015
{label: '杭州', value: 4},
1116
{label: '北京', value: 5},
1217
];
18+
1319
const SearchBarDemo = (props: ComProps) => {
1420
const {Header} = Layout;
1521
const {route} = props;
1622
const description = route.params.description;
1723
const title = route.params.title;
1824

19-
const [data, setData] = useState(datas);
25+
const [data, setData] = useState<Columns[]>([]);
2026
return (
2127
<SafeAreaView style={{flex: 1}}>
2228
<Header title={title} description={description} />

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import React, {Component} from 'react';
2-
import {StyleSheet, View, TextInput} from 'react-native';
2+
import {StyleSheet, View, Text} from 'react-native';
33
import Layout, {Container} from '../../Layout';
44
import {SearchInputBar, Toast} from '@uiw/react-native';
55
import {ComProps} from '../../routes';
66

7-
const {Header, Body, Card, Footer} = Layout;
7+
const {Header, Body, Footer} = Layout;
88

99
export interface IndexProps extends ComProps {}
1010
export interface IndexState {
1111
value?: string;
1212
value2?: string;
13+
loading?: boolean;
1314
}
1415

1516
export default class Index extends Component<IndexProps, IndexState> {
@@ -20,6 +21,7 @@ export default class Index extends Component<IndexProps, IndexState> {
2021
this.state = {
2122
value: '',
2223
value2: '',
24+
loading: false,
2325
};
2426
}
2527
onChangeText = (val: string, key: 'value' | 'value2') => {
@@ -42,30 +44,41 @@ export default class Index extends Component<IndexProps, IndexState> {
4244
ref={this.ref}
4345
onChangeText={val => this.onChangeText(val, 'value')}
4446
onClear={() => this.onClear('value')}
45-
placeholder="请输入搜索关键字"
47+
placeholder="请输入搜索关键字..."
48+
actionName="搜一下"
4649
value={this.state.value}
47-
button={{
48-
type: 'success',
50+
touchProps={{
4951
onPress: () => {
5052
console.log('object', this.ref);
51-
Toast.info('你点击了搜索', 2, 'info');
53+
this.setState({loading: true}, () => {
54+
setTimeout(() => {
55+
this.setState({loading: false});
56+
}, 3000);
57+
});
5258
},
5359
}}
60+
loading={this.state.loading}
5461
/>
5562
<View style={{height: 100}} />
5663
<SearchInputBar
5764
onChangeText={val => this.onChangeText(val, 'value2')}
5865
onClear={() => this.onClear('value2')}
5966
value={this.state.value2}
6067
placeholder="请输入搜索关键字"
61-
actionName=" 搜一下"
68+
searchRender={
69+
<View style={styles.search}>
70+
<Text>搜索</Text>
71+
</View>
72+
}
6273
showActionButton
63-
buttonWidth={120}
64-
button={{
74+
touchProps={{
6575
onPress() {
6676
Toast.info('你点击了搜一下', 2, 'info');
6777
},
6878
}}
79+
containerStyle={{
80+
marginHorizontal: 10,
81+
}}
6982
/>
7083
</Body>
7184
<Footer />
@@ -84,4 +97,9 @@ const styles = StyleSheet.create({
8497
divider: {
8598
marginVertical: 10,
8699
},
100+
search: {
101+
justifyContent: 'center',
102+
borderWidth: 0,
103+
paddingRight: 10,
104+
},
87105
});

packages/core/src/SearchBar/index.tsx

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, memo, useCallback } from 'react';
22
import {
33
View,
44
Text,
5-
TextInput,
65
SafeAreaView,
76
StyleSheet,
87
TouchableWithoutFeedback,
98
ActivityIndicator,
109
Pressable,
1110
} from 'react-native';
1211
import MaskLayer from '../MaskLayer';
12+
import SearchInputBar from '../SearchInputBar';
1313
import List from '../List';
1414
import { down } from './svg';
1515
import Icon from '../Icon';
@@ -19,6 +19,7 @@ interface SearchBarProps {
1919
options?: Array<OptionsState>;
2020
onChange?: (value: string | null) => void;
2121
onFocus?: (e: any | string) => void;
22+
onBlur?: (e: any | string) => void;
2223
labelInValue?: Boolean;
2324
disabled?: Boolean;
2425
value?: String;
@@ -34,24 +35,24 @@ interface OptionsState {
3435
}
3536

3637
// 搜索组件
37-
export default function SearchBar({
38+
function SearchBar({
3839
onChangeText,
3940
options = [],
4041
onChange,
4142
labelInValue,
4243
disabled,
4344
value,
4445
onFocus,
46+
onBlur,
4547
loading,
4648
placeholder,
4749
extra,
4850
showClear = true,
4951
}: SearchBarProps) {
50-
const onHandleChangeText = (val: string) => {
51-
onChangeText && onChangeText(val);
52-
};
5352
const [curValue, setCurValue] = useState<any>(value);
5453
const [visible, setVisivble] = useState(false);
54+
const textValue = labelInValue ? curValue && curValue.label : curValue;
55+
5556
useEffect(() => {
5657
setCurValue(value);
5758
}, [value]);
@@ -60,13 +61,19 @@ export default function SearchBar({
6061
visible && onFocus;
6162
}, [visible]);
6263

64+
// 搜索
65+
const onHandleChangeText = (val: string) => {
66+
onChangeText && onChangeText(val);
67+
};
68+
69+
// 点击打开遮罩层
6370
const showSearchBar = () => {
6471
if (disabled) {
6572
return;
6673
}
6774
setVisivble(true);
6875
};
69-
const textValue = labelInValue ? curValue && curValue.label : curValue;
76+
7077
return !visible ? (
7178
<Pressable onPress={showSearchBar}>
7279
<View style={[styles.content]}>
@@ -91,30 +98,27 @@ export default function SearchBar({
9198
) : (
9299
<MaskLayer visible={visible}>
93100
<SafeAreaView style={styles.container}>
94-
<View style={styles.inputBox}>
95-
<View style={styles.leftBox}>
96-
<View style={styles.icon}>
97-
<Icon name="search" color="#ccc" size={20} />
98-
</View>
99-
<TextInput
100-
placeholderTextColor="#000"
101-
onFocus={onFocus && onFocus}
102-
style={styles.input}
103-
placeholder="输入搜索..."
104-
onChangeText={onHandleChangeText}
105-
/>
106-
</View>
107-
108-
<TouchableWithoutFeedback
109-
onPress={() => {
110-
setVisivble(false);
111-
}}
112-
>
113-
<View style={styles.cancel}>
114-
<Text>取消</Text>
115-
</View>
116-
</TouchableWithoutFeedback>
117-
</View>
101+
<SearchInputBar
102+
loading={loading}
103+
containerStyle={{ backgroundColor: '#fff', marginHorizontal: 10 }}
104+
autoFocus
105+
showActionButton
106+
placeholder="输入搜索..."
107+
onChangeText={onHandleChangeText}
108+
onFocus={onFocus && onFocus}
109+
onBlur={onBlur && onBlur}
110+
searchRender={
111+
<TouchableWithoutFeedback
112+
onPress={() => {
113+
setVisivble(false);
114+
}}
115+
>
116+
<View style={styles.cancel}>
117+
<Text>取消</Text>
118+
</View>
119+
</TouchableWithoutFeedback>
120+
}
121+
/>
118122
{loading ? (
119123
<ActivityIndicator color="#0A0258" size="large" style={styles.loading} />
120124
) : (
@@ -144,6 +148,8 @@ export default function SearchBar({
144148
);
145149
}
146150

151+
export default memo(SearchBar);
152+
147153
const styles = StyleSheet.create({
148154
container: {
149155
flex: 1,
@@ -153,39 +159,14 @@ const styles = StyleSheet.create({
153159
fontSize: 16,
154160
color: 'black',
155161
},
156-
inputBox: {
157-
paddingLeft: 10,
158-
paddingRight: 10,
159-
marginTop: 5,
160-
flexDirection: 'row',
161-
alignItems: 'center',
162-
height: 40,
163-
},
164-
leftBox: {
165-
flex: 1,
166-
flexDirection: 'row',
167-
backgroundColor: '#fff',
168-
alignItems: 'center',
169-
paddingTop: 8,
170-
paddingBottom: 8,
171-
},
172162
icon: {
173163
backgroundColor: '#fff',
174164
paddingLeft: 10,
175165
justifyContent: 'center',
176166
},
177-
input: {
178-
backgroundColor: '#fff',
179-
fontSize: 16,
180-
flex: 1,
181-
paddingLeft: 10,
182-
color: '#7C7D7E',
183-
paddingBottom: 0,
184-
paddingTop: 0,
185-
},
186167
cancel: {
187168
color: '#7C7D7E',
188-
paddingLeft: 10,
169+
paddingRight: 10,
189170
justifyContent: 'center',
190171
},
191172
list: {

packages/core/src/SearchInputBar/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function Demo() {
2727
}
2828
```
2929

30-
## 一直显示右侧按钮 && 自定义placeholder
30+
## 一直显示右侧按钮 && 自定义placeholder && 自定右侧搜索内容
3131

3232
```jsx
3333
import { Fragment, useState } from 'react';
@@ -41,6 +41,7 @@ function Demo() {
4141
onClear={()=>setValue('')}
4242
value={value}
4343
placeholder="请输入搜索关键字"
44+
searchRender={<View style={styles.search}><Text>搜索</Text></View>}
4445
actionName="搜一下"
4546
showActionButton
4647
button={{
@@ -115,7 +116,7 @@ function Demo() {
115116
```
116117

117118
## Props
118-
119+
**注意: 组件继承了```TextInput```属性**
119120
```ts
120121
import { TextInputProps,StyleProp,ViewStyle } from 'react-native';
121122
import { ButtonProps,IconsProps } from '@uiw/react-native'
@@ -129,13 +130,19 @@ export interface SearchInputBarProps extends TextInputProps {
129130
actionName?: string;
130131
/** 是否一直显示右侧按钮 null = 永不显示 */
131132
showActionButton?: boolean | null;
132-
/** 搜索图标 */
133+
/** 搜索图标Icon参数 参考Icon组件 */
133134
searchIcon?: IconsProps;
134135
/** 点击搜索图标时触发事件 */
135136
onSearch?: Function;
136-
/** 清除图标 */
137+
/** 清除图标Icon参数 参考Icon组件 */
137138
closeIcon?: IconsProps;
138139
/** 点击清除图标时触发事件 */
139140
onClear?: Function;
141+
/** 自定义搜索 */
142+
searchRender?: JSX.Element;
143+
/** 输入框TextInput样式 */
144+
inputStyle?: TextStyle
145+
/** loading加载 */
146+
loading?: any
140147
}
141148
```

0 commit comments

Comments
 (0)