|
| 1 | +import React from 'react'; |
| 2 | +import { DndProvider } from 'react-dnd' |
| 3 | +import HTML5Backend from 'react-dnd-html5-backend' |
| 4 | +import ReactDOM from 'react-dom'; |
| 5 | + |
| 6 | +import Button from 'components/Button/Button.react'; |
| 7 | +import ColumnConfigurationItem from 'components/ColumnsConfiguration/ColumnConfigurationItem.react'; |
| 8 | +import styles from 'components/ColumnsConfiguration/ColumnsConfiguration.scss'; |
| 9 | +import Icon from 'components/Icon/Icon.react'; |
| 10 | +import Popover from 'components/Popover/Popover.react'; |
| 11 | +import Position from 'lib/Position'; |
| 12 | + |
| 13 | +export default class ColumnsConfiguration extends React.Component { |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + |
| 17 | + this.state = { |
| 18 | + open: false |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + componentDidMount() { |
| 23 | + this.node = ReactDOM.findDOMNode(this); |
| 24 | + } |
| 25 | + |
| 26 | + componentWillReceiveProps(props) { |
| 27 | + if (props.schema !== this.props.schema) { |
| 28 | + this.setState({ |
| 29 | + open: false |
| 30 | + }); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + toggle() { |
| 35 | + this.setState({ |
| 36 | + open: !this.state.open |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + showAll() { |
| 41 | + this.props.handleColumnsOrder(this.props.order.map(order => ({ ...order, visible: true }))); |
| 42 | + } |
| 43 | + |
| 44 | + hideAll() { |
| 45 | + this.props.handleColumnsOrder(this.props.order.map(order => ({ ...order, visible: false }))); |
| 46 | + } |
| 47 | + |
| 48 | + render() { |
| 49 | + const { handleColumnDragDrop, handleColumnsOrder, order } = this.props; |
| 50 | + const [ title, entry ] = [styles.title, styles.entry ].map(className => ( |
| 51 | + <div className={className} onClick={this.toggle.bind(this)}> |
| 52 | + <Icon name='manage-columns' width={14} height={14} /> |
| 53 | + <span>Manage Columns</span> |
| 54 | + </div> |
| 55 | + )); |
| 56 | + |
| 57 | + let popover = null; |
| 58 | + if (this.state.open) { |
| 59 | + popover = ( |
| 60 | + <Popover fixed={true} position={Position.inDocument(this.node)} onExternalClick={this.toggle.bind(this)}> |
| 61 | + <div className={styles.popover}> |
| 62 | + {title} |
| 63 | + <div className={styles.body}> |
| 64 | + <div className={styles.columnConfigContainer}> |
| 65 | + <DndProvider backend={HTML5Backend}> |
| 66 | + {order.map(({ name, visible, ...rest }, index) => { |
| 67 | + return <ColumnConfigurationItem |
| 68 | + key={index} |
| 69 | + index={index} |
| 70 | + name={name} |
| 71 | + visible={visible} |
| 72 | + onChangeVisible={visible => { |
| 73 | + const updatedOrder = [...order]; |
| 74 | + updatedOrder[index] = { |
| 75 | + ...rest, |
| 76 | + name, |
| 77 | + visible |
| 78 | + }; |
| 79 | + handleColumnsOrder(updatedOrder); |
| 80 | + }} |
| 81 | + handleColumnDragDrop={handleColumnDragDrop} /> |
| 82 | + })} |
| 83 | + </DndProvider> |
| 84 | + </div> |
| 85 | + <div className={styles.footer}> |
| 86 | + <Button |
| 87 | + color='white' |
| 88 | + value='Hide All' |
| 89 | + width='85px' |
| 90 | + onClick={this.hideAll.bind(this)} /> |
| 91 | + <Button |
| 92 | + color='white' |
| 93 | + value='Show all' |
| 94 | + width='85px' |
| 95 | + onClick={this.showAll.bind(this)} /> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + </Popover> |
| 100 | + ); |
| 101 | + } |
| 102 | + return ( |
| 103 | + <> |
| 104 | + {entry} |
| 105 | + {popover} |
| 106 | + </> |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
0 commit comments