index.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  5. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  6. var _excluded = ["prefixCls", "className", "style", "checked", "disabled", "defaultChecked", "type", "title", "onChange"];
  7. import classNames from 'classnames';
  8. import useMergedState from "rc-util/es/hooks/useMergedState";
  9. import * as React from 'react';
  10. import { forwardRef, useImperativeHandle, useRef } from 'react';
  11. export var Checkbox = /*#__PURE__*/forwardRef(function (props, ref) {
  12. var _props$prefixCls = props.prefixCls,
  13. prefixCls = _props$prefixCls === void 0 ? 'rc-checkbox' : _props$prefixCls,
  14. className = props.className,
  15. style = props.style,
  16. checked = props.checked,
  17. disabled = props.disabled,
  18. _props$defaultChecked = props.defaultChecked,
  19. defaultChecked = _props$defaultChecked === void 0 ? false : _props$defaultChecked,
  20. _props$type = props.type,
  21. type = _props$type === void 0 ? 'checkbox' : _props$type,
  22. title = props.title,
  23. onChange = props.onChange,
  24. inputProps = _objectWithoutProperties(props, _excluded);
  25. var inputRef = useRef(null);
  26. var holderRef = useRef(null);
  27. var _useMergedState = useMergedState(defaultChecked, {
  28. value: checked
  29. }),
  30. _useMergedState2 = _slicedToArray(_useMergedState, 2),
  31. rawValue = _useMergedState2[0],
  32. setRawValue = _useMergedState2[1];
  33. useImperativeHandle(ref, function () {
  34. return {
  35. focus: function focus(options) {
  36. var _inputRef$current;
  37. (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus(options);
  38. },
  39. blur: function blur() {
  40. var _inputRef$current2;
  41. (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || _inputRef$current2.blur();
  42. },
  43. input: inputRef.current,
  44. nativeElement: holderRef.current
  45. };
  46. });
  47. var classString = classNames(prefixCls, className, _defineProperty(_defineProperty({}, "".concat(prefixCls, "-checked"), rawValue), "".concat(prefixCls, "-disabled"), disabled));
  48. var handleChange = function handleChange(e) {
  49. if (disabled) {
  50. return;
  51. }
  52. if (!('checked' in props)) {
  53. setRawValue(e.target.checked);
  54. }
  55. onChange === null || onChange === void 0 || onChange({
  56. target: _objectSpread(_objectSpread({}, props), {}, {
  57. type: type,
  58. checked: e.target.checked
  59. }),
  60. stopPropagation: function stopPropagation() {
  61. e.stopPropagation();
  62. },
  63. preventDefault: function preventDefault() {
  64. e.preventDefault();
  65. },
  66. nativeEvent: e.nativeEvent
  67. });
  68. };
  69. return /*#__PURE__*/React.createElement("span", {
  70. className: classString,
  71. title: title,
  72. style: style,
  73. ref: holderRef
  74. }, /*#__PURE__*/React.createElement("input", _extends({}, inputProps, {
  75. className: "".concat(prefixCls, "-input"),
  76. ref: inputRef,
  77. onChange: handleChange,
  78. disabled: disabled,
  79. checked: !!rawValue,
  80. type: type
  81. })), /*#__PURE__*/React.createElement("span", {
  82. className: "".concat(prefixCls, "-inner")
  83. }));
  84. });
  85. export default Checkbox;