react-bootstrap-v5
Version:
Bootstrap 4 components built with React
72 lines (69 loc) • 3.19 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import classNames from 'classnames';
import React, { useContext, useMemo } from 'react';
import Feedback from './Feedback';
import FormCheckInput from './FormCheckInput';
import FormCheckLabel from './FormCheckLabel';
import FormContext from './FormContext';
import { useBootstrapPrefix } from './ThemeProvider';
var FormCheck = React.forwardRef(function (_ref, ref) {
var id = _ref.id,
bsPrefix = _ref.bsPrefix,
bsSwitchPrefix = _ref.bsSwitchPrefix,
_ref$inline = _ref.inline,
inline = _ref$inline === void 0 ? false : _ref$inline,
_ref$disabled = _ref.disabled,
disabled = _ref$disabled === void 0 ? false : _ref$disabled,
_ref$isValid = _ref.isValid,
isValid = _ref$isValid === void 0 ? false : _ref$isValid,
_ref$isInvalid = _ref.isInvalid,
isInvalid = _ref$isInvalid === void 0 ? false : _ref$isInvalid,
_ref$feedbackTooltip = _ref.feedbackTooltip,
feedbackTooltip = _ref$feedbackTooltip === void 0 ? false : _ref$feedbackTooltip,
feedback = _ref.feedback,
className = _ref.className,
style = _ref.style,
_ref$title = _ref.title,
title = _ref$title === void 0 ? '' : _ref$title,
_ref$type = _ref.type,
type = _ref$type === void 0 ? 'checkbox' : _ref$type,
label = _ref.label,
children = _ref.children,
_ref$as = _ref.as,
as = _ref$as === void 0 ? 'input' : _ref$as,
props = _objectWithoutPropertiesLoose(_ref, ["id", "bsPrefix", "bsSwitchPrefix", "inline", "disabled", "isValid", "isInvalid", "feedbackTooltip", "feedback", "className", "style", "title", "type", "label", "children", "as"]);
bsPrefix = useBootstrapPrefix(bsPrefix, 'form-check');
bsSwitchPrefix = useBootstrapPrefix(bsSwitchPrefix, 'form-switch');
var _useContext = useContext(FormContext),
controlId = _useContext.controlId;
var innerFormContext = useMemo(function () {
return {
controlId: id || controlId
};
}, [controlId, id]);
var hasLabel = label != null && label !== false && !children;
var input = /*#__PURE__*/React.createElement(FormCheckInput, _extends({}, props, {
type: type === 'switch' ? 'checkbox' : type,
ref: ref,
isValid: isValid,
isInvalid: isInvalid,
disabled: disabled,
as: as
}));
return /*#__PURE__*/React.createElement(FormContext.Provider, {
value: innerFormContext
}, /*#__PURE__*/React.createElement("div", {
style: style,
className: classNames(className, label && bsPrefix, inline && bsPrefix + "-inline", type === 'switch' && bsSwitchPrefix)
}, children || /*#__PURE__*/React.createElement(React.Fragment, null, input, hasLabel && /*#__PURE__*/React.createElement(FormCheckLabel, {
title: title
}, label), (isValid || isInvalid) && /*#__PURE__*/React.createElement(Feedback, {
type: isValid ? 'valid' : 'invalid',
tooltip: feedbackTooltip
}, feedback))));
});
FormCheck.displayName = 'FormCheck';
FormCheck.Input = FormCheckInput;
FormCheck.Label = FormCheckLabel;
export default FormCheck;