1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.default = exports.styles = void 0;
|
11 |
|
12 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
13 |
|
14 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
15 |
|
16 | var React = _interopRequireWildcard(require("react"));
|
17 |
|
18 | var _reactIs = require("react-is");
|
19 |
|
20 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
21 |
|
22 | var _clsx = _interopRequireDefault(require("clsx"));
|
23 |
|
24 | var _isValueSelected = _interopRequireDefault(require("./isValueSelected"));
|
25 |
|
26 | var _styles = require("@material-ui/core/styles");
|
27 |
|
28 | var _utils = require("@material-ui/core/utils");
|
29 |
|
30 | var styles = function styles(theme) {
|
31 | return {
|
32 |
|
33 | root: {
|
34 | display: 'inline-flex',
|
35 | borderRadius: theme.shape.borderRadius
|
36 | },
|
37 |
|
38 |
|
39 | vertical: {
|
40 | flexDirection: 'column'
|
41 | },
|
42 |
|
43 |
|
44 | grouped: {},
|
45 |
|
46 |
|
47 | groupedHorizontal: {
|
48 | '&:not(:first-child)': {
|
49 | marginLeft: -1,
|
50 | borderLeft: '1px solid transparent',
|
51 | borderTopLeftRadius: 0,
|
52 | borderBottomLeftRadius: 0
|
53 | },
|
54 | '&:not(:last-child)': {
|
55 | borderTopRightRadius: 0,
|
56 | borderBottomRightRadius: 0
|
57 | }
|
58 | },
|
59 |
|
60 |
|
61 | groupedVertical: {
|
62 | '&:not(:first-child)': {
|
63 | marginTop: -1,
|
64 | borderTop: '1px solid transparent',
|
65 | borderTopLeftRadius: 0,
|
66 | borderTopRightRadius: 0
|
67 | },
|
68 | '&:not(:last-child)': {
|
69 | borderBottomLeftRadius: 0,
|
70 | borderBottomRightRadius: 0
|
71 | }
|
72 | }
|
73 | };
|
74 | };
|
75 |
|
76 | exports.styles = styles;
|
77 | var ToggleButtonGroup = React.forwardRef(function ToggleButton(props, ref) {
|
78 | var children = props.children,
|
79 | classes = props.classes,
|
80 | className = props.className,
|
81 | _props$exclusive = props.exclusive,
|
82 | exclusive = _props$exclusive === void 0 ? false : _props$exclusive,
|
83 | onChange = props.onChange,
|
84 | _props$orientation = props.orientation,
|
85 | orientation = _props$orientation === void 0 ? 'horizontal' : _props$orientation,
|
86 | _props$size = props.size,
|
87 | size = _props$size === void 0 ? 'medium' : _props$size,
|
88 | value = props.value,
|
89 | other = (0, _objectWithoutProperties2.default)(props, ["children", "classes", "className", "exclusive", "onChange", "orientation", "size", "value"]);
|
90 |
|
91 | var handleChange = function handleChange(event, buttonValue) {
|
92 | if (!onChange) {
|
93 | return;
|
94 | }
|
95 |
|
96 | var index = value && value.indexOf(buttonValue);
|
97 | var newValue;
|
98 |
|
99 | if (value && index >= 0) {
|
100 | newValue = value.slice();
|
101 | newValue.splice(index, 1);
|
102 | } else {
|
103 | newValue = value ? value.concat(buttonValue) : [buttonValue];
|
104 | }
|
105 |
|
106 | onChange(event, newValue);
|
107 | };
|
108 |
|
109 | var handleExclusiveChange = function handleExclusiveChange(event, buttonValue) {
|
110 | if (!onChange) {
|
111 | return;
|
112 | }
|
113 |
|
114 | onChange(event, value === buttonValue ? null : buttonValue);
|
115 | };
|
116 |
|
117 | return React.createElement("div", (0, _extends2.default)({
|
118 | role: "group",
|
119 | className: (0, _clsx.default)(classes.root, className, orientation === 'vertical' && classes.vertical),
|
120 | ref: ref
|
121 | }, other), React.Children.map(children, function (child) {
|
122 | if (! React.isValidElement(child)) {
|
123 | return null;
|
124 | }
|
125 |
|
126 | if (process.env.NODE_ENV !== 'production') {
|
127 | if ((0, _reactIs.isFragment)(child)) {
|
128 | console.error(["Material-UI: The ToggleButtonGroup component doesn't accept a Fragment as a child.", 'Consider providing an array instead.'].join('\n'));
|
129 | }
|
130 | }
|
131 |
|
132 | return React.cloneElement(child, {
|
133 | className: (0, _clsx.default)(classes.grouped, classes["grouped".concat((0, _utils.capitalize)(orientation))], child.props.className),
|
134 | onChange: exclusive ? handleExclusiveChange : handleChange,
|
135 | selected: child.props.selected === undefined ? (0, _isValueSelected.default)(child.props.value, value) : child.props.selected,
|
136 | size: child.props.size || size
|
137 | });
|
138 | }));
|
139 | });
|
140 | process.env.NODE_ENV !== "production" ? ToggleButtonGroup.propTypes = {
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | |
147 |
|
148 |
|
149 | children: _propTypes.default.node,
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 | classes: _propTypes.default.object,
|
156 |
|
157 | |
158 |
|
159 |
|
160 | className: _propTypes.default.string,
|
161 |
|
162 | |
163 |
|
164 |
|
165 | exclusive: _propTypes.default.bool,
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | onChange: _propTypes.default.func,
|
176 |
|
177 | |
178 |
|
179 |
|
180 | orientation: _propTypes.default.oneOf(['horizontal', 'vertical']),
|
181 |
|
182 | |
183 |
|
184 |
|
185 | size: _propTypes.default.oneOf(['large', 'medium', 'small']),
|
186 |
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | value: _propTypes.default.any
|
194 | } : void 0;
|
195 |
|
196 | var _default = (0, _styles.withStyles)(styles, {
|
197 | name: 'MuiToggleButtonGroup'
|
198 | })(ToggleButtonGroup);
|
199 |
|
200 | exports.default = _default; |
\ | No newline at end of file |