| | |
| | | import React, {Component} from 'react' |
| | | import PropTypes from 'prop-types' |
| | | import { Modal, Transfer, Tree } from 'antd' |
| | | import { fromJS } from 'immutable' |
| | | import { Modal, Transfer, Tree, Input, notification } from 'antd' |
| | | import { FormOutlined } from '@ant-design/icons' |
| | | |
| | | const { TreeNode } = Tree |
| | | const { Search } = Input |
| | | |
| | | const isChecked = (selectedKeys, eventKey) => { |
| | | return selectedKeys.indexOf(eventKey) !== -1 |
| | |
| | | |
| | | state = { |
| | | visible: false, |
| | | targetKeys: ['sifuweoifuweifuo'] |
| | | targetKeys: [], |
| | | users: [] |
| | | } |
| | | |
| | | onChange = targetKeys => { |
| | | this.setState({ targetKeys }) |
| | | } |
| | | |
| | | render() { |
| | | search = (val) => { |
| | | const { orgs } = this.props |
| | | const { visible, targetKeys } = this.state |
| | | |
| | | let _orgs = fromJS(orgs).toJS() |
| | | let _orgs_ = [] |
| | | if (val) { |
| | | let getOrg = (ors) => { |
| | | return ors.filter(or => { |
| | | if (or.key.indexOf(val) > -1 || or.title.indexOf(val) > -1) { |
| | | return true |
| | | } else if (!or.children) { |
| | | return false |
| | | } |
| | | |
| | | or.children = getOrg(or.children) |
| | | |
| | | return or.children.length > 0 |
| | | }) |
| | | } |
| | | |
| | | _orgs_ = getOrg(_orgs) |
| | | } else { |
| | | _orgs_ = _orgs |
| | | } |
| | | |
| | | this.setState({orgs: _orgs_}) |
| | | } |
| | | |
| | | trigger = () => { |
| | | const { orgs, value, title } = this.props |
| | | |
| | | let userIds = [] |
| | | let users = {} |
| | | |
| | | let getUsers = (ors) => { |
| | | ors.forEach(or => { |
| | | if (or.children) { |
| | | getUsers(or.children) |
| | | } else { |
| | | users[or.worker_id] = or |
| | | userIds.push(or.worker_id) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | getUsers(orgs) |
| | | |
| | | let targetKeys = [] |
| | | let lost_works = [] |
| | | value.forEach(user => { |
| | | if (userIds.includes(user.worker_id)) { |
| | | targetKeys.push(user.worker_id) |
| | | } else { |
| | | lost_works.push(user) |
| | | } |
| | | }) |
| | | |
| | | if (lost_works.length) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: `原${title} ${lost_works.map(item => item.workername).join('、')} 不存在,请重新添加。`, |
| | | duration: 10 |
| | | }) |
| | | } |
| | | |
| | | this.setState({visible: true, orgs: fromJS(orgs).toJS(), targetKeys, users}) |
| | | } |
| | | |
| | | confirm = () => { |
| | | const { targetKeys, users } = this.state |
| | | |
| | | let value = [] |
| | | targetKeys.forEach(key => { |
| | | if (!users[key]) return |
| | | |
| | | let user = fromJS(users[key]).toJS() |
| | | |
| | | delete user.key |
| | | delete user.title |
| | | |
| | | value.push(user) |
| | | }) |
| | | |
| | | this.setState({visible: false}) |
| | | this.props.onChange(value) |
| | | } |
| | | |
| | | render() { |
| | | const { value } = this.props |
| | | const { orgs, visible, targetKeys } = this.state |
| | | |
| | | return ( |
| | | <> |
| | | <div className="member-input">0人<FormOutlined onClick={() => this.setState({visible: true})} /></div> |
| | | <div className="member-input">{value.length}人<FormOutlined onClick={this.trigger} /></div> |
| | | <Modal |
| | | wrapClassName="member-modal" |
| | | title="选择人员" |
| | |
| | | onCancel={() => this.setState({visible: false})} |
| | | destroyOnClose |
| | | > |
| | | <Search onSearch={this.search}/> |
| | | <TreeTransfer dataSource={orgs} targetKeys={targetKeys} onChange={this.onChange} /> |
| | | </Modal> |
| | | </> |