@material-ui/core
Version:
React components that implement Google's Material Design.
225 lines (205 loc) • 7.15 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import * as React from 'react';
import { isFragment } from 'react-is';
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';
import ExpansionPanelContext from './ExpansionPanelContext';
import useControlled from '../utils/useControlled';
export const styles = theme => {
const 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: {}
};
};
let warnedOnce = false;
/**
* ⚠️ The ExpansionPanel component was renamed to Accordion to use a more common naming convention.
*
* You should use `import { Accordion } from '@material-ui/core'`
* or `import Accordion from '@material-ui/core/Accordion'`.
*/
const ExpansionPanel = /*#__PURE__*/React.forwardRef(function ExpansionPanel(props, ref) {
if (process.env.NODE_ENV !== 'production') {
if (!warnedOnce) {
warnedOnce = true;
console.error(['Material-UI: the ExpansionPanel component was renamed to Accordion to use a more common naming convention.', '', "You should use `import { Accordion } from '@material-ui/core'`", "or `import Accordion from '@material-ui/core/Accordion'`"].join('\n'));
}
}
const {
children: childrenProp,
classes,
className,
defaultExpanded = false,
disabled = false,
expanded: expandedProp,
onChange,
square = false,
TransitionComponent = Collapse,
TransitionProps
} = props,
other = _objectWithoutPropertiesLoose(props, ["children", "classes", "className", "defaultExpanded", "disabled", "expanded", "onChange", "square", "TransitionComponent", "TransitionProps"]);
const [expanded, setExpandedState] = useControlled({
controlled: expandedProp,
default: defaultExpanded,
name: 'ExpansionPanel',
state: 'expanded'
});
const handleChange = React.useCallback(event => {
setExpandedState(!expanded);
if (onChange) {
onChange(event, !expanded);
}
}, [expanded, onChange, setExpandedState]);
const [summary, ...children] = React.Children.toArray(childrenProp);
const contextValue = React.useMemo(() => ({
expanded,
disabled,
toggle: handleChange
}), [expanded, disabled, handleChange]);
return /*#__PURE__*/React.createElement(Paper, _extends({
className: clsx(classes.root, className, expanded && classes.expanded, disabled && classes.disabled, !square && classes.rounded),
ref: ref,
square: square
}, other), /*#__PURE__*/React.createElement(ExpansionPanelContext.Provider, {
value: contextValue
}, summary), /*#__PURE__*/React.createElement(TransitionComponent, _extends({
in: expanded,
timeout: "auto"
}, TransitionProps), /*#__PURE__*/React.createElement("div", {
"aria-labelledby": summary.props.id,
id: summary.props['aria-controls'],
role: "region"
}, children)));
});
process.env.NODE_ENV !== "production" ? ExpansionPanel.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the expansion panel.
*/
children: chainPropTypes(PropTypes.node.isRequired, props => {
const summary = React.Children.toArray(props.children)[0];
if (isFragment(summary)) {
return new Error("Material-UI: The ExpansionPanel doesn't accept a Fragment as a child. " + 'Consider providing an array instead.');
}
if (! /*#__PURE__*/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,
/**
* @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,
/**
* If `true`, rounded corners are disabled.
*/
square: PropTypes.bool,
/**
* The component used for the collapse effect.
* [Follow this guide](/components/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
*/
TransitionComponent: PropTypes.elementType,
/**
* Props applied to the [`Transition`](http://reactcommunity.org/react-transition-group/transition#Transition-props) element.
*/
TransitionProps: PropTypes.object
} : void 0;
export default withStyles(styles, {
name: 'MuiExpansionPanel'
})(ExpansionPanel);