index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { Component } from 'react';
  2. import { TreeSelect as ATreeSelect } from 'antd';
  3. import { search } from '../../services/Tools';
  4. import './index.less';
  5. export default class TreeSelect extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.nodeMap = {};
  9. this.closeKey = null;
  10. this.Select = null;
  11. this.ignore = props.ignore;
  12. this.treeData = this.getTreeData(props.treeData);
  13. this.state = {
  14. open: false,
  15. value: props.value,
  16. };
  17. if (props.value && props.initData) {
  18. props.initData([props.value]).then(list => {
  19. this.makeValue(list);
  20. this.setState({});
  21. });
  22. }
  23. }
  24. getNode(node) {
  25. const list = [];
  26. if (node.children) {
  27. node.children.forEach(n => {
  28. if (this.ignore === n.key) return;
  29. list.push(this.getNode(n));
  30. });
  31. }
  32. this.nodeMap[node.key] = {
  33. key: node.key,
  34. value: node.value === undefined ? node.key : node.value,
  35. title: node.title,
  36. children: list,
  37. };
  38. return this.nodeMap[node.key];
  39. }
  40. getTreeData(treeData) {
  41. if (!treeData) return [];
  42. const list = [];
  43. treeData.forEach(data => {
  44. if (this.ignore === data.key) return;
  45. list.push(this.getNode(data));
  46. });
  47. return list;
  48. }
  49. makeValue(list) {
  50. list.forEach(data => {
  51. if (!this.nodeMap[data.pKey]) {
  52. this.treeData.push(this.getNode(data));
  53. } else if (search(this.nodeMap[data.pKey].children, 'key', data.key) === -1) {
  54. this.nodeMap[data.pKey].children.push(this.getNode(data));
  55. }
  56. });
  57. }
  58. onSelect(value) {
  59. if (this.closeKey) clearTimeout(this.closeKey);
  60. this.Select.focus();
  61. if (this.props.loadData && !this.nodeMap[value].loaded) {
  62. this.props.loadData(value).then(list => {
  63. this.makeValue(list);
  64. this.nodeMap[value].loaded = true;
  65. this.setState({});
  66. });
  67. }
  68. return false;
  69. }
  70. onChange(value) {
  71. if (this.props.isNode) {
  72. if (this.props.isNode(value)) {
  73. this.props.onChange(value);
  74. this.updateValue(value);
  75. }
  76. } else {
  77. this.props.onChange(value);
  78. this.updateValue(value);
  79. }
  80. }
  81. updateValue(value) {
  82. this.setState({ value });
  83. this.close();
  84. }
  85. close() {
  86. this.setState({ open: false });
  87. }
  88. onDropdownVisibleChange(open) {
  89. if (open) {
  90. this.setState({ open: true });
  91. }
  92. }
  93. onBlur() {
  94. this.closeKey = setTimeout(() => {
  95. this.setState({ open: false });
  96. }, 200);
  97. }
  98. render() {
  99. return (
  100. <ATreeSelect
  101. {...this.props}
  102. className="tree-select-layout"
  103. ref={ref => {
  104. this.Select = ref;
  105. }}
  106. open={this.state.open}
  107. disabled={!!this.props.disabled}
  108. placeholder={this.props.placeholder}
  109. treeDefaultExpandAll
  110. treeData={this.treeData}
  111. value={this.state.value}
  112. onSelect={value => this.onSelect(value)}
  113. onChange={value => this.onChange(value)}
  114. onDropdownVisibleChange={e => this.onDropdownVisibleChange(e)}
  115. onBlur={e => this.onBlur(e)}
  116. />
  117. );
  118. }
  119. }