UNPKG

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