UNPKG

7.15 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import * as React from 'react';
4import { isFragment } from 'react-is';
5import PropTypes from 'prop-types';
6import clsx from 'clsx';
7import { chainPropTypes } from '@material-ui/utils';
8import Collapse from '../Collapse';
9import Paper from '../Paper';
10import withStyles from '../styles/withStyles';
11import ExpansionPanelContext from './ExpansionPanelContext';
12import useControlled from '../utils/useControlled';
13export const styles = theme => {
14 const transition = {
15 duration: theme.transitions.duration.shortest
16 };
17 return {
18 /* Styles applied to the root element. */
19 root: {
20 position: 'relative',
21 transition: theme.transitions.create(['margin'], transition),
22 '&:before': {
23 position: 'absolute',
24 left: 0,
25 top: -1,
26 right: 0,
27 height: 1,
28 content: '""',
29 opacity: 1,
30 backgroundColor: theme.palette.divider,
31 transition: theme.transitions.create(['opacity', 'background-color'], transition)
32 },
33 '&:first-child': {
34 '&:before': {
35 display: 'none'
36 }
37 },
38 '&$expanded': {
39 margin: '16px 0',
40 '&:first-child': {
41 marginTop: 0
42 },
43 '&:last-child': {
44 marginBottom: 0
45 },
46 '&:before': {
47 opacity: 0
48 }
49 },
50 '&$expanded + &': {
51 '&:before': {
52 display: 'none'
53 }
54 },
55 '&$disabled': {
56 backgroundColor: theme.palette.action.disabledBackground
57 }
58 },
59
60 /* Styles applied to the root element if `square={false}`. */
61 rounded: {
62 borderRadius: 0,
63 '&:first-child': {
64 borderTopLeftRadius: theme.shape.borderRadius,
65 borderTopRightRadius: theme.shape.borderRadius
66 },
67 '&:last-child': {
68 borderBottomLeftRadius: theme.shape.borderRadius,
69 borderBottomRightRadius: theme.shape.borderRadius,
70 // Fix a rendering issue on Edge
71 '@supports (-ms-ime-align: auto)': {
72 borderBottomLeftRadius: 0,
73 borderBottomRightRadius: 0
74 }
75 }
76 },
77
78 /* Styles applied to the root element if `expanded={true}`. */
79 expanded: {},
80
81 /* Styles applied to the root element if `disabled={true}`. */
82 disabled: {}
83 };
84};
85let warnedOnce = false;
86/**
87 * ⚠️ The ExpansionPanel component was renamed to Accordion to use a more common naming convention.
88 *
89 * You should use `import { Accordion } from '@material-ui/core'`
90 * or `import Accordion from '@material-ui/core/Accordion'`.
91 */
92
93const ExpansionPanel = /*#__PURE__*/React.forwardRef(function ExpansionPanel(props, ref) {
94 if (process.env.NODE_ENV !== 'production') {
95 if (!warnedOnce) {
96 warnedOnce = true;
97 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'));
98 }
99 }
100
101 const {
102 children: childrenProp,
103 classes,
104 className,
105 defaultExpanded = false,
106 disabled = false,
107 expanded: expandedProp,
108 onChange,
109 square = false,
110 TransitionComponent = Collapse,
111 TransitionProps
112 } = props,
113 other = _objectWithoutPropertiesLoose(props, ["children", "classes", "className", "defaultExpanded", "disabled", "expanded", "onChange", "square", "TransitionComponent", "TransitionProps"]);
114
115 const [expanded, setExpandedState] = useControlled({
116 controlled: expandedProp,
117 default: defaultExpanded,
118 name: 'ExpansionPanel',
119 state: 'expanded'
120 });
121 const handleChange = React.useCallback(event => {
122 setExpandedState(!expanded);
123
124 if (onChange) {
125 onChange(event, !expanded);
126 }
127 }, [expanded, onChange, setExpandedState]);
128 const [summary, ...children] = React.Children.toArray(childrenProp);
129 const contextValue = React.useMemo(() => ({
130 expanded,
131 disabled,
132 toggle: handleChange
133 }), [expanded, disabled, handleChange]);
134 return /*#__PURE__*/React.createElement(Paper, _extends({
135 className: clsx(classes.root, className, expanded && classes.expanded, disabled && classes.disabled, !square && classes.rounded),
136 ref: ref,
137 square: square
138 }, other), /*#__PURE__*/React.createElement(ExpansionPanelContext.Provider, {
139 value: contextValue
140 }, summary), /*#__PURE__*/React.createElement(TransitionComponent, _extends({
141 in: expanded,
142 timeout: "auto"
143 }, TransitionProps), /*#__PURE__*/React.createElement("div", {
144 "aria-labelledby": summary.props.id,
145 id: summary.props['aria-controls'],
146 role: "region"
147 }, children)));
148});
149process.env.NODE_ENV !== "production" ? ExpansionPanel.propTypes = {
150 // ----------------------------- Warning --------------------------------
151 // | These PropTypes are generated from the TypeScript type definitions |
152 // | To update them edit the d.ts file and run "yarn proptypes" |
153 // ----------------------------------------------------------------------
154
155 /**
156 * The content of the expansion panel.
157 */
158 children: chainPropTypes(PropTypes.node.isRequired, props => {
159 const summary = React.Children.toArray(props.children)[0];
160
161 if (isFragment(summary)) {
162 return new Error("Material-UI: The ExpansionPanel doesn't accept a Fragment as a child. " + 'Consider providing an array instead.');
163 }
164
165 if (! /*#__PURE__*/React.isValidElement(summary)) {
166 return new Error('Material-UI: Expected the first child of ExpansionPanel to be a valid element.');
167 }
168
169 return null;
170 }),
171
172 /**
173 * Override or extend the styles applied to the component.
174 * See [CSS API](#css) below for more details.
175 */
176 classes: PropTypes.object,
177
178 /**
179 * @ignore
180 */
181 className: PropTypes.string,
182
183 /**
184 * If `true`, expands the panel by default.
185 */
186 defaultExpanded: PropTypes.bool,
187
188 /**
189 * If `true`, the panel will be displayed in a disabled state.
190 */
191 disabled: PropTypes.bool,
192
193 /**
194 * If `true`, expands the panel, otherwise collapse it.
195 * Setting this prop enables control over the panel.
196 */
197 expanded: PropTypes.bool,
198
199 /**
200 * Callback fired when the expand/collapse state is changed.
201 *
202 * @param {object} event The event source of the callback.
203 * @param {boolean} expanded The `expanded` state of the panel.
204 */
205 onChange: PropTypes.func,
206
207 /**
208 * If `true`, rounded corners are disabled.
209 */
210 square: PropTypes.bool,
211
212 /**
213 * The component used for the collapse effect.
214 * [Follow this guide](/components/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
215 */
216 TransitionComponent: PropTypes.elementType,
217
218 /**
219 * Props applied to the [`Transition`](http://reactcommunity.org/react-transition-group/transition#Transition-props) element.
220 */
221 TransitionProps: PropTypes.object
222} : void 0;
223export default withStyles(styles, {
224 name: 'MuiExpansionPanel'
225})(ExpansionPanel);
\No newline at end of file