UNPKG

chayns-components

Version:

A set of beautiful React components for developing chayns® applications.

65 lines (64 loc) 1.57 kB
import classnames from 'clsx'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import AbstractList from '../AbstractList'; import ExpandableContext from './ExpandableContext'; export default class ExpandableList extends Component { constructor(props) { super(props); this.providerState = { onOpen: this.onOpen.bind(this), onClose: this.onClose.bind(this), onToggle: this.onToggle.bind(this) }; this.state = { open: [] }; } onOpen(id) { this.changeOpen(id); } onClose() { this.changeOpen(null); } onToggle(id) { const { open } = this.state; if (open.indexOf(id) === -1) { this.onOpen(id); } else { this.onClose(id); } } changeOpen(id) { this.providerState = { ...this.providerState, open: [id] }; this.setState({ open: [id] }); } render() { const { children, className } = this.props; return /*#__PURE__*/React.createElement(AbstractList, { className: classnames('list--expandable', className) }, /*#__PURE__*/React.createElement(ExpandableContext.Provider, { value: this.providerState }, children)); } } ExpandableList.Context = ExpandableContext; ExpandableList.propTypes = { children: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)]).isRequired, className: PropTypes.string }; ExpandableList.defaultProps = { className: null }; ExpandableList.displayName = 'ExpandableList'; //# sourceMappingURL=ExpandableList.js.map