搜索组件SearchBar

举报
云会议运营喵大人 发表于 2020/02/28 09:51:52 2020/02/28
【摘要】 earchBar搜索框。SearchBar UI 提供与WeLink规范一致的视图。 组件提供了搜索框、搜索结果、搜索按钮的样式。参数说明名称类型默认值说明autocomplete'off'defaultValuestringdefault搜索框的值langobjectlanguage取消按钮显示字样, 例如:{ cancel: '取消' }onCancelfuncundefined取消on...

earchBar


搜索框。SearchBar UI 提供与WeLink规范一致的视图。 组件提供了搜索框、搜索结果、搜索按钮的样式。

参数说明

名称类型默认值说明
autocomplete
'off'

defaultValuestringdefault搜索框的值
langobjectlanguage取消按钮显示字样, 例如:{ cancel: '取消' }
onCancelfuncundefined取消
onChangefuncundefined搜索框的input的onChange事件
onClearfuncundefined清除按钮事件
onSubmitfuncundefined搜索按钮事件
placeholderstring'搜索'搜索框为空时显示的文字,默认为搜索
searchNamestring'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/fa67rqB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ABJRU5ErkJggg=="
    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>
    );
  }
}

【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。