This commit is contained in:
2026-05-25 17:08:18 +08:00
parent df501f6151
commit f1259b71b5
9178 changed files with 1626125 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
The MIT License (MIT) Copyright (c) 2016 React Components
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+112
View File
@@ -0,0 +1,112 @@
# rc-checkbox
---
Checkbox ui component for react.
[![NPM version][npm-image]][npm-url]
[![npm download][download-image]][download-url]
[![build status][github-actions-image]][github-actions-url]
[![Codecov][codecov-image]][codecov-url]
[![bundle size][bundlephobia-image]][bundlephobia-url]
[![dumi][dumi-image]][dumi-url]
[npm-image]: http://img.shields.io/npm/v/rc-checkbox.svg?style=flat-square
[npm-url]: http://npmjs.org/package/rc-checkbox
[travis-image]: https://img.shields.io/travis/react-component/checkbox/master?style=flat-square
[travis-url]: https://travis-ci.com/react-component/checkbox
[github-actions-image]: https://github.com/react-component/checkbox/workflows/CI/badge.svg
[github-actions-url]: https://github.com/react-component/checkbox/actions
[codecov-image]: https://img.shields.io/codecov/c/github/react-component/checkbox/master.svg?style=flat-square
[codecov-url]: https://app.codecov.io/gh/react-component/checkbox
[david-url]: https://david-dm.org/react-component/checkbox
[david-image]: https://david-dm.org/react-component/checkbox/status.svg?style=flat-square
[david-dev-url]: https://david-dm.org/react-component/checkbox?type=dev
[david-dev-image]: https://david-dm.org/react-component/checkbox/dev-status.svg?style=flat-square
[download-image]: https://img.shields.io/npm/dm/rc-checkbox.svg?style=flat-square
[download-url]: https://npmjs.org/package/rc-checkbox
[bundlephobia-url]: https://bundlephobia.com/package/rc-checkbox
[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-checkbox
[dumi-url]: https://github.com/umijs/dumi
[dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square
## Install
[![rc-checkbox](https://nodei.co/npm/rc-checkbox.png)](https://npmjs.org/package/rc-checkbox)
## Usage
```js
import checkbox from '@rc-component/checkbox';
export default () => <checkbox />;
```
## Compatibility
| [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/electron/electron_48x48.png" alt="Electron" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Electron |
| --- | --- | --- | --- | --- |
| IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
## API
<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 100px;">name</th>
<th style="width: 50px;">type</th>
<th style="width: 50px;">default</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>prefixCls</td>
<td>String</td>
<td>rc-checkbox</td>
<td></td>
</tr>
<tr>
<td>className</td>
<td>String</td>
<td>''</td>
<td>additional class name of root node</td>
</tr>
<tr>
<td>name</td>
<td>String</td>
<td></td>
<td>same with native input checkbox</td>
</tr>
<tr>
<td>checked</td>
<td>enum: 0,1,2</td>
<td></td>
<td></td>
</tr>
<tr>
<td>defaultChecked</td>
<td>enum: 0,1,2</td>
<td>0</td>
<td>same with native input checkbox</td>
<tr>
<td>onChange</td>
<td>Function(e:Event, checked:Number)</td>
<td></td>
<td>called when checkbox is changed. e is native event, checked is original checked state.</td>
</tr>
</tbody>
</table>
## Development
```
npm install
npm start
```
Online demo: http://react-component.github.io/checkbox/
## License
rc-checkbox is released under the MIT license.
+109
View File
@@ -0,0 +1,109 @@
/* Default state */
.rc-checkbox {
position: relative;
display: inline-block;
width: 14px;
height: 14px;
line-height: 1;
white-space: nowrap;
vertical-align: middle;
background-color: #ffffff;
border-color: #d9d9d9;
border-style: solid;
border-width: 1px;
border-radius: 3px;
outline: none;
cursor: pointer;
transition: border-color 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55), background-color 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}
.rc-checkbox:hover,
.rc-checkbox-input:focus + .rc-checkbox {
border-color: #3dbcf6;
}
.rc-checkbox:after {
position: absolute;
top: 1px;
left: 4px;
display: table;
width: 5px;
height: 8px;
border: 2px solid #ffffff;
border-top: 0;
border-left: 0;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
animation-name: amCheckboxOut;
animation-duration: 0.3s;
animation-timing-function: cubic-bezier(0.68, -0.55, 0.27, 1.55);
content: ' ';
}
.rc-checkbox-input {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 9999;
cursor: pointer;
opacity: 0;
}
/* Checked state */
.rc-checkbox-checked {
background-color: #3dbcf6;
border-color: #3dbcf6;
}
.rc-checkbox-checked:hover {
border-color: #3dbcf6;
}
@media print {
.rc-checkbox-checked {
box-shadow: inset 0 0 0 16px #3dbcf6;
}
}
.rc-checkbox-disabled {
background-color: #f3f3f3;
border-color: #d9d9d9;
}
.rc-checkbox-disabled:after {
border-color: #f3f3f3;
animation-name: none;
}
.rc-checkbox-disabled.rc-checkbox-checked {
background-color: #f3f3f3;
border-color: #d9d9d9;
}
.rc-checkbox-disabled.rc-checkbox-checked:after {
border-color: #cccccc;
animation-name: none;
}
@media print {
.rc-checkbox-disabled.rc-checkbox-checked {
box-shadow: inset 0 0 0 16px #f3f3f3;
}
}
.rc-checkbox-disabled:hover {
border-color: #d9d9d9;
}
.rc-checkbox-disabled .rc-checkbox-input {
cursor: default;
}
@keyframes amCheckboxIn {
0% {
transform: scale(0, 0) rotate(45deg);
transform-origin: 50% 50%;
opacity: 0;
}
100% {
transform: scale(1, 1) rotate(45deg);
transform-origin: 50% 50%;
opacity: 1;
}
}
@keyframes amCheckboxOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
+22
View File
@@ -0,0 +1,22 @@
import * as React from 'react';
export interface CheckboxChangeEvent {
target: CheckboxChangeEventTarget;
stopPropagation: () => void;
preventDefault: () => void;
nativeEvent: React.ChangeEvent<HTMLInputElement>['nativeEvent'];
}
export interface CheckboxChangeEventTarget extends CheckboxProps {
checked: boolean;
}
export interface CheckboxRef {
focus: (options?: FocusOptions) => void;
blur: () => void;
input: HTMLInputElement | null;
nativeElement: HTMLElement | null;
}
export interface CheckboxProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> {
prefixCls?: string;
onChange?: (e: CheckboxChangeEvent) => void;
}
export declare const Checkbox: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<CheckboxRef>>;
export default Checkbox;
+72
View File
@@ -0,0 +1,72 @@
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import useControlledState from "@rc-component/util/es/hooks/useControlledState";
import { clsx } from 'clsx';
import * as React from 'react';
import { forwardRef, useImperativeHandle, useRef } from 'react';
export const Checkbox = /*#__PURE__*/forwardRef((props, ref) => {
const {
prefixCls = 'rc-checkbox',
className,
style,
checked,
disabled,
defaultChecked = false,
type = 'checkbox',
title,
onChange,
...inputProps
} = props;
const inputRef = useRef(null);
const holderRef = useRef(null);
const [rawValue, setRawValue] = useControlledState(defaultChecked, checked);
useImperativeHandle(ref, () => ({
focus: options => {
inputRef.current?.focus(options);
},
blur: () => {
inputRef.current?.blur();
},
input: inputRef.current,
nativeElement: holderRef.current
}));
const classString = clsx(prefixCls, className, {
[`${prefixCls}-checked`]: rawValue,
[`${prefixCls}-disabled`]: disabled
});
const handleChange = e => {
if (disabled) {
return;
}
if (!('checked' in props)) {
setRawValue(e.target.checked);
}
onChange?.({
target: {
...props,
type,
checked: e.target.checked
},
stopPropagation() {
e.stopPropagation();
},
preventDefault() {
e.preventDefault();
},
nativeEvent: e.nativeEvent
});
};
return /*#__PURE__*/React.createElement("span", {
className: classString,
title: title,
style: style,
ref: holderRef
}, /*#__PURE__*/React.createElement("input", _extends({}, inputProps, {
className: `${prefixCls}-input`,
ref: inputRef,
onChange: handleChange,
disabled: disabled,
checked: !!rawValue,
type: type
})));
});
export default Checkbox;
+22
View File
@@ -0,0 +1,22 @@
import * as React from 'react';
export interface CheckboxChangeEvent {
target: CheckboxChangeEventTarget;
stopPropagation: () => void;
preventDefault: () => void;
nativeEvent: React.ChangeEvent<HTMLInputElement>['nativeEvent'];
}
export interface CheckboxChangeEventTarget extends CheckboxProps {
checked: boolean;
}
export interface CheckboxRef {
focus: (options?: FocusOptions) => void;
blur: () => void;
input: HTMLInputElement | null;
nativeElement: HTMLElement | null;
}
export interface CheckboxProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> {
prefixCls?: string;
onChange?: (e: CheckboxChangeEvent) => void;
}
export declare const Checkbox: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<CheckboxRef>>;
export default Checkbox;
+81
View File
@@ -0,0 +1,81 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.Checkbox = void 0;
var _useControlledState = _interopRequireDefault(require("@rc-component/util/lib/hooks/useControlledState"));
var _clsx = require("clsx");
var _react = _interopRequireWildcard(require("react"));
var React = _react;
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const Checkbox = exports.Checkbox = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
const {
prefixCls = 'rc-checkbox',
className,
style,
checked,
disabled,
defaultChecked = false,
type = 'checkbox',
title,
onChange,
...inputProps
} = props;
const inputRef = (0, _react.useRef)(null);
const holderRef = (0, _react.useRef)(null);
const [rawValue, setRawValue] = (0, _useControlledState.default)(defaultChecked, checked);
(0, _react.useImperativeHandle)(ref, () => ({
focus: options => {
inputRef.current?.focus(options);
},
blur: () => {
inputRef.current?.blur();
},
input: inputRef.current,
nativeElement: holderRef.current
}));
const classString = (0, _clsx.clsx)(prefixCls, className, {
[`${prefixCls}-checked`]: rawValue,
[`${prefixCls}-disabled`]: disabled
});
const handleChange = e => {
if (disabled) {
return;
}
if (!('checked' in props)) {
setRawValue(e.target.checked);
}
onChange?.({
target: {
...props,
type,
checked: e.target.checked
},
stopPropagation() {
e.stopPropagation();
},
preventDefault() {
e.preventDefault();
},
nativeEvent: e.nativeEvent
});
};
return /*#__PURE__*/React.createElement("span", {
className: classString,
title: title,
style: style,
ref: holderRef
}, /*#__PURE__*/React.createElement("input", _extends({}, inputProps, {
className: `${prefixCls}-input`,
ref: inputRef,
onChange: handleChange,
disabled: disabled,
checked: !!rawValue,
type: type
})));
});
var _default = exports.default = Checkbox;
+82
View File
@@ -0,0 +1,82 @@
{
"name": "@rc-component/checkbox",
"version": "2.0.0",
"description": "checkbox ui component for react",
"keywords": [
"react",
"react-component",
"react-checkbox",
"checkbox"
],
"homepage": "https://react-component.github.io/checkbox",
"bugs": {
"url": "http://github.com/react-component/checkbox/issues"
},
"repository": {
"type": "git",
"url": "git@github.com:react-component/checkbox.git"
},
"license": "MIT",
"main": "lib/index.js",
"module": "es/index.js",
"typings": "es/index.d.ts",
"files": [
"assets/*.css",
"es",
"lib"
],
"scripts": {
"build": "dumi build",
"compile": "father build && lessc assets/index.less assets/index.css",
"coverage": "jest --coverage",
"gh-pages": "npm run build && father doc deploy -d dist",
"lint": "eslint src/ --ext .tsx,.ts",
"prepare": "husky install && dumi setup",
"prepublishOnly": "npm run compile && rc-np",
"start": "dumi dev",
"test": "jest",
"tsc": "bunx tsc --noEmit"
},
"lint-staged": {
"*.{js,jsx,less,md,json}": [
"prettier --write"
],
"*.ts?(x)": [
"prettier --parser=typescript --write"
]
},
"dependencies": {
"@rc-component/util": "^1.3.0",
"clsx": "^2.1.1"
},
"devDependencies": {
"@rc-component/father-plugin": "^2.1.3",
"@rc-component/np": "^1.0.0",
"@testing-library/jest-dom": "^6.1.5",
"@testing-library/react": "^14.1.2",
"@types/jest": "^29.2.4",
"@types/node": "^24.5.2",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@umijs/fabric": "^4.0.1",
"@umijs/test": "^4.0.34",
"dumi": "^2.0.12",
"eslint": "^8.55.0",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-unicorn": "^50.0.1",
"father": "^4.1.1",
"husky": "9.0.5",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"less": "^4.2.0",
"lint-staged": "^15.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"peerDependencies": {
"react": ">=16.9.0",
"react-dom": ">=16.9.0"
}
}