Search 搜索

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

安装方法

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

开发指南

何时使用

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

API

搜索

参数说明类型默认值
prefix样式前缀String'next-'
size大小,可选 primary/secondary 可选择 medium/large; normal 可选择 small/mediumString'medium'
filter前置下拉框,default为默认选中项 [{text:'Products', value:'Products',default: true},{text:'Suppliers',valuse:'Suppliers'}]Array[]
hasIcon搜索按钮图标Booleantrue
searchText搜索按钮文案String'Search'
combox定制下拉框,适合业务特殊定制ReactNodefalse
inputWidth搜索框宽度String/Number'auto'
overlayVisible与combox配合使用,控制定制下拉框的展现Boolean-
dataSource下拉提示框:历史搜索/搜索建议 [{label:'',value:'',disabled:true}]Array[]
type类型可选值:'primary', 'secondary', 'normal'Enum'primary'
value数值String/Number-
defaultValue搜索框默认值 (不适用于Combox)String-
className样式名称String-
placeholder默认提示String-
onInputFocusinput获取焦点的时候触发的回调签名:Function() => voidFunctionfunction() { }
onInputBlurinput失去焦点的时候触发的回调签名:Function() => voidFunctionfunction() { }
onSearch点击搜索按钮触发的回调签名:Function(object: Object) => void参数:object: {Object} {filter:'',key:''}Functionfunction() { }
onChange输入关键字时的回掉签名:Function() => voidFunctionfunction() { }
onFilterChangeFilter改变时的回掉(filterValue)签名:Function() => voidFunctionfunction() { }
autoWidth搜索框100%自适应父容器Booleanfalse
filterAutoWidthfilter的下拉菜单是否与选择器对齐Booleantrue
style自定义样式Object-
container指定渲染combox的容器ReactNode-

代码示例

自适应宽度

自适应宽度

Search 搜索 - 图1

查看源码在线预览

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

自定义下拉框内容

自定义下拉框内容。

Search 搜索 - 图2

查看源码在线预览

  1. import { Search, Menu } from "@icedesign/base";
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. overlayVisible: false,
  7. value: "111222",
  8. menuData: [
  9. {
  10. text: "Option 1",
  11. value: "Option 1 Value",
  12. index: "1"
  13. },
  14. {
  15. text: "Option 2",
  16. value: "Option 2 Value",
  17. index: "2"
  18. },
  19. {
  20. text: "Option 3",
  21. value: "Option 3 Value",
  22. index: "3"
  23. },
  24. {
  25. text: "Option 4",
  26. value: "Option 4 Value",
  27. index: "4"
  28. }
  29. ],
  30. filter: [
  31. {
  32. text: "Products",
  33. value: "Products"
  34. },
  35. {
  36. text: "Products1",
  37. value: "Products1"
  38. },
  39. {
  40. text: "Products2",
  41. value: "Products2"
  42. },
  43. {
  44. text: "Products3",
  45. value: "Products3"
  46. },
  47. {
  48. text: "Products4",
  49. value: "Products4"
  50. },
  51. {
  52. text: "Products5",
  53. value: "Products5"
  54. },
  55. {
  56. text: "Products6",
  57. value: "Products6"
  58. },
  59. {
  60. text: "Products7",
  61. value: "Products7"
  62. },
  63. {
  64. text: "Products8",
  65. value: "Products8"
  66. },
  67. {
  68. text: "Products9",
  69. value: "Products9"
  70. },
  71. {
  72. text: "Products10",
  73. value: "Products10"
  74. },
  75. {
  76. text: "Suppliers",
  77. value: "Suppliers",
  78. default: true
  79. }
  80. ]
  81. };
  82. }
  83. renderMenu() {
  84. const menuData = this.state.menuData;
  85. return (
  86. <Menu onClick={this.onClick.bind(this)} className="diy-menu">
  87. <Menu.Group label="Recent" key="xxx">
  88. {menuData.map(item => {
  89. return (
  90. <Menu.Item key={item.value}>
  91. {" "}
  92. {item.text}{" "}
  93. <a
  94. onClick={this.onDelete.bind(this)}
  95. target="_self"
  96. data-index={item.index}
  97. >
  98. Delete
  99. </a>{" "}
  100. </Menu.Item>
  101. );
  102. })}
  103. </Menu.Group>
  104. </Menu>
  105. );
  106. }
  107. onSearch(value) {
  108. console.log(value);
  109. }
  110. onChange(value) {
  111. this.setState({
  112. overlayVisible: value.length > 0,
  113. value: value
  114. });
  115. }
  116. onClick(selectedKeys) {
  117. if (typeof selectedKeys === "string") {
  118. this.setState({
  119. overlayVisible: false,
  120. value: selectedKeys
  121. });
  122. }
  123. }
  124. onDelete(e) {
  125. e.stopPropagation();
  126. const index = e.currentTarget.getAttribute("data-index");
  127. const menuData = this.state.menuData;
  128. const menuDataNew = [];
  129. menuData.forEach(function(item) {
  130. if (item.index !== index) {
  131. menuDataNew.push(item);
  132. }
  133. });
  134. this.setState({
  135. menuData: menuDataNew
  136. });
  137. }
  138. onInputFocus() {
  139. this.setState({
  140. overlayVisible: true
  141. });
  142. }
  143. render() {
  144. const overlayVisible = this.state.overlayVisible;
  145. const value = this.state.value;
  146. return (
  147. <div style={{ width: 700 }}>
  148. <Search
  149. placeholder="123"
  150. autoWidth
  151. combox={<div>{this.renderMenu()}</div>}
  152. overlayVisible={overlayVisible}
  153. value={value}
  154. onSearch={this.onSearch.bind(this)}
  155. onChange={this.onChange.bind(this)}
  156. onInputFocus={this.onInputFocus.bind(this)}
  157. />
  158. </div>
  159. );
  160. }
  161. }
  162. 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. }

