UNPKG

3.98 kBJavaScriptView Raw
1var _excluded = ["active", "aria-label", "block", "className", "close", "cssModule", "color", "outline", "size", "tag", "innerRef"];
2function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
5import React, { useCallback } from 'react';
6import PropTypes from 'prop-types';
7import classNames from 'classnames';
8import { mapToCssModules, tagPropType } from './utils';
9import CloseButton from './CloseButton';
10var propTypes = {
11 /** Manually set the visual state of the button to active */
12 active: PropTypes.bool,
13 /** Aria label */
14 'aria-label': PropTypes.string,
15 block: PropTypes.bool,
16 /** Pass children so this component can wrap them */
17 children: PropTypes.node,
18 /** Add custom class */
19 className: PropTypes.string,
20 /** Change existing className with a new className */
21 cssModule: PropTypes.object,
22 /** Use the button as a close button */
23 close: PropTypes.bool,
24 /** Change color of Button to one of the available colors */
25 color: PropTypes.string,
26 /** Disables the button */
27 disabled: PropTypes.bool,
28 innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
29 /** Function to be triggered on click */
30 onClick: PropTypes.func,
31 /** Adds outline to the button */
32 outline: PropTypes.bool,
33 /** Make the button bigger or smaller */
34 size: PropTypes.string,
35 /** Set a custom element for this component */
36 tag: tagPropType
37};
38function Button(props) {
39 var onClick = useCallback(function (e) {
40 if (props.disabled) {
41 e.preventDefault();
42 return;
43 }
44 if (props.onClick) {
45 return props.onClick(e);
46 }
47 }, [props.onClick, props.disabled]);
48 var active = props.active,
49 ariaLabel = props['aria-label'],
50 block = props.block,
51 className = props.className,
52 close = props.close,
53 cssModule = props.cssModule,
54 _props$color = props.color,
55 color = _props$color === void 0 ? 'secondary' : _props$color,
56 outline = props.outline,
57 size = props.size,
58 _props$tag = props.tag,
59 Tag = _props$tag === void 0 ? 'button' : _props$tag,
60 innerRef = props.innerRef,
61 attributes = _objectWithoutProperties(props, _excluded);
62 if (close) {
63 return /*#__PURE__*/React.createElement(CloseButton, attributes);
64 }
65 var btnOutlineColor = "btn".concat(outline ? '-outline' : '', "-").concat(color);
66 var classes = mapToCssModules(classNames(className, 'btn', btnOutlineColor, size ? "btn-".concat(size) : false, block ? 'd-block w-100' : false, {
67 active: active,
68 disabled: props.disabled
69 }), cssModule);
70 if (attributes.href && Tag === 'button') {
71 Tag = 'a';
72 }
73 return /*#__PURE__*/React.createElement(Tag, _extends({
74 type: Tag === 'button' && attributes.onClick ? 'button' : undefined
75 }, attributes, {
76 className: classes,
77 ref: innerRef,
78 onClick: onClick,
79 "aria-label": ariaLabel
80 }));
81}
82Button.propTypes = propTypes;
83export default Button;
\No newline at end of file