import React, { Component } from 'react'; import './index.less'; import { Icon } from 'antd'; import Select from '../Select'; import CheckboxItem from '../CheckboxItem'; import { Button } from '../Button'; import { Icon as GIcon } from '../Icon'; export default class UserAction extends Component { constructor(props) { super(props); this.state = { showInput: false, searchText: '' }; } onAction(key) { const { onAction } = this.props; if (onAction) onAction(key); } onAll(checked) { const { onAll } = this.props; if (onAll) onAll(checked); } onSearch(value) { this.setState({ searchText: value }); } onFilter(key, value) { const { filterMap = {}, onFilter } = this.props; filterMap[key] = value; if (onFilter) onFilter(filterMap); } onSort(key, value) { const { onSort, sortMap = {} } = this.props; sortMap[key] = value; if (onSort) onSort(sortMap); } render() { const { allCheckbox, allChecked, help, btnList = [], search, selectList = [], sortList = [], sortMap = [], right, left, } = this.props; const { showInput, searchText } = this.state; return ( <div className="user-action"> {left && <div className="left">{left}</div>} {allCheckbox && ( <div className="all"> <CheckboxItem theme="white" checked={allChecked} onClick={value => this.onAll(value)} /> 全选 </div> )} <div hidden={btnList.length === 0} className="button-list"> {btnList.map(btn => { return ( <Button radius size="small" onClick={() => this.onAction(btn.key)}> {btn.title} </Button> ); })} {help && ( <div className="help"> <Icon type="question-circle" theme="filled" onClick={() => this.onAction('help')} /> </div> )} </div> <div hidden={selectList.length === 0 && sortList.length === 0} className="item-list"> {selectList.map(item => { return ( <div className={`item select-item ${item.right ? 'right' : ''}`}> <span>{item.label}</span> {item.select && this.renderSelect(item)} {item.children && item.children.map(child => this.renderSelect(child))} </div> ); })} {sortList.map(item => { return ( <div className={`item sort-item ${item.right ? 'right' : ''}`}> {item.label} {sortMap[item.key] ? ( <GIcon active name={`arrow-${sortMap[item.key] === 'asc' ? 'up' : 'down'}`} onClick={() => this.onSort(item.key, sortMap[item.key] === 'asc' ? 'desc' : '')} /> ) : ( <GIcon name="arrow-up" onClick={() => this.onSort(item.key, 'asc')} /> )} </div> ); })} </div> {search && ( <div className="search"> {showInput && ( <input className="search-input" placeholder="请输入您想搜索的内容" value={searchText} onChange={e => this.onSearch(e.target.value)} /> )} {!showInput && <Icon type="search" onClick={() => this.setState({ showInput: true })} />} </div> )} {right && ( <div className={`right ${ btnList.length === 0 && selectList.length === 0 && sortList.length === 0 ? 'only' : '' }`} > {right} </div> )} </div> ); } renderSelect(item) { const { filterMap = {} } = this.props; return ( <Select size="small" theme="default" value={filterMap[item.key]} placeholder={item.placeholder} list={item.be ? item.selectMap[filterMap[item.be]] || [] : item.select} onChange={({ key }) => this.onFilter(item.key, key)} /> ); } }