You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

81 lines
2.9 KiB

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