chayns-components
Version:
A set of beautiful React components for developing chayns® applications.
121 lines (117 loc) • 4.98 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _Checkbox = _interopRequireDefault(require("../views/Checkbox"));
var _ToggleButton = _interopRequireDefault(require("../views/ToggleButton"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* @component
*/
let checkboxId = 1;
/**
* Checkboxes allow users to complete tasks that involve making choices such as
* selecting options. Can be styled as a switch, a visual toggle between two
* mutually exclusive states — on and off.
*/
const Checkbox = _ref => {
let {
id,
toggleButton = false,
disabled = false,
stopPropagation = false,
onChange,
...props
} = _ref;
const idRef = (0, _react.useRef)(`cc_checkbox_${checkboxId++}`);
const handleChange = e => {
if (!disabled && onChange) {
onChange(e.target.checked);
}
};
const currentId = id || idRef.current;
if (toggleButton) {
return /*#__PURE__*/_react.default.createElement(_ToggleButton.default, (0, _extends2.default)({}, props, {
id: currentId,
disabled: disabled,
onChange: handleChange,
stopPropagation: stopPropagation
}));
}
return /*#__PURE__*/_react.default.createElement(_Checkbox.default, (0, _extends2.default)({}, props, {
id: currentId,
disabled: disabled,
onChange: handleChange,
stopPropagation: stopPropagation
}));
};
var _default = Checkbox;
exports.default = _default;
Checkbox.propTypes = {
/**
* A React style object that will be applied to the CheckBox element.
*/
style: _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number])),
/**
* A classname string that will be applied to the CheckBox element.
*/
className: _propTypes.default.string,
/**
* A React style object that will be applied to the label element.
*/
labelStyle: _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number])),
/**
* A classname string that will be applied to the label element.
*/
labelClassName: _propTypes.default.string,
/**
* A label that will be shown next to the CheckBox.
*/
label: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.arrayOf(_propTypes.default.node)]),
/**
* A label that will be shown next to the CheckBox.
*/
children: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.arrayOf(_propTypes.default.node)]),
/**
* This will be called when the state of the CheckBox changes.
*/
onChange: _propTypes.default.func,
/**
* Changes the rendering to a switch-/toggle-style.
*/
toggleButton: _propTypes.default.bool,
/**
* Wether the CheckBox is checked. Makes it a controlled input.
*/
checked: _propTypes.default.bool,
/**
* Wether the CheckBox is checked by default. Do not use it with the
* `checked`-prop.
*/
defaultChecked: _propTypes.default.bool,
/**
* Disables any interactions with the CheckBox and changes the style to a
* disabled look.
*/
disabled: _propTypes.default.bool,
/**
* Set the contents of the label with a raw HTML string.
*/
dangerouslySetLabel: _propTypes.default.shape({
__html: _propTypes.default.string.isRequired
}),
/**
* Wether to stop propagation of click events.
*/
stopPropagation: _propTypes.default.bool,
/**
* The HTML id of the input element.
*/
id: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string])
};
Checkbox.displayName = 'Checkbox';
//# sourceMappingURL=Checkbox.js.map