UNPKG

3.5 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3var _excluded = ["id", "bsPrefix", "bsCustomPrefix", "inline", "disabled", "isValid", "isInvalid", "feedbackTooltip", "feedback", "className", "style", "title", "type", "label", "children", "custom", "as"];
4import classNames from 'classnames';
5import all from 'prop-types-extra/lib/all';
6import React, { useContext, useMemo } from 'react';
7import Feedback from './Feedback';
8import FormCheckInput from './FormCheckInput';
9import FormCheckLabel from './FormCheckLabel';
10import FormContext from './FormContext';
11import { useBootstrapPrefix } from './ThemeProvider';
12var FormCheck = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
13 var id = _ref.id,
14 bsPrefix = _ref.bsPrefix,
15 bsCustomPrefix = _ref.bsCustomPrefix,
16 _ref$inline = _ref.inline,
17 inline = _ref$inline === void 0 ? false : _ref$inline,
18 _ref$disabled = _ref.disabled,
19 disabled = _ref$disabled === void 0 ? false : _ref$disabled,
20 _ref$isValid = _ref.isValid,
21 isValid = _ref$isValid === void 0 ? false : _ref$isValid,
22 _ref$isInvalid = _ref.isInvalid,
23 isInvalid = _ref$isInvalid === void 0 ? false : _ref$isInvalid,
24 _ref$feedbackTooltip = _ref.feedbackTooltip,
25 feedbackTooltip = _ref$feedbackTooltip === void 0 ? false : _ref$feedbackTooltip,
26 feedback = _ref.feedback,
27 className = _ref.className,
28 style = _ref.style,
29 _ref$title = _ref.title,
30 title = _ref$title === void 0 ? '' : _ref$title,
31 _ref$type = _ref.type,
32 type = _ref$type === void 0 ? 'checkbox' : _ref$type,
33 label = _ref.label,
34 children = _ref.children,
35 propCustom = _ref.custom,
36 _ref$as = _ref.as,
37 as = _ref$as === void 0 ? 'input' : _ref$as,
38 props = _objectWithoutPropertiesLoose(_ref, _excluded);
39
40 var custom = type === 'switch' ? true : propCustom;
41
42 var _ref2 = custom ? [bsCustomPrefix, 'custom-control'] : [bsPrefix, 'form-check'],
43 prefix = _ref2[0],
44 defaultPrefix = _ref2[1];
45
46 bsPrefix = useBootstrapPrefix(prefix, defaultPrefix);
47
48 var _useContext = useContext(FormContext),
49 controlId = _useContext.controlId;
50
51 var innerFormContext = useMemo(function () {
52 return {
53 controlId: id || controlId,
54 custom: custom
55 };
56 }, [controlId, custom, id]);
57 var hasLabel = custom || label != null && label !== false && !children;
58 var input = /*#__PURE__*/React.createElement(FormCheckInput, _extends({}, props, {
59 type: type === 'switch' ? 'checkbox' : type,
60 ref: ref,
61 isValid: isValid,
62 isInvalid: isInvalid,
63 isStatic: !hasLabel,
64 disabled: disabled,
65 as: as
66 }));
67 return /*#__PURE__*/React.createElement(FormContext.Provider, {
68 value: innerFormContext
69 }, /*#__PURE__*/React.createElement("div", {
70 style: style,
71 className: classNames(className, bsPrefix, custom && "custom-" + type, inline && bsPrefix + "-inline")
72 }, children || /*#__PURE__*/React.createElement(React.Fragment, null, input, hasLabel && /*#__PURE__*/React.createElement(FormCheckLabel, {
73 title: title
74 }, label), (isValid || isInvalid) && /*#__PURE__*/React.createElement(Feedback, {
75 type: isValid ? 'valid' : 'invalid',
76 tooltip: feedbackTooltip
77 }, feedback))));
78});
79FormCheck.displayName = 'FormCheck';
80FormCheck.Input = FormCheckInput;
81FormCheck.Label = FormCheckLabel;
82export default FormCheck;
\No newline at end of file