Skip to content

Commit c6c61dd

Browse files
云水云水
authored andcommitted
feat: add checkbox disable api
1 parent 8fa6bf4 commit c6c61dd

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

doc/markdown/nCheckbox/zh-CN/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
在一组可选项中进行多项选择时;单独使用可以表示两种状态之间的切换,和 switch 类似。区别在于切换 switch 会直接触发状态改变,而 checkbox 一般用于状态标记,需要和提交操作配合。
77

88
## 代码演示
9+
### 基本使用
10+
最简单的checkbox
911
:::demo
1012
```html
1113
<template>
@@ -15,6 +17,18 @@
1517
```
1618
:::
1719

20+
### 不可用
21+
checkbox 不可用。
22+
:::demo
23+
```html
24+
<template>
25+
<n-checkbox :disabled="true" :checked="true">Checkbox</n-checkbox>
26+
<n-checkbox :disabled="true" :checked="false">Checkbox</n-checkbox>
27+
</template>
28+
29+
```
30+
:::
31+
1832
## API
1933

2034
| 参数 | 说明 | 类型 | 默认值 |

packages/n-checkbox/index.scss

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
margin: 0;
55
padding: 0;
66
height: 20px;
7+
line-height: unset;
78
.slot-text{
8-
margin: 0 10px;
9+
margin: 0 10px 0 25px;
910
display: inline-block;
11+
line-height: unset;
1012
font-family: PingFangSC-Regular;
1113
color: #575757;
1214
}
@@ -20,9 +22,13 @@
2022
height: 16px;
2123
border-radius: 3px;
2224
border: 1px solid #34c3ff;
23-
position: relative;
25+
position: absolute;
2426
cursor: pointer;
2527
}
28+
.n-checkbox-disble{
29+
cursor: not-allowed;
30+
border: 1px solid rgba(118, 118, 118, 0.3);
31+
}
2632
label::before {
2733
line-height: 20px;
2834
display: inline-block;
@@ -41,8 +47,16 @@
4147
input:checked+label {
4248
background: #34c3ff;
4349
}
50+
input:checked+label.n-checkbox-disble{
51+
background: rgba(245, 245, 245,1);
52+
}
4453
input:checked+label::before{
4554
opacity: 1;
4655
transform: all 0.5s;
4756
}
57+
input:checked+label.n-checkbox-disble::before{
58+
border: 2px solid rgba(185, 185, 185,1);
59+
border-top: none;
60+
border-right: none;
61+
}
4862
}

packages/n-checkbox/index.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
import { defineComponent, App, HTMLAttributes, SetupContext } from 'vue';
1+
import { defineComponent, App,PropType,HTMLAttributes, SetupContext } from 'vue';
22
import './index.scss';
33
import { guid } from '../../src/utils/util';
44

5-
const initDefaultProps = {
6-
checked: false,
7-
defaultChecked: false
5+
const CheckboxProps = {
6+
checked: {
7+
type: Boolean as PropType<false>,
8+
default: false
9+
},
10+
defaultChecked: {
11+
type: Boolean as PropType<false>,
12+
default: false
13+
},
14+
disabled: {
15+
type: Boolean as PropType<false>,
16+
default: false
17+
}
818
};
9-
export interface CheckboxProps extends HTMLAttributes {
10-
checked?: boolean;
11-
defaultChecked?: boolean;
12-
}
1319

1420
const nCheckbox = defineComponent({
1521
name: 'NCheckbox',
16-
setup (_:CheckboxProps, { slots, attrs }: SetupContext) {
17-
const props = attrs as CheckboxProps;
22+
props: CheckboxProps,
23+
setup (props, { slots }: SetupContext) {
1824

19-
const { checked, defaultChecked } = { ...initDefaultProps, ...props };
2025

2126
const id = guid();
2227
const checkeval = () => {
23-
if (checked) {
24-
return checked;
28+
if (props.checked) {
29+
return props.checked;
2530
}
26-
return defaultChecked;
31+
return false;
2732
};
2833
return () => (
2934
<div class="n-checkbox">
30-
<input id={id} type="checkbox" checked={checkeval()}/>
31-
<label for={id} />
35+
<input id={id} class={[props.disabled ? 'n-checkbox-disble': '']} disabled={props.disabled} type="checkbox" checked={checkeval()}/>
36+
<label for={id} class={[props.disabled ? 'n-checkbox-disble': '']}/>
3237
<div class="slot-text">
3338
{slots.default && slots.default()}
3439
</div>

0 commit comments

Comments
 (0)