1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import composeClasses from '@mui/utils/composeClasses';
|
7 | import { alpha } from '@mui/system/colorManipulator';
|
8 | import getValidReactChildren from '@mui/utils/getValidReactChildren';
|
9 | import capitalize from "../utils/capitalize.js";
|
10 | import { styled } from "../zero-styled/index.js";
|
11 | import memoTheme from "../utils/memoTheme.js";
|
12 | import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
|
13 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
14 | import buttonGroupClasses, { getButtonGroupUtilityClass } from "./buttonGroupClasses.js";
|
15 | import ButtonGroupContext from "./ButtonGroupContext.js";
|
16 | import ButtonGroupButtonContext from "./ButtonGroupButtonContext.js";
|
17 | import { jsx as _jsx } from "react/jsx-runtime";
|
18 | const overridesResolver = (props, styles) => {
|
19 | const {
|
20 | ownerState
|
21 | } = props;
|
22 | return [{
|
23 | [`& .${buttonGroupClasses.grouped}`]: styles.grouped
|
24 | }, {
|
25 | [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.orientation)}`]
|
26 | }, {
|
27 | [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.variant)}`]
|
28 | }, {
|
29 | [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.variant)}${capitalize(ownerState.orientation)}`]
|
30 | }, {
|
31 | [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.variant)}${capitalize(ownerState.color)}`]
|
32 | }, {
|
33 | [`& .${buttonGroupClasses.firstButton}`]: styles.firstButton
|
34 | }, {
|
35 | [`& .${buttonGroupClasses.lastButton}`]: styles.lastButton
|
36 | }, {
|
37 | [`& .${buttonGroupClasses.middleButton}`]: styles.middleButton
|
38 | }, styles.root, styles[ownerState.variant], ownerState.disableElevation === true && styles.disableElevation, ownerState.fullWidth && styles.fullWidth, ownerState.orientation === 'vertical' && styles.vertical];
|
39 | };
|
40 | const useUtilityClasses = ownerState => {
|
41 | const {
|
42 | classes,
|
43 | color,
|
44 | disabled,
|
45 | disableElevation,
|
46 | fullWidth,
|
47 | orientation,
|
48 | variant
|
49 | } = ownerState;
|
50 | const slots = {
|
51 | root: ['root', variant, orientation, fullWidth && 'fullWidth', disableElevation && 'disableElevation', `color${capitalize(color)}`],
|
52 | grouped: ['grouped', `grouped${capitalize(orientation)}`, `grouped${capitalize(variant)}`, `grouped${capitalize(variant)}${capitalize(orientation)}`, `grouped${capitalize(variant)}${capitalize(color)}`, disabled && 'disabled'],
|
53 | firstButton: ['firstButton'],
|
54 | lastButton: ['lastButton'],
|
55 | middleButton: ['middleButton']
|
56 | };
|
57 | return composeClasses(slots, getButtonGroupUtilityClass, classes);
|
58 | };
|
59 | const ButtonGroupRoot = styled('div', {
|
60 | name: 'MuiButtonGroup',
|
61 | slot: 'Root',
|
62 | overridesResolver
|
63 | })(memoTheme(({
|
64 | theme
|
65 | }) => ({
|
66 | display: 'inline-flex',
|
67 | borderRadius: (theme.vars || theme).shape.borderRadius,
|
68 | variants: [{
|
69 | props: {
|
70 | variant: 'contained'
|
71 | },
|
72 | style: {
|
73 | boxShadow: (theme.vars || theme).shadows[2]
|
74 | }
|
75 | }, {
|
76 | props: {
|
77 | disableElevation: true
|
78 | },
|
79 | style: {
|
80 | boxShadow: 'none'
|
81 | }
|
82 | }, {
|
83 | props: {
|
84 | fullWidth: true
|
85 | },
|
86 | style: {
|
87 | width: '100%'
|
88 | }
|
89 | }, {
|
90 | props: {
|
91 | orientation: 'vertical'
|
92 | },
|
93 | style: {
|
94 | flexDirection: 'column',
|
95 | [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
|
96 | borderTopRightRadius: 0,
|
97 | borderTopLeftRadius: 0
|
98 | },
|
99 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
100 | borderBottomRightRadius: 0,
|
101 | borderBottomLeftRadius: 0
|
102 | }
|
103 | }
|
104 | }, {
|
105 | props: {
|
106 | orientation: 'horizontal'
|
107 | },
|
108 | style: {
|
109 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
110 | borderTopRightRadius: 0,
|
111 | borderBottomRightRadius: 0
|
112 | },
|
113 | [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
|
114 | borderTopLeftRadius: 0,
|
115 | borderBottomLeftRadius: 0
|
116 | }
|
117 | }
|
118 | }, {
|
119 | props: {
|
120 | variant: 'text',
|
121 | orientation: 'horizontal'
|
122 | },
|
123 | style: {
|
124 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
125 | borderRight: theme.vars ? `1px solid rgba(${theme.vars.palette.common.onBackgroundChannel} / 0.23)` : `1px solid ${theme.palette.mode === 'light' ? 'rgba(0, 0, 0, 0.23)' : 'rgba(255, 255, 255, 0.23)'}`,
|
126 | [`&.${buttonGroupClasses.disabled}`]: {
|
127 | borderRight: `1px solid ${(theme.vars || theme).palette.action.disabled}`
|
128 | }
|
129 | }
|
130 | }
|
131 | }, {
|
132 | props: {
|
133 | variant: 'text',
|
134 | orientation: 'vertical'
|
135 | },
|
136 | style: {
|
137 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
138 | borderBottom: theme.vars ? `1px solid rgba(${theme.vars.palette.common.onBackgroundChannel} / 0.23)` : `1px solid ${theme.palette.mode === 'light' ? 'rgba(0, 0, 0, 0.23)' : 'rgba(255, 255, 255, 0.23)'}`,
|
139 | [`&.${buttonGroupClasses.disabled}`]: {
|
140 | borderBottom: `1px solid ${(theme.vars || theme).palette.action.disabled}`
|
141 | }
|
142 | }
|
143 | }
|
144 | }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()).flatMap(([color]) => [{
|
145 | props: {
|
146 | variant: 'text',
|
147 | color
|
148 | },
|
149 | style: {
|
150 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
151 | borderColor: theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / 0.5)` : alpha(theme.palette[color].main, 0.5)
|
152 | }
|
153 | }
|
154 | }]), {
|
155 | props: {
|
156 | variant: 'outlined',
|
157 | orientation: 'horizontal'
|
158 | },
|
159 | style: {
|
160 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
161 | borderRightColor: 'transparent',
|
162 | '&:hover': {
|
163 | borderRightColor: 'currentColor'
|
164 | }
|
165 | },
|
166 | [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
|
167 | marginLeft: -1
|
168 | }
|
169 | }
|
170 | }, {
|
171 | props: {
|
172 | variant: 'outlined',
|
173 | orientation: 'vertical'
|
174 | },
|
175 | style: {
|
176 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
177 | borderBottomColor: 'transparent',
|
178 | '&:hover': {
|
179 | borderBottomColor: 'currentColor'
|
180 | }
|
181 | },
|
182 | [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
|
183 | marginTop: -1
|
184 | }
|
185 | }
|
186 | }, {
|
187 | props: {
|
188 | variant: 'contained',
|
189 | orientation: 'horizontal'
|
190 | },
|
191 | style: {
|
192 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
193 | borderRight: `1px solid ${(theme.vars || theme).palette.grey[400]}`,
|
194 | [`&.${buttonGroupClasses.disabled}`]: {
|
195 | borderRight: `1px solid ${(theme.vars || theme).palette.action.disabled}`
|
196 | }
|
197 | }
|
198 | }
|
199 | }, {
|
200 | props: {
|
201 | variant: 'contained',
|
202 | orientation: 'vertical'
|
203 | },
|
204 | style: {
|
205 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
206 | borderBottom: `1px solid ${(theme.vars || theme).palette.grey[400]}`,
|
207 | [`&.${buttonGroupClasses.disabled}`]: {
|
208 | borderBottom: `1px solid ${(theme.vars || theme).palette.action.disabled}`
|
209 | }
|
210 | }
|
211 | }
|
212 | }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter(['dark'])).map(([color]) => ({
|
213 | props: {
|
214 | variant: 'contained',
|
215 | color
|
216 | },
|
217 | style: {
|
218 | [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
|
219 | borderColor: (theme.vars || theme).palette[color].dark
|
220 | }
|
221 | }
|
222 | }))],
|
223 | [`& .${buttonGroupClasses.grouped}`]: {
|
224 | minWidth: 40,
|
225 | boxShadow: 'none',
|
226 | props: {
|
227 | variant: 'contained'
|
228 | },
|
229 | style: {
|
230 | '&:hover': {
|
231 | boxShadow: 'none'
|
232 | }
|
233 | }
|
234 | }
|
235 | })));
|
236 | const ButtonGroup = React.forwardRef(function ButtonGroup(inProps, ref) {
|
237 | const props = useDefaultProps({
|
238 | props: inProps,
|
239 | name: 'MuiButtonGroup'
|
240 | });
|
241 | const {
|
242 | children,
|
243 | className,
|
244 | color = 'primary',
|
245 | component = 'div',
|
246 | disabled = false,
|
247 | disableElevation = false,
|
248 | disableFocusRipple = false,
|
249 | disableRipple = false,
|
250 | fullWidth = false,
|
251 | orientation = 'horizontal',
|
252 | size = 'medium',
|
253 | variant = 'outlined',
|
254 | ...other
|
255 | } = props;
|
256 | const ownerState = {
|
257 | ...props,
|
258 | color,
|
259 | component,
|
260 | disabled,
|
261 | disableElevation,
|
262 | disableFocusRipple,
|
263 | disableRipple,
|
264 | fullWidth,
|
265 | orientation,
|
266 | size,
|
267 | variant
|
268 | };
|
269 | const classes = useUtilityClasses(ownerState);
|
270 | const context = React.useMemo(() => ({
|
271 | className: classes.grouped,
|
272 | color,
|
273 | disabled,
|
274 | disableElevation,
|
275 | disableFocusRipple,
|
276 | disableRipple,
|
277 | fullWidth,
|
278 | size,
|
279 | variant
|
280 | }), [color, disabled, disableElevation, disableFocusRipple, disableRipple, fullWidth, size, variant, classes.grouped]);
|
281 | const validChildren = getValidReactChildren(children);
|
282 | const childrenCount = validChildren.length;
|
283 | const getButtonPositionClassName = index => {
|
284 | const isFirstButton = index === 0;
|
285 | const isLastButton = index === childrenCount - 1;
|
286 | if (isFirstButton && isLastButton) {
|
287 | return '';
|
288 | }
|
289 | if (isFirstButton) {
|
290 | return classes.firstButton;
|
291 | }
|
292 | if (isLastButton) {
|
293 | return classes.lastButton;
|
294 | }
|
295 | return classes.middleButton;
|
296 | };
|
297 | return _jsx(ButtonGroupRoot, {
|
298 | as: component,
|
299 | role: "group",
|
300 | className: clsx(classes.root, className),
|
301 | ref: ref,
|
302 | ownerState: ownerState,
|
303 | ...other,
|
304 | children: _jsx(ButtonGroupContext.Provider, {
|
305 | value: context,
|
306 | children: validChildren.map((child, index) => {
|
307 | return _jsx(ButtonGroupButtonContext.Provider, {
|
308 | value: getButtonPositionClassName(index),
|
309 | children: child
|
310 | }, index);
|
311 | })
|
312 | })
|
313 | });
|
314 | });
|
315 | process.env.NODE_ENV !== "production" ? ButtonGroup.propTypes = {
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | |
321 |
|
322 |
|
323 | children: PropTypes.node,
|
324 | |
325 |
|
326 |
|
327 | classes: PropTypes.object,
|
328 | |
329 |
|
330 |
|
331 | className: PropTypes.string,
|
332 | |
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | color: PropTypes .oneOfType([PropTypes.oneOf(['inherit', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
|
339 | |
340 |
|
341 |
|
342 |
|
343 | component: PropTypes.elementType,
|
344 | |
345 |
|
346 |
|
347 |
|
348 | disabled: PropTypes.bool,
|
349 | |
350 |
|
351 |
|
352 |
|
353 | disableElevation: PropTypes.bool,
|
354 | |
355 |
|
356 |
|
357 |
|
358 | disableFocusRipple: PropTypes.bool,
|
359 | |
360 |
|
361 |
|
362 |
|
363 | disableRipple: PropTypes.bool,
|
364 | |
365 |
|
366 |
|
367 |
|
368 | fullWidth: PropTypes.bool,
|
369 | |
370 |
|
371 |
|
372 |
|
373 | orientation: PropTypes.oneOf(['horizontal', 'vertical']),
|
374 | |
375 |
|
376 |
|
377 |
|
378 |
|
379 | size: PropTypes .oneOfType([PropTypes.oneOf(['small', 'medium', 'large']), PropTypes.string]),
|
380 | |
381 |
|
382 |
|
383 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
384 | |
385 |
|
386 |
|
387 |
|
388 | variant: PropTypes .oneOfType([PropTypes.oneOf(['contained', 'outlined', 'text']), PropTypes.string])
|
389 | } : void 0;
|
390 | export default ButtonGroup; |
\ | No newline at end of file |