UNPKG

6.46 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 AccordionContext from './AccordionContext';
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};
85const Accordion = /*#__PURE__*/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 /*#__PURE__*/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), /*#__PURE__*/React.createElement(AccordionContext.Provider, {
124 value: contextValue
125 }, summary), /*#__PURE__*/React.createElement(TransitionComponent, _extends({
126 in: expanded,
127 timeout: "auto"
128 }, TransitionProps), /*#__PURE__*/React.createElement("div", {
129 "aria-labelledby": summary.props.id,
130 id: summary.props['aria-controls'],
131 role: "region"
132 }, children)));
133});
134process.env.NODE_ENV !== "production" ? Accordion.propTypes = {
135 // ----------------------------- Warning --------------------------------
136 // | These PropTypes are generated from the TypeScript type definitions |
137 // | To update them edit the d.ts file and run "yarn proptypes" |
138 // ----------------------------------------------------------------------
139
140 /**
141 * The content of the accordion.
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 (! /*#__PURE__*/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 * Override or extend the styles applied to the component.
159 * See [CSS API](#css) below for more details.
160 */
161 classes: PropTypes.object,
162
163 /**
164 * @ignore
165 */
166 className: PropTypes.string,
167
168 /**
169 * If `true`, expands the accordion by default.
170 */
171 defaultExpanded: PropTypes.bool,
172
173 /**
174 * If `true`, the accordion will be displayed in a disabled state.
175 */
176 disabled: PropTypes.bool,
177
178 /**
179 * If `true`, expands the accordion, otherwise collapse it.
180 * Setting this prop enables control over the accordion.
181 */
182 expanded: PropTypes.bool,
183
184 /**
185 * Callback fired when the expand/collapse state is changed.
186 *
187 * @param {object} event The event source of the callback.
188 * @param {boolean} expanded The `expanded` state of the accordion.
189 */
190 onChange: PropTypes.func,
191
192 /**
193 * If `true`, rounded corners are disabled.
194 */
195 square: PropTypes.bool,
196
197 /**
198 * The component used for the collapse effect.
199 * [Follow this guide](/components/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
200 */
201 TransitionComponent: PropTypes.elementType,
202
203 /**
204 * Props applied to the [`Transition`](http://reactcommunity.org/react-transition-group/transition#Transition-props) element.
205 */
206 TransitionProps: PropTypes.object
207} : void 0;
208export default withStyles(styles, {
209 name: 'MuiAccordion'
210})(Accordion);
\No newline at end of file