Search 搜索

如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @alifd/next@latest -S

开发指南

何时使用

页面、表单数据搜索时使用

API

输入框部分继承 Select.AutoComplete 的能力,可以直接用AutoComplete 的 api

参数说明类型默认值
size大小可选值:'large'('大')'medium'('小')Enum'medium'
type类型 shape=normal: primary/secondary; shape=simple: normal/dark;可选值:'primary', 'secondary', 'normal', 'dark'Enum'normal'
shape形状可选值:'normal', 'simple'Enum'normal'
defaultValue搜索框默认值String-
value搜索框数值String/Number-
onChange输入关键字时的回掉签名:Function(value: Object) => void参数:value: {Object} 输入值Functionfunc.noop
onSearch点击搜索按钮触发的回调签名:Function(value: Object) => void参数:value: {Object} 输入值Functionfunc.noop
defaultFilterValue选择器默认值String-
filter选择器Array[]
filterValue选择器值String-
onFilterChange选择器发生变化时回调签名:Function(filter: Object) => void参数:filter: {Object} valueFunctionfunc.noop
dataSource搜索框下拉联想列表Array-
placeholder默认提示String-
searchTextbutton 的内容ReactNode-
filterProps选择器的propsObject-
buttonProps按钮的额外属性Object{}
popupContent自定义渲染的的下拉框ReactNode-
followTrigger是否跟随滚动Boolean-
visible自定义渲染的的下拉框Boolean-
hasClear是否显示清除按钮Booleanfalse
hasIcon是否显示搜索按钮Booleantrue
disabled是否禁用Booleanfalse

ARIA and KeyBoard

按键说明
Enter触发onSearch事件

代码示例

基础用法

Search 搜索 - 图1

查看源码在线预览

  1. import { Search } from '@alifd/next';
  2. function onSearch(v) {
  3. console.log(v);
  4. }
  5. const App = () => [
  6. <p key="1">simple</p>,
  7. <Search key="2" shape="simple" onSearch={onSearch} style={{width: '400px'}}/>,
  8. <p key="3">default</p>,
  9. <Search key="4" onSearch={onSearch} style={{width: '400px'}}/>,
  10. <p key="5">custom text </p>,
  11. <Search key="6" searchText="search" onSearch={onSearch} style={{width: '400px'}}/>,
  12. <p key="7">custom text widthout icon</p>,
  13. <Search key="8" hasIcon={false} searchText={<span style={{color: 'blue'}}>search</span>} onSearch={onSearch} style={{width: '400px'}}/>
  14. ];
  15. ReactDOM.render(<App />, mountNode);

类别

简单用法

Search 搜索 - 图2

查看源码在线预览

  1. import { Search } from '@alifd/next';
  2. ReactDOM.render(<div>
  3. <h2>Normal</h2>
  4. <Search type="primary" placeholder="primary"/>
  5. <br/> <br/>
  6. <Search type="secondary" placeholder="Secondary"/>
  7. <br/> <br/>
  8. <Search type="normal" placeholder="normal"/>
  9. <br/> <br/>
  10. <div style={{background: '#333', padding: 20}}>
  11. <Search type="dark" placeholder="dark"/>
  12. </div>
  13. <h2>Simple</h2>
  14. <Search type="normal" shape="simple" placeholder="normal"/>
  15. <br/> <br/>
  16. <div style={{background: '#333', padding: 20}}>
  17. <Search shape="simple" type="dark" placeholder="dark"/>
  18. </div>
  19. </div>, mountNode);

大小

通过size进行大小设置,支持large、medium

Search 搜索 - 图3

查看源码在线预览

  1. import { Search } from '@alifd/next';
  2. ReactDOM.render(<div>
  3. <Search
  4. size="large"
  5. defaultValue="large"
  6. searchText="Search"
  7. placeholder="What are you looking for..." />
  8. <br/><br/>
  9. <Search
  10. size="medium"
  11. defaultValue="medium"
  12. searchText="Search"
  13. placeholder="What are you looking for..." />
  14. </div>, mountNode);

下拉框

带下拉框的用法。可以设置onFilterChange事件

Search 搜索 - 图4

查看源码在线预览

  1. import { Search } from '@alifd/next';
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. filter: [
  7. {
  8. label: 'Products',
  9. value: 'Products'
  10. },
  11. {
  12. label: 'Products1',
  13. value: 'Products1'
  14. },
  15. {
  16. label: 'Products2',
  17. value: 'Products2'
  18. },
  19. {
  20. label: 'Products3',
  21. value: 'Products3'
  22. },
  23. {
  24. label: 'Products4',
  25. value: 'Products4'
  26. },
  27. {
  28. label: 'Products5',
  29. value: 'Products5'
  30. },
  31. {
  32. label: 'Products6',
  33. value: 'Products6'
  34. },
  35. {
  36. label: 'Products7',
  37. value: 'Products7'
  38. },
  39. {
  40. label: 'Products8',
  41. value: 'Products8'
  42. },
  43. {
  44. label: 'Products9',
  45. value: 'Products9'
  46. },
  47. {
  48. label: 'Products10',
  49. value: 'Products10'
  50. },
  51. {
  52. label: 'Suppliers',
  53. value: 'Suppliers',
  54. default: true
  55. }
  56. ],
  57. value: ''
  58. };
  59. }
  60. onSearch(value, filterValue) {
  61. console.log(value, filterValue);
  62. }
  63. onChange(value) {
  64. this.setState({
  65. value: value
  66. });
  67. }
  68. // value is filter value,obj is the search value
  69. onFilterChange(value) {
  70. console.log(value);
  71. }
  72. render() {
  73. return (<div>
  74. <Search onChange={this.onChange.bind(this)} onSearch={this.onSearch.bind(this)}
  75. filterProps={{autoWidth: false}}
  76. filter={this.state.filter} onFilterChange={this.onFilterChange.bind(this)}/>
  77. </div>);
  78. }
  79. }
  80. ReactDOM.render(<App/>, mountNode);