普通search的事件以及受控方式

onSearch, onChange, onInputBlur, onInputFocus事件。

Search 搜索 - 图3

查看源码在线预览

  1. import { Search, Button } from "@icedesign/base";
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. dataSource: [
  7. {
  8. label: "Recent",
  9. value: "Recent",
  10. disabled: true
  11. },
  12. {
  13. label: "连衣裙",
  14. value: "连衣裙",
  15. disabled: false
  16. },
  17. {
  18. label: "墨镜",
  19. value: "墨镜",
  20. disabled: false
  21. },
  22. {
  23. label: "短袖",
  24. value: "短袖",
  25. disabled: false
  26. }
  27. ],
  28. value: ""
  29. };
  30. }
  31. onSearchClick() {
  32. let time = new Date();
  33. time = time.getTime();
  34. this.setState({
  35. value: time
  36. });
  37. }
  38. onClearClick() {
  39. this.setState({
  40. value: ""
  41. });
  42. }
  43. // 点击search按钮和在选中项上回车时触发
  44. // 参数为obj:
  45. // {
  46. // filter: [],
  47. // key: xx
  48. // }
  49. onSearch(obj) {
  50. console.log(obj);
  51. }
  52. // input 输入时触发
  53. onChange(value) {
  54. this.setState({
  55. value
  56. });
  57. }
  58. onInputBlur(e, obj) {
  59. console.log(e, obj);
  60. }
  61. onInputFocus(e, clickByUser, state) {
  62. console.log(e, clickByUser, state);
  63. }
  64. render() {
  65. return (
  66. <div>
  67. <div>
  68. <Button onClick={this.onSearchClick.bind(this)}>
  69. 改变搜索关键字
  70. </Button>
  71. <Button onClick={this.onClearClick.bind(this)}>清除关键字</Button>
  72. </div>
  73. <br />
  74. <Search
  75. inputWidth={300}
  76. value={this.state.value}
  77. onSearch={this.onSearch.bind(this)}
  78. onChange={this.onChange.bind(this)}
  79. onInputBlur={this.onInputBlur.bind(this)}
  80. onInputFocus={this.onInputFocus.bind(this)}
  81. dataSource={this.state.dataSource}
  82. placeholder="What are you looking for..."
  83. name="textName"
  84. />
  85. <br />
  86. </div>
  87. );
  88. }
  89. }
  90. ReactDOM.render(<App />, mountNode);

下拉框

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

Search 搜索 - 图4

