Skip to content

Commit 2cfae39

Browse files
author
Tenny
committed
feat(Form): 表单支持 disabled
1 parent 1160370 commit 2cfae39

File tree

14 files changed

+373
-311
lines changed

14 files changed

+373
-311
lines changed

docs/components/form.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
}
6363
</script>
6464
<template>
65-
<nt-form :model="formFields" :rules="rules">
66-
<nt-form-item label="用户名" required name="username">
65+
<nt-form :model="formFields" :rules="rules" >
66+
<nt-form-item label="用户名" required name="username" >
6767
<nt-input placeholder="请输入用户名" v-model="formFields.username"></nt-input>
6868
</nt-form-item>
6969
<nt-form-item label="密码" required name="password">

docs/components/input.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@
6464
</CodePreview>
6565
</ClientOnly>
6666

67+
### 禁用状态
68+
69+
通过 `disabled` 属性设置输入框为禁用状态。
70+
71+
<ClientOnly>
72+
<CodePreview>
73+
<textarea lang="vue-html">
74+
<nt-input placeholder="请输入内容" disabled></nt-input>
75+
</textarea>
76+
</CodePreview>
77+
</ClientOnly>
78+
6779
## API
6880

6981
### Input Props

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import globals from 'globals';
22
import pluginJs from '@eslint/js';
33
import tseslint from 'typescript-eslint';
4-
import pluginVue from 'eslint-plugin-vue';
4+
import pluginVue, { rules } from 'eslint-plugin-vue';
55
import markdown from '@eslint/markdown';
66

77
export default [

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
"devDependencies": {
1717
"@eslint/js": "^9.10.0",
1818
"@eslint/markdown": "^6.1.0",
19-
"@types/node": "^22.5.4",
20-
"@vitejs/plugin-vue": "^5.1.3",
19+
"@types/node": "^22.5.5",
20+
"@vitejs/plugin-vue": "^5.1.4",
2121
"eslint": "^9.10.0",
2222
"eslint-plugin-vue": "^9.28.0",
2323
"globals": "^15.9.0",
2424
"less": "^4.2.0",
2525
"prettier": "^3.3.3",
26-
"shiki": "^1.17.0",
26+
"shiki": "^1.17.7",
2727
"typescript": "^5.6.2",
28-
"typescript-eslint": "^8.5.0",
29-
"vite": "^5.4.4",
28+
"typescript-eslint": "^8.6.0",
29+
"vite": "^5.4.6",
3030
"vite-plugin-dts": "4.2.1",
3131
"vitepress": "^1.3.4",
3232
"vue-tsc": "^2.1.6"
@@ -68,9 +68,9 @@
6868
"license": "MulanPSL-2.0",
6969
"dependencies": {
7070
"@tanstack/vue-table": "^8.20.5",
71-
"ph-utils": "^0.9.1",
71+
"ph-utils": "^0.9.3",
7272
"qrcode-generator-es": "^1.0.2",
73-
"vue-router": "^4.4.4"
73+
"vue-router": "^4.4.5"
7474
},
7575
"packageManager": "pnpm@9.0.2"
7676
}

pnpm-lock.yaml

Lines changed: 298 additions & 294 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/form/Form.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import { ref, provide, watch } from 'vue';
1717
import Validator from 'ph-utils/validator';
1818
import type { SchemaType } from 'ph-utils/validator';
19-
import { formContext } from './constant';
19+
import { formContext, formDisabledContext } from '../../utils/constant.js';
2020
2121
const props = withDefaults(
2222
defineProps<{
@@ -28,9 +28,11 @@ const props = withDefaults(
2828
rules?: SchemaType[];
2929
/** 是否行内表单 */
3030
inline?: boolean;
31+
disabled?: boolean;
3132
}>(),
3233
{
3334
inline: false,
35+
disabled: false,
3436
},
3537
);
3638
@@ -92,6 +94,8 @@ provide(formContext, {
9294
requiredKeys,
9395
});
9496
97+
provide(formDisabledContext, () => props.disabled);
98+
9599
function handleSubmit(e: Event) {
96100
e.preventDefault();
97101
if (validator != null) {

src/components/form/FormItem.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
</div>
2525
</template>
2626
<script setup lang="ts">
27-
import { Ref, computed, inject } from 'vue';
28-
import { formContext } from './constant';
27+
import { Ref, computed, inject, provide } from 'vue';
28+
import { formContext, formItemDisabledContext } from '../../utils/constant';
2929
3030
const props = withDefaults(
3131
defineProps<{
@@ -39,11 +39,13 @@ const props = withDefaults(
3939
name?: string;
4040
/** 表单域验证错误时的提示信息。设置该值会导致表单验证状态变为 error,并显示该错误信息。 */
4141
error?: string;
42+
disabled?: boolean;
4243
}>(),
4344
{
4445
required: undefined,
4546
error: undefined,
4647
label: undefined,
48+
disabled: undefined,
4749
},
4850
);
4951
@@ -78,4 +80,6 @@ const isRequired = computed(() => {
7880
}
7981
return false;
8082
});
83+
84+
provide(formItemDisabledContext, () => props.disabled);
8185
</script>

src/components/form/constant.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/components/input/Input.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
:placeholder="placeholder"
1010
@input="handleInput"
1111
ref="el"
12+
:disabled="isDisabled"
1213
/>
1314
</template>
1415

1516
<script setup lang="ts">
1617
import { ref } from 'vue';
18+
import useDisabled from '../../hooks/useFormDisabled.js';
1719
1820
const el = ref<HTMLInputElement>();
1921
@@ -24,16 +26,20 @@ const props = withDefaults(
2426
autosize?: boolean;
2527
parser?: (value: string) => string;
2628
modelValue?: string | number;
29+
disabled?: boolean;
2730
}>(),
2831
{
2932
htmlType: 'text',
3033
placeholder: '',
3134
autosize: false,
35+
disabled: undefined,
3236
},
3337
);
3438
3539
const emits = defineEmits(['update:modelValue', 'input']);
3640
41+
const isDisabled = useDisabled(() => props.disabled);
42+
3743
function focus() {
3844
if (el.value != null) {
3945
el.value.focus();

src/hooks/useFormDisabled.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { inject, computed, toValue } from 'vue';
2+
import {
3+
formDisabledContext,
4+
formItemDisabledContext,
5+
} from '../utils/constant.js';
6+
import type { MaybeRefOrGetter } from 'vue';
7+
8+
export default function useDisabled(
9+
disabled?: MaybeRefOrGetter<boolean | undefined>,
10+
) {
11+
const formDisabled = inject(formDisabledContext);
12+
const formItemDisabeld = inject(formItemDisabledContext);
13+
14+
const isDisabled = computed(() => {
15+
if (toValue(disabled) != null) return toValue<boolean>(disabled as boolean);
16+
if (toValue(formItemDisabeld) != null)
17+
return toValue<boolean>(formItemDisabeld as boolean);
18+
if (toValue(formDisabled) != null)
19+
return toValue<boolean>(formDisabled as boolean);
20+
});
21+
22+
return isDisabled;
23+
}

0 commit comments

Comments
 (0)