搜索组件SearchBar
earchBar
搜索框。SearchBar UI 提供与WeLink规范一致的视图。 组件提供了搜索框、搜索结果、搜索按钮的样式。
参数说明
名称 | 类型 | 默认值 | 说明 | |
---|---|---|---|---|
autocomplete | 'off' | |||
defaultValue | string | default | 搜索框的值 | |
lang | object | language | 取消按钮显示字样, 例如:{ cancel: '取消' } | |
onCancel | func | undefined | 取消 | |
onChange | func | undefined | 搜索框的input的onChange事件 | |
onClear | func | undefined | 清除按钮事件 | |
onSubmit | func | undefined | 搜索按钮事件 | |
placeholder | string | '搜索' | 搜索框为空时显示的文字,默认为搜索 | |
searchName | string | 'q' | 搜索框input的name |
import React from 'react';
import {
// main component
SearchBar,
// for display data
Panel,
PanelHeader,
PanelBody,
PanelFooter,
MediaBox,
MediaBoxHeader,
MediaBoxBody,
MediaBoxTitle,
MediaBoxDescription,
Cell,
CellBody,
CellFooter
} from '@wecode/react-weui';
const sampleData = [
'Aaren',
'Bab',
'Cacilia',
'Dacey',
'Eachelle',
'Fae',
'Gabbey',
'Hadria',
'Ianthe',
'Jacenta',
'Kacey',
'La Verne',
'Mab',
'Nada',
'Octavia',
'Page',
'Queenie',
'Rachael',
'Saba',
'Tabatha',
'Ula',
'Val',
'Wallie',
'Xaviera',
'Yalonda',
'Zabrina'
];const appMsgIcon = (
<img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAMAAAAOusbgAAAAeFBMVEUAwAD///+U5ZTc9twOww7G8MYwzDCH4YcfyR9x23Hw+/DY9dhm2WZG0kbT9NP0/PTL8sux7LFe115T1VM+zz7i+OIXxhes6qxr2mvA8MCe6J6M4oz6/frr+us5zjn2/fa67rqB4IF13XWn6ad83nxa1loqyirn+eccHxx4AAAC/klEQVRo3u2W2ZKiQBBF8wpCNSCyLwri7v//4bRIFVXoTBBB+DAReV5sG6lTXDITiGEYhmEYhmEYhmEYhmEY5v9i5fsZGRx9PyGDne8f6K9cfd+mKXe1yNG/0CcqYE86AkBMBh66f20deBc7wA/1WFiTwvSEpBMA2JJOBsSLxe/4QEEaJRrASP8EVF8Q74GbmevKg0saa0B8QbwBdjRyADYxIhqxAZ++IKYtciPXLQVG+imw+oo4Bu56rjEJ4GYsvPmKOAB+xlz7L5aevqUXuePWVhvWJ4eWiwUQ67mK51qPj4dFDMlRLBZTqF3SDvmr4BwtkECu5gHWPkmDfQh02WLxXuvbvC8ku8F57GsI5e0CmUwLz1kq3kD17R1In5816rGvQ5VMk5FEtIiWislTffuDpl/k/PzscdQsv8r9qWq4LRWX6tQYtTxvI3XyrwdyQxChXioOngH3dLgOFjk0all56XRi/wDFQrGQU3Os5t0wJu1GNtNKHdPqYaGYQuRDfbfDf26AGLYSyGS3ZAK4S8XuoAlxGSdYMKwqZKM9XJMtyqXi7HX/CiAZS6d8bSVUz5J36mEMFDTlAFQzxOT1dzLRljjB6+++ejFqka+mXIe6F59mw22OuOw1F4T6lg/9VjL1rLDoI9Xzl1MSYDNHnPQnt3D1EE7PrXjye/3pVpr1Z45hMUdcACc5NVQI0bOdS1WA0wuz73e7/5TNqBPhQXPEFGJNV2zNqWI7QKBd2Gn6AiBko02zuAOXeWIXjV0jNqdKegaE/kJQ6Bfs4aju04lMLkA2T5wBSYPKDGF3RKhFYEa6A1L1LG2yacmsaZ6YPOSAMKNsO+N5dNTfkc5Aqe26uxHpx7ZirvgCwJpWq/lmX1hA7LyabQ34tt5RiJKXSwQ+0KU0V5xg+hZrd4Bn1n4EID+WkQdgLfRNtvil9SPfwy+WQ7PFBWQz6dGWZBLkeJFXZGCfLUjCgGgqXo5TuSu3cugdcTv/HjqnBTEMwzAMwzAMwzAMwzAMw/zf/AFbXiOA6frlMAAAAABJRU5ErkJggg=="
alt=""
/>
);const CellMore = () => (
<Cell access link>
<CellBody>More</CellBody>
<CellFooter />
</Cell>
);export default class SearchBarDemo extends React.Component {
state = {
searchText: '',
results: []
}; handleChange(text, e) {
const keywords = [text];
let results = sampleData.filter(/./.test.bind(new RegExp(keywords.join('|'), 'i'))); if (results.length > 3) results = results.slice(0, 3);
this.setState({
results,
searchText: text
});
} render() {
const { searchText, results } = this.state;
return (
<section>
<SearchBar
onChange={this.handleChange.bind(this)}
defaultValue={searchText}
placeholder="Female Name Search"
onSubmit={(text, e) => {
console.log('~~~~~~~', text);
e.preventDefault();
document.activeElement.blur();
}}
lang={{
cancel: 'Cancel'
}}
/> <Panel style={{ display: searchText ? null : 'none', marginTop: 0 }}>
<PanelHeader>Female Name Search</PanelHeader>
<PanelBody>
{results.length > 0 ? (
results.map((item, i) => (
<MediaBox key={i} type="appmsg" href="javascript:void(0);">
<MediaBoxHeader>{appMsgIcon}</MediaBoxHeader>
<MediaBoxBody>
<MediaBoxTitle>{item}</MediaBoxTitle>
<MediaBoxDescription>You may like this name.</MediaBoxDescription>
</MediaBoxBody>
</MediaBox>
))
) : (
<MediaBox> Can not find any!</MediaBox>
)}
</PanelBody>
<PanelFooter href="javascript:void(0);">
<CellMore />
</PanelFooter>
</Panel>
</section>
);
}
}
- 点赞
- 收藏
- 关注作者
评论(0)