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 AccordionContext from './AccordionContext';
|
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 | const Accordion = React.forwardRef(function Accordion(props, ref) {
|
86 | const {
|
87 | children: childrenProp,
|
88 | classes,
|
89 | className,
|
90 | defaultExpanded = false,
|
91 | disabled = false,
|
92 | expanded: expandedProp,
|
93 | onChange,
|
94 | square = false,
|
95 | TransitionComponent = Collapse,
|
96 | TransitionProps
|
97 | } = props,
|
98 | other = _objectWithoutPropertiesLoose(props, ["children", "classes", "className", "defaultExpanded", "disabled", "expanded", "onChange", "square", "TransitionComponent", "TransitionProps"]);
|
99 |
|
100 | const [expanded, setExpandedState] = useControlled({
|
101 | controlled: expandedProp,
|
102 | default: defaultExpanded,
|
103 | name: 'Accordion',
|
104 | state: 'expanded'
|
105 | });
|
106 | const handleChange = React.useCallback(event => {
|
107 | setExpandedState(!expanded);
|
108 |
|
109 | if (onChange) {
|
110 | onChange(event, !expanded);
|
111 | }
|
112 | }, [expanded, onChange, setExpandedState]);
|
113 | const [summary, ...children] = React.Children.toArray(childrenProp);
|
114 | const contextValue = React.useMemo(() => ({
|
115 | expanded,
|
116 | disabled,
|
117 | toggle: handleChange
|
118 | }), [expanded, disabled, handleChange]);
|
119 | return React.createElement(Paper, _extends({
|
120 | className: clsx(classes.root, className, expanded && classes.expanded, disabled && classes.disabled, !square && classes.rounded),
|
121 | ref: ref,
|
122 | square: square
|
123 | }, other), React.createElement(AccordionContext.Provider, {
|
124 | value: contextValue
|
125 | }, summary), React.createElement(TransitionComponent, _extends({
|
126 | in: expanded,
|
127 | timeout: "auto"
|
128 | }, TransitionProps), React.createElement("div", {
|
129 | "aria-labelledby": summary.props.id,
|
130 | id: summary.props['aria-controls'],
|
131 | role: "region"
|
132 | }, children)));
|
133 | });
|
134 | process.env.NODE_ENV !== "production" ? Accordion.propTypes = {
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | |
141 |
|
142 |
|
143 | children: chainPropTypes(PropTypes.node.isRequired, props => {
|
144 | const summary = React.Children.toArray(props.children)[0];
|
145 |
|
146 | if (isFragment(summary)) {
|
147 | return new Error("Material-UI: The Accordion doesn't accept a Fragment as a child. " + 'Consider providing an array instead.');
|
148 | }
|
149 |
|
150 | if (! React.isValidElement(summary)) {
|
151 | return new Error('Material-UI: Expected the first child of Accordion to be a valid element.');
|
152 | }
|
153 |
|
154 | return null;
|
155 | }),
|
156 |
|
157 | |
158 |
|
159 |
|
160 |
|
161 | classes: PropTypes.object,
|
162 |
|
163 | |
164 |
|
165 |
|
166 | className: PropTypes.string,
|
167 |
|
168 | |
169 |
|
170 |
|
171 | defaultExpanded: PropTypes.bool,
|
172 |
|
173 | |
174 |
|
175 |
|
176 | disabled: PropTypes.bool,
|
177 |
|
178 | |
179 |
|
180 |
|
181 |
|
182 | expanded: PropTypes.bool,
|
183 |
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | onChange: PropTypes.func,
|
191 |
|
192 | |
193 |
|
194 |
|
195 | square: PropTypes.bool,
|
196 |
|
197 | |
198 |
|
199 |
|
200 |
|
201 | TransitionComponent: PropTypes.elementType,
|
202 |
|
203 | |
204 |
|
205 |
|
206 | TransitionProps: PropTypes.object
|
207 | } : void 0;
|
208 | export default withStyles(styles, {
|
209 | name: 'MuiAccordion'
|
210 | })(Accordion); |
\ | No newline at end of file |