UNPKG

5.21 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
4/* eslint-disable jsx-a11y/aria-role */
5import * as React from 'react';
6import PropTypes from 'prop-types';
7import clsx from 'clsx';
8import ButtonBase from '../ButtonBase';
9import IconButton from '../IconButton';
10import withStyles from '../styles/withStyles';
11import AccordionContext from '../Accordion/AccordionContext';
12export const styles = theme => {
13 const transition = {
14 duration: theme.transitions.duration.shortest
15 };
16 return {
17 /* Styles applied to the root element. */
18 root: {
19 display: 'flex',
20 minHeight: 8 * 6,
21 transition: theme.transitions.create(['min-height', 'background-color'], transition),
22 padding: theme.spacing(0, 2),
23 '&:hover:not($disabled)': {
24 cursor: 'pointer'
25 },
26 '&$expanded': {
27 minHeight: 64
28 },
29 '&$focused': {
30 backgroundColor: theme.palette.action.focus
31 },
32 '&$disabled': {
33 opacity: theme.palette.action.disabledOpacity
34 }
35 },
36
37 /* Pseudo-class applied to the root element, children wrapper element and `IconButton` component if `expanded={true}`. */
38 expanded: {},
39
40 /* Pseudo-class applied to the root element if `focused={true}`. */
41 focused: {},
42
43 /* Pseudo-class applied to the root element if `disabled={true}`. */
44 disabled: {},
45
46 /* Styles applied to the children wrapper element. */
47 content: {
48 display: 'flex',
49 flexGrow: 1,
50 transition: theme.transitions.create(['margin'], transition),
51 margin: '12px 0',
52 '&$expanded': {
53 margin: '20px 0'
54 }
55 },
56
57 /* Styles applied to the `IconButton` component when `expandIcon` is supplied. */
58 expandIcon: {
59 transform: 'rotate(0deg)',
60 transition: theme.transitions.create('transform', transition),
61 '&:hover': {
62 // Disable the hover effect for the IconButton,
63 // because a hover effect should apply to the entire Expand button and
64 // not only to the IconButton.
65 backgroundColor: 'transparent'
66 },
67 '&$expanded': {
68 transform: 'rotate(180deg)'
69 }
70 }
71 };
72};
73const AccordionSummary = /*#__PURE__*/React.forwardRef(function AccordionSummary(props, ref) {
74 const {
75 children,
76 classes,
77 className,
78 expandIcon,
79 IconButtonProps,
80 onBlur,
81 onClick,
82 onFocusVisible
83 } = props,
84 other = _objectWithoutPropertiesLoose(props, ["children", "classes", "className", "expandIcon", "IconButtonProps", "onBlur", "onClick", "onFocusVisible"]);
85
86 const [focusedState, setFocusedState] = React.useState(false);
87
88 const handleFocusVisible = event => {
89 setFocusedState(true);
90
91 if (onFocusVisible) {
92 onFocusVisible(event);
93 }
94 };
95
96 const handleBlur = event => {
97 setFocusedState(false);
98
99 if (onBlur) {
100 onBlur(event);
101 }
102 };
103
104 const {
105 disabled = false,
106 expanded,
107 toggle
108 } = React.useContext(AccordionContext);
109
110 const handleChange = event => {
111 if (toggle) {
112 toggle(event);
113 }
114
115 if (onClick) {
116 onClick(event);
117 }
118 };
119
120 return /*#__PURE__*/React.createElement(ButtonBase, _extends({
121 focusRipple: false,
122 disableRipple: true,
123 disabled: disabled,
124 component: "div",
125 "aria-expanded": expanded,
126 className: clsx(classes.root, className, disabled && classes.disabled, expanded && classes.expanded, focusedState && classes.focused),
127 onFocusVisible: handleFocusVisible,
128 onBlur: handleBlur,
129 onClick: handleChange,
130 ref: ref
131 }, other), /*#__PURE__*/React.createElement("div", {
132 className: clsx(classes.content, expanded && classes.expanded)
133 }, children), expandIcon && /*#__PURE__*/React.createElement(IconButton, _extends({
134 className: clsx(classes.expandIcon, expanded && classes.expanded),
135 edge: "end",
136 component: "div",
137 tabIndex: null,
138 role: null,
139 "aria-hidden": true
140 }, IconButtonProps), expandIcon));
141});
142process.env.NODE_ENV !== "production" ? AccordionSummary.propTypes = {
143 // ----------------------------- Warning --------------------------------
144 // | These PropTypes are generated from the TypeScript type definitions |
145 // | To update them edit the d.ts file and run "yarn proptypes" |
146 // ----------------------------------------------------------------------
147
148 /**
149 * The content of the accordion summary.
150 */
151 children: PropTypes.node,
152
153 /**
154 * Override or extend the styles applied to the component.
155 * See [CSS API](#css) below for more details.
156 */
157 classes: PropTypes.object,
158
159 /**
160 * @ignore
161 */
162 className: PropTypes.string,
163
164 /**
165 * The icon to display as the expand indicator.
166 */
167 expandIcon: PropTypes.node,
168
169 /**
170 * Props applied to the `IconButton` element wrapping the expand icon.
171 */
172 IconButtonProps: PropTypes.object,
173
174 /**
175 * @ignore
176 */
177 onBlur: PropTypes.func,
178
179 /**
180 * @ignore
181 */
182 onClick: PropTypes.func,
183
184 /**
185 * @ignore
186 */
187 onFocusVisible: PropTypes.func
188} : void 0;
189export default withStyles(styles, {
190 name: 'MuiAccordionSummary'
191})(AccordionSummary);
\No newline at end of file