mock平台

UsernameAutoComplete.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React, { PureComponent as Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Select } from 'antd';
  4. import axios from 'axios';
  5. const Option = Select.Option;
  6. /**
  7. * 用户名输入框自动完成组件
  8. *
  9. * @component UsernameAutoComplete
  10. * @examplelanguage js
  11. *
  12. * * 用户名输入框自动完成组件
  13. * * 用户名输入框自动完成组件
  14. *
  15. *s
  16. */
  17. /**
  18. * 获取自动输入的用户信息
  19. *
  20. * 获取子组件state
  21. * @property callbackState
  22. * @type function
  23. * @description 类型提示:支持数组传值;也支持用函数格式化字符串:函数有两个参数(scale, index);
  24. * 受控属性:滑块滑到某一刻度时所展示的刻度文本信息。如果不需要标签,请将该属性设置为 [] 空列表来覆盖默认转换函数。
  25. * @returns {object} {uid: xxx, username: xxx}
  26. * @examplelanguage js
  27. * @example
  28. * onUserSelect(childState) {
  29. * this.setState({
  30. * uid: childState.uid,
  31. * username: childState.username
  32. * })
  33. * }
  34. *
  35. */
  36. class UsernameAutoComplete extends Component {
  37. constructor(props) {
  38. super(props);
  39. // this.lastFetchId = 0;
  40. // this.fetchUser = debounce(this.fetchUser, 800);
  41. }
  42. state = {
  43. dataSource: [],
  44. fetching: false
  45. };
  46. static propTypes = {
  47. callbackState: PropTypes.func
  48. };
  49. // 搜索回调
  50. handleSearch = value => {
  51. const params = { q: value };
  52. // this.lastFetchId += 1;
  53. // const fetchId = this.lastFetchId;
  54. this.setState({ fetching: true });
  55. axios.get('/api/user/search', { params }).then(data => {
  56. // if (fetchId !== this.lastFetchId) { // for fetch callback order
  57. // return;
  58. // }
  59. const userList = [];
  60. data = data.data.data;
  61. if (data) {
  62. data.forEach(v =>
  63. userList.push({
  64. username: v.username,
  65. id: v.uid
  66. })
  67. );
  68. // 取回搜索值后,设置 dataSource
  69. this.setState({
  70. dataSource: userList
  71. });
  72. }
  73. });
  74. };
  75. // 选中候选词时
  76. handleChange = value => {
  77. this.setState({
  78. dataSource: [],
  79. // value,
  80. fetching: false
  81. });
  82. this.props.callbackState(value);
  83. };
  84. render() {
  85. let { dataSource, fetching } = this.state;
  86. const children = dataSource.map((item, index) => (
  87. <Option key={index} value={'' + item.id}>
  88. {item.username}
  89. </Option>
  90. ));
  91. // if (!children.length) {
  92. // fetching = false;
  93. // }
  94. return (
  95. <Select
  96. mode="multiple"
  97. style={{ width: '100%' }}
  98. placeholder="请输入用户名"
  99. filterOption={false}
  100. optionLabelProp="children"
  101. notFoundContent={fetching ? <span style={{ color: 'red' }}> 当前用户不存在</span> : null}
  102. onSearch={this.handleSearch}
  103. onChange={this.handleChange}
  104. >
  105. {children}
  106. </Select>
  107. );
  108. }
  109. }
  110. export default UsernameAutoComplete;