1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
3 | import * as React from 'react';
|
4 | import { isFragment } from 'react-is';
|
5 | import PropTypes from 'prop-types';
|
6 | import clsx from 'clsx';
|
7 | import { chainPropTypes } from '@material-ui/utils';
|
8 | import Collapse from '../Collapse';
|
9 | import Paper from '../Paper';
|
10 | import withStyles from '../styles/withStyles';
|
11 | import ExpansionPanelContext from './ExpansionPanelContext';
|
12 | import useControlled from '../utils/useControlled';
|
13 | export const styles = theme => {
|
14 | const transition = {
|
15 | duration: theme.transitions.duration.shortest
|
16 | };
|
17 | return {
|
18 |
|
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 |
|
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 |
|
71 | '@supports (-ms-ime-align: auto)': {
|
72 | borderBottomLeftRadius: 0,
|
73 | borderBottomRightRadius: 0
|
74 | }
|
75 | }
|
76 | },
|
77 |
|
78 |
|
79 | expanded: {},
|
80 |
|
81 |
|
82 | disabled: {}
|
83 | };
|
84 | };
|
85 | let warnedOnce = false;
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | const ExpansionPanel = 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 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), React.createElement(ExpansionPanelContext.Provider, {
|
139 | value: contextValue
|
140 | }, summary), React.createElement(TransitionComponent, _extends({
|
141 | in: expanded,
|
142 | timeout: "auto"
|
143 | }, TransitionProps), React.createElement("div", {
|
144 | "aria-labelledby": summary.props.id,
|
145 | id: summary.props['aria-controls'],
|
146 | role: "region"
|
147 | }, children)));
|
148 | });
|
149 | process.env.NODE_ENV !== "production" ? ExpansionPanel.propTypes = {
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | |
156 |
|
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 (! 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 |
|
174 |
|
175 |
|
176 | classes: PropTypes.object,
|
177 |
|
178 | |
179 |
|
180 |
|
181 | className: PropTypes.string,
|
182 |
|
183 | |
184 |
|
185 |
|
186 | defaultExpanded: PropTypes.bool,
|
187 |
|
188 | |
189 |
|
190 |
|
191 | disabled: PropTypes.bool,
|
192 |
|
193 | |
194 |
|
195 |
|
196 |
|
197 | expanded: PropTypes.bool,
|
198 |
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | onChange: PropTypes.func,
|
206 |
|
207 | |
208 |
|
209 |
|
210 | square: PropTypes.bool,
|
211 |
|
212 | |
213 |
|
214 |
|
215 |
|
216 | TransitionComponent: PropTypes.elementType,
|
217 |
|
218 | |
219 |
|
220 |
|
221 | TransitionProps: PropTypes.object
|
222 | } : void 0;
|
223 | export default withStyles(styles, {
|
224 | name: 'MuiExpansionPanel'
|
225 | })(ExpansionPanel); |
\ | No newline at end of file |