查看源码在线预览

  1. import { Search, Button } from "@icedesign/base";
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. filter: [
  7. {
  8. text: "Products",
  9. value: "Products"
  10. },
  11. {
  12. text: "Products1",
  13. value: "Products1"
  14. },
  15. {
  16. text: "Products2",
  17. value: "Products2"
  18. },
  19. {
  20. text: "Products3",
  21. value: "Products3"
  22. },
  23. {
  24. text: "Products4",
  25. value: "Products4"
  26. },
  27. {
  28. text: "Products5",
  29. value: "Products5"
  30. },
  31. {
  32. text: "Products6",
  33. value: "Products6"
  34. },
  35. {
  36. text: "Products7",
  37. value: "Products7"
  38. },
  39. {
  40. text: "Products8",
  41. value: "Products8"
  42. },
  43. {
  44. text: "Products9",
  45. value: "Products9"
  46. },
  47. {
  48. text: "Products10",
  49. value: "Products10"
  50. },
  51. {
  52. text: "Suppliers",
  53. value: "Suppliers",
  54. default: true
  55. }
  56. ],
  57. value: ""
  58. };
  59. }
  60. onSearch(value) {
  61. console.log(value);
  62. }
  63. onChange(value) {
  64. console.log(`input is: ${value}`);
  65. this.setState({
  66. value: value
  67. });
  68. }
  69. // value为filter的值,obj为search的全量值
  70. onFilterChange(value, obj) {
  71. console.log(`filter is: ${value}`);
  72. console.log("fullData: ", obj);
  73. }
  74. onButtonClick() {
  75. this.setState({
  76. filter: [
  77. {
  78. text: "111111111111111111",
  79. value: "111111111111111111"
  80. },
  81. {
  82. text: "2222222",
  83. value: "22222222",
  84. default: true
  85. }
  86. ]
  87. });
  88. }
  89. render() {
  90. return (
  91. <div>
  92. <div>
  93. <Button onClick={this.onButtonClick.bind(this)}>修改filter</Button>
  94. </div>
  95. <br />
  96. <Search
  97. onChange={this.onChange.bind(this)}
  98. onSearch={this.onSearch.bind(this)}
  99. filter={this.state.filter}
  100. onFilterChange={this.onFilterChange.bind(this)}
  101. />
  102. <br />
  103. <br />
  104. <Search
  105. size="large"
  106. onChange={this.onChange.bind(this)}
  107. onSearch={this.onSearch.bind(this)}
  108. filter={this.state.filter}
  109. onFilterChange={this.onFilterChange.bind(this)}
  110. />
  111. <br />
  112. </div>
  113. );
  114. }
  115. }
  116. ReactDOM.render(<App />, mountNode);

size

【注意】type为primary和secondary的时候size只能为'medium'和'large',type为normal是size只能为'medium'和'small'.type 切换,展示出不同的样式风格。size进行大小设置 .

Search 搜索 - 图5

查看源码在线预览

  1. import { Search } from "@icedesign/base";
  2. class App extends React.Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = {
  6. size: "medium",
  7. normalSize: "small"
  8. };
  9. }
  10. onButtonClick() {
  11. this.setState({
  12. size: this.state.size === "medium" ? "large" : "medium",
  13. normalSize: this.state.normalSize === "medium" ? "small" : "medium"
  14. });
  15. }
  16. render() {
  17. return (
  18. <div>
  19. <h2>Primary</h2>
  20. <Search
  21. size="large"
  22. inputWidth={300}
  23. defaultValue="large"
  24. placeholder="What are you looking for..."
  25. />
  26. <br />
  27. <Search
  28. size="medium"
  29. inputWidth={300}
  30. defaultValue="medium"
  31. placeholder="What are you looking for..."
  32. />
  33. <h2>Secondary</h2>
  34. <Search
  35. size="large"
  36. inputWidth={300}
  37. defaultValue="large"
  38. type="secondary"
  39. placeholder="What are you looking for..."
  40. />
  41. <br />
  42. <Search
  43. inputWidth={300}
  44. size="medium"
  45. defaultValue="medium"
  46. type="secondary"
  47. placeholder="What are you looking for..."
  48. />
  49. <h2>normal</h2>
  50. <Search
  51. size="medium"
  52. type="normal"
  53. inputWidth={300}
  54. defaultValue="medium"
  55. placeholder="What are you looking for..."
  56. />
  57. <br />
  58. <Search
  59. size="small"
  60. type="normal"
  61. inputWidth={300}
  62. defaultValue="small"
  63. placeholder="What are you looking for..."
  64. />
  65. </div>
  66. );
  67. }
  68. }
  69. ReactDOM.render(<App />, mountNode);

type

简单用法

Search 搜索 - 图6

查看源码在线预览

  1. import { Search } from "@icedesign/base";
  2. ReactDOM.render(
  3. <div>
  4. <Search type="primary" inputWidth={300} placeholder="primary" />
  5. <br />
  6. <Search type="secondary" inputWidth={300} placeholder="Secondary" />
  7. <br />
  8. <Search type="normal" inputWidth={300} placeholder="normal" />
  9. </div>,
  10. mountNode
  11. );

相关区块

Search 搜索 - 图7

暂无相关区块