selectUserBox.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * 人员选择器
  3. */
  4. import React from 'react'
  5. import { Modal, Row, Col, Select, Spin } from 'antd'
  6. import * as service from '../../services/index'
  7. import URLS from '../../constants/url'
  8. const SelectOption = Select.Option
  9. class AddGroupMemberBox extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. selectedUser: null,
  14. userData: [],
  15. fetching: false
  16. }
  17. }
  18. fetchUserData = (keyword) => {
  19. this.setState({ userData: [], fetching: true }, () => {
  20. const body = keyword || '';
  21. service.fetch({
  22. url: URLS.USER_QUERY,
  23. body: body,
  24. }).then(r => {
  25. if(!r.err && r.data.code > 0) {
  26. return r;
  27. }else {
  28. let obj = {};
  29. throw obj;
  30. }
  31. }).then(r => {
  32. console.log('获得用户数据', body, r);
  33. const resData = r.data.data || [];
  34. this.setState({
  35. userData: resData.map(d => ({
  36. code: d.id,
  37. name: d.name,
  38. account: d.userName,
  39. password: d.passWord,
  40. role: d.role,
  41. department: d.department,
  42. post: d.post,
  43. })),
  44. fetching: false
  45. });
  46. }).catch(ex => {
  47. this.setState({
  48. userData: [],
  49. fetching: false
  50. });
  51. console.error('fetch error', ex);
  52. });
  53. });
  54. }
  55. render() {
  56. const { visibleBox, title, hideBox, okHandler, multiple } = this.props;
  57. const { userData, fetching } = this.state;
  58. return (
  59. <Modal
  60. className='addmember-box'
  61. title={title}
  62. visible={visibleBox}
  63. onOk={() => {
  64. okHandler(this.state.selectedUser);
  65. hideBox();
  66. }}
  67. onCancel={hideBox}
  68. maskClosable={false}
  69. destroyOnClose={true}
  70. >
  71. <Row type='flex'>
  72. <Col span={24}>
  73. <Select
  74. style={{ width: '100%' }}
  75. mode={ multiple ? 'multiple' : 'sigle'}
  76. showSearch
  77. filterOption={false}
  78. labelInValue={true}
  79. notFoundContent={fetching ? <Spin size="small" /> : '无'}
  80. onSearch={(value) => {
  81. const timeout = this.state.timeout;
  82. timeout && window.clearTimeout(timeout);
  83. this.setState({
  84. timeout: window.setTimeout(() => {
  85. this.fetchUserData(value)
  86. }, 500)
  87. });
  88. }}
  89. onFocus={() => {
  90. this.fetchUserData('');
  91. }}
  92. onChange={(value) => {
  93. this.setState({
  94. selectedUser: {
  95. code: value.key,
  96. name: value.label
  97. }
  98. });
  99. }}
  100. >
  101. { userData.map((s, i) => {
  102. return <SelectOption key={i} value={s.code}>{s.name}</SelectOption>
  103. }) }
  104. </Select>
  105. </Col>
  106. </Row>
  107. </Modal>
  108. );
  109. }
  110. }
  111. export default AddGroupMemberBox;