UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

213 lines (191 loc) 6.58 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _toArray from "@babel/runtime/helpers/esm/toArray"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import { chainPropTypes } from '@material-ui/utils'; import Collapse from '../Collapse'; import Paper from '../Paper'; import withStyles from '../styles/withStyles'; export var styles = function styles(theme) { var transition = { duration: theme.transitions.duration.shortest }; return { /* Styles applied to the root element. */ root: { position: 'relative', transition: theme.transitions.create(['margin'], transition), '&:before': { position: 'absolute', left: 0, top: -1, right: 0, height: 1, content: '""', opacity: 1, backgroundColor: theme.palette.divider, transition: theme.transitions.create(['opacity', 'background-color'], transition) }, '&:first-child': { '&:before': { display: 'none' } }, '&$expanded': { margin: '16px 0', '&:first-child': { marginTop: 0 }, '&:last-child': { marginBottom: 0 }, '&:before': { opacity: 0 } }, '&$expanded + &': { '&:before': { display: 'none' } }, '&$disabled': { backgroundColor: theme.palette.action.disabledBackground } }, /* Styles applied to the root element if `square={false}`. */ rounded: { borderRadius: 0, '&:first-child': { borderTopLeftRadius: theme.shape.borderRadius, borderTopRightRadius: theme.shape.borderRadius }, '&:last-child': { borderBottomLeftRadius: theme.shape.borderRadius, borderBottomRightRadius: theme.shape.borderRadius, // Fix a rendering issue on Edge '@supports (-ms-ime-align: auto)': { borderBottomLeftRadius: 0, borderBottomRightRadius: 0 } } }, /* Styles applied to the root element if `expanded={true}`. */ expanded: {}, /* Styles applied to the root element if `disabled={true}`. */ disabled: {} }; }; var ExpansionPanel = React.forwardRef(function ExpansionPanel(props, ref) { var childrenProp = props.children, classes = props.classes, className = props.className, _props$defaultExpande = props.defaultExpanded, defaultExpanded = _props$defaultExpande === void 0 ? false : _props$defaultExpande, _props$disabled = props.disabled, disabled = _props$disabled === void 0 ? false : _props$disabled, expandedProp = props.expanded, onChange = props.onChange, _props$square = props.square, square = _props$square === void 0 ? false : _props$square, _props$TransitionComp = props.TransitionComponent, TransitionComponent = _props$TransitionComp === void 0 ? Collapse : _props$TransitionComp, TransitionProps = props.TransitionProps, other = _objectWithoutProperties(props, ["children", "classes", "className", "defaultExpanded", "disabled", "expanded", "onChange", "square", "TransitionComponent", "TransitionProps"]); var _React$useRef = React.useRef(expandedProp != null), isControlled = _React$useRef.current; var _React$useState = React.useState(defaultExpanded), _React$useState2 = _slicedToArray(_React$useState, 2), expandedState = _React$useState2[0], setExpandedState = _React$useState2[1]; var expanded = isControlled ? expandedProp : expandedState; var handleChange = function handleChange(event) { if (!isControlled) { setExpandedState(!expanded); } if (onChange) { onChange(event, !expanded); } }; var _React$Children$toArr = React.Children.toArray(childrenProp), _React$Children$toArr2 = _toArray(_React$Children$toArr), summary = _React$Children$toArr2[0], children = _React$Children$toArr2.slice(1); return React.createElement(Paper, _extends({ className: clsx(classes.root, className, expanded && classes.expanded, disabled && classes.disabled, !square && classes.rounded), ref: ref, square: square }, other), React.cloneElement(summary, { disabled: disabled, expanded: expanded, onChange: handleChange }), React.createElement(TransitionComponent, _extends({ in: expanded, timeout: "auto" }, TransitionProps), React.createElement("div", { "aria-labelledby": summary.props.id, id: summary.props['aria-controls'], role: "region" }, children))); }); process.env.NODE_ENV !== "production" ? ExpansionPanel.propTypes = { /** * The content of the expansion panel. */ children: chainPropTypes(PropTypes.node.isRequired, function (props) { var summary = React.Children.toArray(props.children)[0]; if (summary.type === React.Fragment) { return new Error("Material-UI: the ExpansionPanel doesn't accept a Fragment as a child. " + 'Consider providing an array instead.'); } if (!React.isValidElement(summary)) { return new Error('Material-UI: expected the first child of ExpansionPanel to be a valid element.'); } return null; }), /** * Override or extend the styles applied to the component. * See [CSS API](#css) below for more details. */ classes: PropTypes.object.isRequired, /** * @ignore */ className: PropTypes.string, /** * If `true`, expands the panel by default. */ defaultExpanded: PropTypes.bool, /** * If `true`, the panel will be displayed in a disabled state. */ disabled: PropTypes.bool, /** * If `true`, expands the panel, otherwise collapse it. * Setting this prop enables control over the panel. */ expanded: PropTypes.bool, /** * Callback fired when the expand/collapse state is changed. * * @param {object} event The event source of the callback * @param {boolean} expanded The `expanded` state of the panel */ onChange: PropTypes.func, /** * @ignore */ square: PropTypes.bool, /** * The component used for the collapse effect. */ TransitionComponent: PropTypes.elementType, /** * Props applied to the `Transition` element. */ TransitionProps: PropTypes.object } : void 0; export default withStyles(styles, { name: 'MuiExpansionPanel' })(ExpansionPanel);