UNPKG

6.34 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var React = _interopRequireWildcard(require("react"));
17
18var _reactIs = require("react-is");
19
20var _propTypes = _interopRequireDefault(require("prop-types"));
21
22var _clsx = _interopRequireDefault(require("clsx"));
23
24var _isValueSelected = _interopRequireDefault(require("./isValueSelected"));
25
26var _styles = require("@material-ui/core/styles");
27
28var _utils = require("@material-ui/core/utils");
29
30var styles = function styles(theme) {
31 return {
32 /* Styles applied to the root element. */
33 root: {
34 display: 'inline-flex',
35 borderRadius: theme.shape.borderRadius
36 },
37
38 /* Styles applied to the root element if `orientation="vertical"`. */
39 vertical: {
40 flexDirection: 'column'
41 },
42
43 /* Styles applied to the children. */
44 grouped: {},
45
46 /* Styles applied to the children if `orientation="horizontal"`. */
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 /* Styles applied to the children if `orientation="vertical"`. */
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
76exports.styles = styles;
77var ToggleButtonGroup = /*#__PURE__*/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 /*#__PURE__*/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 (! /*#__PURE__*/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 /*#__PURE__*/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});
140process.env.NODE_ENV !== "production" ? ToggleButtonGroup.propTypes = {
141 // ----------------------------- Warning --------------------------------
142 // | These PropTypes are generated from the TypeScript type definitions |
143 // | To update them edit the d.ts file and run "yarn proptypes" |
144 // ----------------------------------------------------------------------
145
146 /**
147 * The content of the button.
148 */
149 children: _propTypes.default.node,
150
151 /**
152 * Override or extend the styles applied to the component.
153 * See [CSS API](#css) below for more details.
154 */
155 classes: _propTypes.default.object,
156
157 /**
158 * @ignore
159 */
160 className: _propTypes.default.string,
161
162 /**
163 * If `true`, only allow one of the child ToggleButton values to be selected.
164 */
165 exclusive: _propTypes.default.bool,
166
167 /**
168 * Callback fired when the value changes.
169 *
170 * @param {object} event The event source of the callback.
171 * @param {any} value of the selected buttons. When `exclusive` is true
172 * this is a single value; when false an array of selected values. If no value
173 * is selected and `exclusive` is true the value is null; when false an empty array.
174 */
175 onChange: _propTypes.default.func,
176
177 /**
178 * The group orientation (layout flow direction).
179 */
180 orientation: _propTypes.default.oneOf(['horizontal', 'vertical']),
181
182 /**
183 * The size of the buttons.
184 */
185 size: _propTypes.default.oneOf(['large', 'medium', 'small']),
186
187 /**
188 * The currently selected value within the group or an array of selected
189 * values when `exclusive` is false.
190 *
191 * The value must have reference equality with the option in order to be selected.
192 */
193 value: _propTypes.default.any
194} : void 0;
195
196var _default = (0, _styles.withStyles)(styles, {
197 name: 'MuiToggleButtonGroup'
198})(ToggleButtonGroup);
199
200exports.default = _default;
\No newline at end of file