@material-ui/core
Version:
React components that implement Google's Material Design.
225 lines (204 loc) • 7.41 kB
JavaScript
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 * 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 AccordionContext from './AccordionContext';
import useControlled from '../utils/useControlled';
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 Accordion = /*#__PURE__*/React.forwardRef(function Accordion(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 _useControlled = useControlled({
controlled: expandedProp,
default: defaultExpanded,
name: 'Accordion',
state: 'expanded'
}),
_useControlled2 = _slicedToArray(_useControlled, 2),
expanded = _useControlled2[0],
setExpandedState = _useControlled2[1];
var handleChange = React.useCallback(function (event) {
setExpandedState(!expanded);
if (onChange) {
onChange(event, !expanded);
}
}, [expanded, onChange, setExpandedState]);
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);
var contextValue = React.useMemo(function () {
return {
expanded: expanded,
disabled: 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(AccordionContext.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" ? Accordion.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 accordion.
*/
children: chainPropTypes(PropTypes.node.isRequired, function (props) {
var summary = React.Children.toArray(props.children)[0];
if (isFragment(summary)) {
return new Error("Material-UI: The Accordion 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 Accordion 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 accordion by default.
*/
defaultExpanded: PropTypes.bool,
/**
* If `true`, the accordion will be displayed in a disabled state.
*/
disabled: PropTypes.bool,
/**
* If `true`, expands the accordion, otherwise collapse it.
* Setting this prop enables control over the accordion.
*/
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 accordion.
*/
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: 'MuiAccordion'
})(Accordion);