import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import {KEYCODES} from './constants'; import {onEventCapture} from './utils'; import {IconButton} from './'; /** * @ngdoc react * @name CollapseBox * @description CollapseBox which has a closed and open view of an item */ export class CollapseBox extends React.Component { static propTypes: any; static defaultProps: any; dom: any; constructor(props) { super(props); this.state = {isOpen: this.props.isOpen}; this.scrollInView = this.scrollInView.bind(this); this.handleOpenClick = this.handleOpenClick.bind(this); this.openBox = this.openBox.bind(this); this.closeBox = this.closeBox.bind(this); this.handleKeyDown = this.handleKeyDown.bind(this); this.dom = {node: null}; } componentWillReceiveProps(nextProps) { if (this.props.onOpen) { this.setState({isOpen: nextProps.isOpen}); } } handleKeyDown(event) { if (event.keyCode === KEYCODES.ENTER) { onEventCapture(event); // If we closed it by keydown, keep focus to show the tab route if (this.state.isOpen) { this.closeBox(); this.dom.node.focus(); } else { this.openBox(); } } } openBox() { if (this.props.noOpen || this.state.isOpen) { return; } this.setState({isOpen: true}); if (this.props.onOpen) { this.props.onOpen(); } } closeBox() { if (this.state.isOpen) { this.setState({isOpen: false}); if (this.props.onClose) { this.props.onClose(); } } } handleOpenClick() { if (this.props.onClick) { this.props.onClick(); } this.openBox(); } scrollInView() { if (this.props.scrollInView && (this.state.isOpen || this.props.noOpen) && this.dom.node) { this.dom.node.scrollIntoView(); // When just opened, lose focus to remove greyed background due to // initial collapsed view this.dom.node.blur(); } } componentDidMount() { // Upon first rendering, if the box is open then scroll it into view this.scrollInView(); } componentDidUpdate(prevProps, prevState) { // Scroll into view only upon first opening if (prevState.isOpen !== this.state.isOpen || (this.props.forceScroll && this.props.forceScroll !== prevProps.forceScroll)) { this.scrollInView(); } } render() { return (
this.dom.node = node} onClick={this.handleOpenClick} > {this.state.isOpen && (
{this.props.tools}
{this.props.openItemTopBar && (
{this.props.openItemTopBar}
)} {this.props.openItem}
) || (
{this.props.collapsedItem}
)}
); } } CollapseBox.propTypes = { collapsedItem: PropTypes.node.isRequired, openItem: PropTypes.node.isRequired, openItemTopBar: PropTypes.node, tools: PropTypes.node, isOpen: PropTypes.bool, scrollInView: PropTypes.bool, invalid: PropTypes.bool, tabEnabled: PropTypes.bool, noOpen: PropTypes.bool, onOpen: PropTypes.func, onClose: PropTypes.func, forceScroll: PropTypes.bool, onClick: PropTypes.func, }; CollapseBox.defaultProps = { isOpen: false, scrollInView: false, invalid: false, };