联想

Search 搜索 - 图5

查看源码在线预览

  1. import { Search } from '@alifd/next';
  2. const dataSource = [{
  3. label: 'Recent',
  4. value: 'Recent'
  5. }, {
  6. label: 'dress',
  7. value: 'dress'
  8. }, {
  9. label: 'sunglasses',
  10. value: 'sunglasses'
  11. }, {
  12. label: 't-shirt',
  13. value: 't-shirt'
  14. }];
  15. class App extends React.Component {
  16. onSearch(value, filterValue) {
  17. console.log(value, filterValue);
  18. }
  19. onChange(value) {
  20. this.setState({
  21. value: value
  22. });
  23. }
  24. render() {
  25. return (
  26. <Search dataSource={dataSource} onChange={this.onChange.bind(this)}
  27. onSearch={this.onSearch.bind(this)}/>
  28. );
  29. }
  30. }
  31. ReactDOM.render(<App/>, mountNode);

自定义弹层

自定义下拉框内容。

Search 搜索 - 图6

查看源码在线预览

  1. import { Search, Menu, Button } from '@alifd/next';
  2. const menuData = [
  3. {
  4. label: 'Option 1',
  5. value: 'Option 1 Value',
  6. index: '1'
  7. }, {
  8. label: 'Option 2',
  9. value: 'Option 2 Value',
  10. index: '2'
  11. }, {
  12. label: 'Option 3',
  13. value: 'Option 3 Value',
  14. index: '3'
  15. }, {
  16. label: 'Option 4',
  17. value: 'Option 4 Value',
  18. index: '4'
  19. }
  20. ];
  21. class App extends React.Component {
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. visible: false,
  26. value: '111222',
  27. menuData: menuData
  28. };
  29. this.onVisibleChange = this.onVisibleChange.bind(this);
  30. this.onSearch = this.onSearch.bind(this);
  31. this.onChange = this.onChange.bind(this);
  32. this.onFocus = this.onFocus.bind(this);
  33. }
  34. renderMenu() {
  35. const menuData = this.state.menuData;
  36. return (<Menu onSelect={this.onSelect.bind(this)} rtl className="diy-menu" selectMode="single">
  37. <Menu.Group label="Recent" key="xxx">
  38. {menuData.map((item) => {
  39. return (<Menu.Item key={item.value}>
  40. {item.label}
  41. <Button className="diy-menu-button" onClick={this.onDelete.bind(this, item.index)} text>Delete</Button>
  42. </Menu.Item>);
  43. })}
  44. </Menu.Group>
  45. </Menu>);
  46. }
  47. onSearch(value) {
  48. console.log(value);
  49. }
  50. onChange(value) {
  51. this.setState({
  52. visible: value.length > 0,
  53. value: value
  54. });
  55. }
  56. onSelect(selectedKeys) {
  57. this.setState({
  58. visible: false,
  59. value: selectedKeys[0]
  60. });
  61. }
  62. onDelete(index, e) {
  63. e.stopPropagation();
  64. const menuData = this.state.menuData;
  65. const menuDataNew = [];
  66. menuData.forEach(function (item) {
  67. if (item.index !== index) {
  68. menuDataNew.push(item);
  69. }
  70. });
  71. this.setState({
  72. menuData: menuDataNew
  73. });
  74. }
  75. onFocus() {
  76. this.setState({
  77. visible: true
  78. });
  79. }
  80. onVisibleChange() {
  81. this.setState({
  82. visible: false
  83. });
  84. }
  85. render() {
  86. const {visible, value} = this.state;
  87. return (<div style={{width: 700}}>
  88. <Search
  89. onVisibleChange={this.onVisibleChange}
  90. popupContent={this.renderMenu()}
  91. visible={visible}
  92. value={value}
  93. onSearch={this.onSearch}
  94. onChange={this.onChange}
  95. onFocus={this.onFocus}
  96. />
  97. </div>);
  98. }
  99. }
  100. ReactDOM.render(<App/>, mountNode);
  1. .diy-menu{
  2. /*width: 275px*/;
  3. }
  4. .diy-menu .next-menu-item a{
  5. display:none;
  6. float: right;
  7. cursor: pointer;
  8. }
  9. .diy-menu .next-menu-item:hover a{
  10. display:inline-block;
  11. }
  12. .diy-menu .diy-menu-button {
  13. float: right;
  14. }
  15. .diy-menu[dir=rtl] .diy-menu-button {
  16. float: left;
  17. }

无障碍

按下Enter键调用onSearch事件去处理,请参考ARIA and KeyBoard

Search 搜索 - 图7

查看源码在线预览

  1. import { Search } from '@alifd/next';
  2. ReactDOM.render(<div>
  3. <Search key="3" placeholder="Please enter the search content" onSearch={v => console.log(v)} searchText={<span>search</span>} style={{width: '400px'}}/>
  4. </div>, mountNode);

相关区块

Search 搜索 - 图8

暂无相关区块