UNPKG

6.17 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2import _extends from "@babel/runtime/helpers/esm/extends";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import withStyles from '../styles/withStyles';
7import ButtonBase from '../ButtonBase';
8import capitalize from '../utils/capitalize';
9import unsupportedProp from '../utils/unsupportedProp';
10export const styles = theme => ({
11 /* Styles applied to the root element. */
12 root: _extends({}, theme.typography.button, {
13 maxWidth: 264,
14 minWidth: 72,
15 position: 'relative',
16 boxSizing: 'border-box',
17 minHeight: 48,
18 flexShrink: 0,
19 padding: '6px 12px',
20 [theme.breakpoints.up('sm')]: {
21 padding: '6px 24px'
22 },
23 overflow: 'hidden',
24 whiteSpace: 'normal',
25 textAlign: 'center',
26 [theme.breakpoints.up('sm')]: {
27 minWidth: 160
28 }
29 }),
30
31 /* Styles applied to the root element if both `icon` and `label` are provided. */
32 labelIcon: {
33 minHeight: 72,
34 paddingTop: 9,
35 '& $wrapper > *:first-child': {
36 marginBottom: 6
37 }
38 },
39
40 /* Styles applied to the root element if the parent [`Tabs`](/api/tabs/) has `textColor="inherit"`. */
41 textColorInherit: {
42 color: 'inherit',
43 opacity: 0.7,
44 '&$selected': {
45 opacity: 1
46 },
47 '&$disabled': {
48 opacity: 0.5
49 }
50 },
51
52 /* Styles applied to the root element if the parent [`Tabs`](/api/tabs/) has `textColor="primary"`. */
53 textColorPrimary: {
54 color: theme.palette.text.secondary,
55 '&$selected': {
56 color: theme.palette.primary.main
57 },
58 '&$disabled': {
59 color: theme.palette.text.disabled
60 }
61 },
62
63 /* Styles applied to the root element if the parent [`Tabs`](/api/tabs/) has `textColor="secondary"`. */
64 textColorSecondary: {
65 color: theme.palette.text.secondary,
66 '&$selected': {
67 color: theme.palette.secondary.main
68 },
69 '&$disabled': {
70 color: theme.palette.text.disabled
71 }
72 },
73
74 /* Pseudo-class applied to the root element if `selected={true}` (controlled by the Tabs component). */
75 selected: {},
76
77 /* Pseudo-class applied to the root element if `disabled={true}` (controlled by the Tabs component). */
78 disabled: {},
79
80 /* Styles applied to the root element if `fullWidth={true}` (controlled by the Tabs component). */
81 fullWidth: {
82 flexShrink: 1,
83 flexGrow: 1,
84 flexBasis: 0,
85 maxWidth: 'none'
86 },
87
88 /* Styles applied to the root element if `wrapped={true}`. */
89 wrapped: {
90 fontSize: theme.typography.pxToRem(12),
91 lineHeight: 1.5
92 },
93
94 /* Styles applied to the `icon` and `label`'s wrapper element. */
95 wrapper: {
96 display: 'inline-flex',
97 alignItems: 'center',
98 justifyContent: 'center',
99 width: '100%',
100 flexDirection: 'column'
101 }
102});
103const Tab = /*#__PURE__*/React.forwardRef(function Tab(props, ref) {
104 const {
105 classes,
106 className,
107 disabled = false,
108 disableFocusRipple = false,
109 fullWidth,
110 icon,
111 indicator,
112 label,
113 onChange,
114 onClick,
115 onFocus,
116 selected,
117 selectionFollowsFocus,
118 textColor = 'inherit',
119 value,
120 wrapped = false
121 } = props,
122 other = _objectWithoutPropertiesLoose(props, ["classes", "className", "disabled", "disableFocusRipple", "fullWidth", "icon", "indicator", "label", "onChange", "onClick", "onFocus", "selected", "selectionFollowsFocus", "textColor", "value", "wrapped"]);
123
124 const handleClick = event => {
125 if (onChange) {
126 onChange(event, value);
127 }
128
129 if (onClick) {
130 onClick(event);
131 }
132 };
133
134 const handleFocus = event => {
135 if (selectionFollowsFocus && !selected && onChange) {
136 onChange(event, value);
137 }
138
139 if (onFocus) {
140 onFocus(event);
141 }
142 };
143
144 return /*#__PURE__*/React.createElement(ButtonBase, _extends({
145 focusRipple: !disableFocusRipple,
146 className: clsx(classes.root, classes[`textColor${capitalize(textColor)}`], className, disabled && classes.disabled, selected && classes.selected, label && icon && classes.labelIcon, fullWidth && classes.fullWidth, wrapped && classes.wrapped),
147 ref: ref,
148 role: "tab",
149 "aria-selected": selected,
150 disabled: disabled,
151 onClick: handleClick,
152 onFocus: handleFocus,
153 tabIndex: selected ? 0 : -1
154 }, other), /*#__PURE__*/React.createElement("span", {
155 className: classes.wrapper
156 }, icon, label), indicator);
157});
158process.env.NODE_ENV !== "production" ? Tab.propTypes = {
159 /**
160 * This prop isn't supported.
161 * Use the `component` prop if you need to change the children structure.
162 */
163 children: unsupportedProp,
164
165 /**
166 * Override or extend the styles applied to the component.
167 * See [CSS API](#css) below for more details.
168 */
169 classes: PropTypes.object.isRequired,
170
171 /**
172 * @ignore
173 */
174 className: PropTypes.string,
175
176 /**
177 * If `true`, the tab will be disabled.
178 */
179 disabled: PropTypes.bool,
180
181 /**
182 * If `true`, the keyboard focus ripple will be disabled.
183 */
184 disableFocusRipple: PropTypes.bool,
185
186 /**
187 * If `true`, the ripple effect will be disabled.
188 */
189 disableRipple: PropTypes.bool,
190
191 /**
192 * @ignore
193 */
194 fullWidth: PropTypes.bool,
195
196 /**
197 * The icon element.
198 */
199 icon: PropTypes.node,
200
201 /**
202 * @ignore
203 * For server-side rendering consideration, we let the selected tab
204 * render the indicator.
205 */
206 indicator: PropTypes.node,
207
208 /**
209 * The label element.
210 */
211 label: PropTypes.node,
212
213 /**
214 * @ignore
215 */
216 onChange: PropTypes.func,
217
218 /**
219 * @ignore
220 */
221 onClick: PropTypes.func,
222
223 /**
224 * @ignore
225 */
226 onFocus: PropTypes.func,
227
228 /**
229 * @ignore
230 */
231 selected: PropTypes.bool,
232
233 /**
234 * @ignore
235 */
236 selectionFollowsFocus: PropTypes.bool,
237
238 /**
239 * @ignore
240 */
241 textColor: PropTypes.oneOf(['secondary', 'primary', 'inherit']),
242
243 /**
244 * You can provide your own value. Otherwise, we fallback to the child position index.
245 */
246 value: PropTypes.any,
247
248 /**
249 * Tab labels appear in a single row.
250 * They can use a second line if needed.
251 */
252 wrapped: PropTypes.bool
253} : void 0;
254export default withStyles(styles, {
255 name: 'MuiTab'
256})(Tab);
\No newline at end of file