Skip to content

Commit

Permalink
feat(form): 新增禁用(disabled)属性 (#2690)
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-boide authored Feb 9, 2024
1 parent 791e8b0 commit 14da68a
Show file tree
Hide file tree
Showing 24 changed files with 158 additions and 90 deletions.
6 changes: 4 additions & 2 deletions src/packages/__VUE/checkbox/index.taro.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script lang="ts">
import { h, computed, inject, getCurrentInstance, onMounted, reactive, watch, onBeforeUnmount } from 'vue';
import { h, computed, inject, getCurrentInstance, onMounted, reactive, watch, onBeforeUnmount, toRef } from 'vue';
import type { Component, PropType } from 'vue';
import { createComponent } from '@/packages/utils/create';
import { CheckNormal, Checked, CheckDisabled } from '@nutui/icons-vue-taro';
import { pxCheck } from '@/packages/utils/pxCheck';
import { CHECKBOX_KEY, CheckboxTextPosition, CheckboxShape } from './types';
import { useFormDisabled } from '../form/common';
const { create, componentName } = createComponent('checkbox');
export default create({
Expand Down Expand Up @@ -40,6 +41,7 @@ export default create({
},
emits: ['change', 'update:modelValue'],
setup(props, { emit, slots }) {
const disabled = useFormDisabled(toRef(props, 'disabled'));
const parent: any = inject(CHECKBOX_KEY, null);
const state = reactive({
partialSelect: props.indeterminate
Expand All @@ -56,7 +58,7 @@ export default create({
});
const pDisabled = computed(() => {
return hasParent.value ? (parent.disabled.value ? parent.disabled.value : props.disabled) : props.disabled;
return hasParent.value ? (parent.disabled.value ? parent.disabled.value : disabled.value) : disabled.value;
});
const checked = computed(() => !!props.modelValue);
Expand Down
6 changes: 4 additions & 2 deletions src/packages/__VUE/checkbox/index.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script lang="ts">
import { h, computed, inject, getCurrentInstance, onMounted, reactive, watch, onBeforeUnmount } from 'vue';
import { h, computed, inject, getCurrentInstance, onMounted, reactive, watch, onBeforeUnmount, toRef } from 'vue';
import type { Component, PropType } from 'vue';
import { createComponent } from '@/packages/utils/create';
import { CheckNormal, Checked, CheckDisabled } from '@nutui/icons-vue';
import { pxCheck } from '@/packages/utils/pxCheck';
import { CHECKBOX_KEY, CheckboxTextPosition, CheckboxShape } from './types';
import { useFormDisabled } from '../form/common';
const { create, componentName } = createComponent('checkbox');
export default create({
Expand Down Expand Up @@ -40,6 +41,7 @@ export default create({
},
emits: ['change', 'update:modelValue'],
setup(props, { emit, slots }) {
const disabled = useFormDisabled(toRef(props, 'disabled'));
const parent: any = inject(CHECKBOX_KEY, null);
const state = reactive({
partialSelect: props.indeterminate
Expand All @@ -56,7 +58,7 @@ export default create({
});
const pDisabled = computed(() => {
return hasParent.value ? (parent.disabled.value ? parent.disabled.value : props.disabled) : props.disabled;
return hasParent.value ? (parent.disabled.value ? parent.disabled.value : disabled.value) : disabled.value;
});
const checked = computed(() => !!props.modelValue);
Expand Down
16 changes: 13 additions & 3 deletions src/packages/__VUE/form/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { getPropByPath, isPromise } from '@/packages/utils/util';
import { computed, PropType, provide, reactive, watch } from 'vue';
import { computed, provide, reactive, watch } from 'vue';
import { useChildren, useParent } from '@/packages/utils';
import { FORM_KEY } from './types';
import { useChildren } from '@/packages/utils';
import type { ComputedRef, PropType, Ref } from 'vue';
import type { FormItemRule } from '../formitem/types';
import type { ErrorMessage, FormRule, FormRules, FormLabelPosition, FormStarPosition } from './types';

export const useFormDisabled = (disabled: Ref<boolean>): ComputedRef<boolean> => {
const { parent } = useParent(FORM_KEY);
return computed(() => disabled.value || parent?.props?.disabled || false);
};

export const component = (components: any) => {
return {
props: {
Expand All @@ -16,6 +22,10 @@ export const component = (components: any) => {
type: Object as PropType<FormRules>,
default: () => ({})
},
disabled: {
type: Boolean,
default: false
},
labelPosition: {
type: String as PropType<FormLabelPosition>,
default: 'left'
Expand Down Expand Up @@ -71,7 +81,7 @@ export const component = (components: any) => {
};

const checkRule = async (item: FormRule): Promise<ErrorMessage | boolean> => {
const { rules, prop } = item;
const { rules = [], prop } = item;

const _Promise = (errorMsg: ErrorMessage): Promise<ErrorMessage> => {
return new Promise((resolve, reject) => {
Expand Down
1 change: 1 addition & 0 deletions src/packages/__VUE/form/doc.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ app.use(FormItem);
| --- | --- | --- | --- |
| model-value | Form data object (required when using form verification) | object | |
| rules | Unified configuration FormItem attr rules | { prop: FormItemRule[] } | `{}` |
| disabled | Disable all data entry components under the form | boolean | `false` |
| label-position`v4.2.4` | The location of the form item label | `top` \| `left` \| `right` | `left` |
| star-position`v4.2.4` | The red star position of the single label is required | `left` \| `right` | `left` |

Expand Down
1 change: 1 addition & 0 deletions src/packages/__VUE/form/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ app.use(FormItem);
| --- | --- | --- | --- |
| model-value | 表单数据对象(使用表单校验时,_必填_) | object | |
| rules | 统一配置每个 `FormItem``rules` | { prop: FormItemRule[] } | `{}` |
| disabled | 禁用表单下的所有数据录入组件 | boolean | `false` |
| label-position`v4.2.4` | 表单项 label 的位置 | `top` \| `left` \| `right` | `left` |
| star-position`v4.2.4` | 必填表单项 label 的红色星标位置 | `left` \| `right` | `left` |

Expand Down
1 change: 1 addition & 0 deletions src/packages/__VUE/form/doc.taro.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ app.use(FormItem);
| --- | --- | --- | --- |
| model-value | 表单数据对象(使用表单校验时,_必填_) | object | |
| rules | 统一配置每个 `FormItem``rules` | { prop: FormItemRule[] } | `{}` |
| disabled | 禁用表单下的所有数据录入组件 | boolean | `false` |
| label-position`v4.2.4` | 表单项 label 的位置 | `top` \| `left` \| `right` | `left` |
| star-position`v4.2.4` | 必填表单项 label 的红色星标位置 | `left` \| `right` | `left` |

Expand Down
20 changes: 12 additions & 8 deletions src/packages/__VUE/input/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@
</view>
</template>
<script lang="ts">
import { PropType, ref, reactive, computed, onMounted, watch, ComputedRef, h } from 'vue';
import Taro from '@tarojs/taro';
import { MaskClose } from '@nutui/icons-vue-taro';
import { ref, reactive, computed, onMounted, watch, h, toRef } from 'vue';
import { createComponent } from '@/packages/utils/create';
import { formatNumber } from './util';
import { MaskClose } from '@nutui/icons-vue-taro';
import Taro from '@tarojs/taro';
import { useFormDisabled } from '../form/common';
import type { PropType, ComputedRef } from 'vue';
import type { InputType, InputAlignType, InputFormatTrigger, InputTarget, ConfirmTextType, InputEvent } from './type';
const { componentName, create } = createComponent('input');
Expand Down Expand Up @@ -158,6 +160,7 @@ export default create({
emits: ['update:modelValue', 'blur', 'focus', 'clear', 'keypress', 'click', 'clickInput', 'confirm'],
setup(props, { emit }) {
const disabled = useFormDisabled(toRef(props, 'disabled'));
const active = ref(false);
const inputRef = ref();
Expand Down Expand Up @@ -193,7 +196,7 @@ export default create({
const prefixCls = componentName;
return {
[prefixCls]: true,
[`${prefixCls}--disabled`]: props.disabled,
[`${prefixCls}--disabled`]: disabled.value,
[`${prefixCls}--required`]: props.required,
[`${prefixCls}--error`]: props.error,
[`${prefixCls}--border`]: props.border,
Expand Down Expand Up @@ -243,7 +246,7 @@ export default create({
};
const onFocus = (event: Event) => {
if (props.disabled || props.readonly) {
if (disabled.value || props.readonly) {
return;
}
active.value = true;
Expand All @@ -252,7 +255,7 @@ export default create({
};
const onBlur = (event: Event) => {
if (props.disabled || props.readonly) {
if (disabled.value || props.readonly) {
return;
}
setTimeout(() => {
Expand All @@ -271,7 +274,7 @@ export default create({
const clear = (event: Event) => {
event.stopPropagation();
if (props.disabled) return;
if (disabled.value) return;
emit('update:modelValue', '', event);
// emit('change', '', event);
emit('clear', '', event);
Expand All @@ -285,7 +288,7 @@ export default create({
};
const onClickInput = (event: MouseEvent) => {
if (props.disabled) {
if (disabled.value) {
return;
}
emit('clickInput', event);
Expand Down Expand Up @@ -341,6 +344,7 @@ export default create({
active,
classes,
styles,
disabled,
onInput,
onFocus,
onBlur,
Expand Down
17 changes: 10 additions & 7 deletions src/packages/__VUE/input/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@
</view>
</template>
<script lang="ts">
import { PropType, ref, reactive, computed, onMounted, watch, ComputedRef, h } from 'vue';
import { PropType, ref, reactive, computed, onMounted, watch, ComputedRef, h, toRef } from 'vue';
import { MaskClose } from '@nutui/icons-vue';
import { createComponent } from '@/packages/utils/create';
import { formatNumber, mapInputType } from './util';
import { MaskClose } from '@nutui/icons-vue';
import { useFormDisabled } from '../form/common';
import type { InputType, InputAlignType, InputFormatTrigger, InputTarget, ConfirmTextType } from './type';
Expand Down Expand Up @@ -146,6 +147,7 @@ export default create({
expose: ['focus', 'blur', 'select'],
setup(props, { emit }) {
const disabled = useFormDisabled(toRef(props, 'disabled'));
const active = ref(false);
const inputRef = ref();
const getModelValue = () => String(props.modelValue ?? '');
Expand All @@ -162,7 +164,7 @@ export default create({
const prefixCls = componentName;
return {
[prefixCls]: true,
[`${prefixCls}--disabled`]: props.disabled,
[`${prefixCls}--disabled`]: disabled.value,
[`${prefixCls}--required`]: props.required,
[`${prefixCls}--error`]: props.error,
[`${prefixCls}--border`]: props.border,
Expand Down Expand Up @@ -208,7 +210,7 @@ export default create({
};
const onFocus = (event: Event) => {
if (props.disabled || props.readonly) {
if (disabled.value || props.readonly) {
return;
}
active.value = true;
Expand All @@ -217,7 +219,7 @@ export default create({
};
const onBlur = (event: Event) => {
if (props.disabled || props.readonly) {
if (disabled.value || props.readonly) {
return;
}
setTimeout(() => {
Expand All @@ -236,7 +238,7 @@ export default create({
const clear = (event: Event) => {
event.stopPropagation();
if (props.disabled) return;
if (disabled.value) return;
emit('update:modelValue', '', event);
// emit('change', '', event);
emit('clear', '', event);
Expand All @@ -250,7 +252,7 @@ export default create({
};
const onClickInput = (event: MouseEvent) => {
if (props.disabled) {
if (disabled.value) {
return;
}
emit('clickInput', event);
Expand Down Expand Up @@ -306,6 +308,7 @@ export default create({
active,
classes,
styles,
disabled,
onInput,
onFocus,
onBlur,
Expand Down
19 changes: 11 additions & 8 deletions src/packages/__VUE/inputnumber/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
</view>
</template>
<script lang="ts">
import { computed, watch } from 'vue';
import { computed, toRef, watch } from 'vue';
import { createComponent } from '@/packages/utils/create';
import { pxCheck } from '@/packages/utils/pxCheck';
import { Minus, Plus } from '@nutui/icons-vue-taro';
import { useFormDisabled } from '../form/common';
const { componentName, create } = createComponent('input-number');
export default create({
components: { Minus, Plus },
Expand Down Expand Up @@ -85,11 +86,12 @@ export default create({
},
emits: ['update:modelValue', 'change', 'blur', 'focus', 'reduce', 'add', 'overlimit'],
setup(props, { emit }) {
const disabled = useFormDisabled(toRef(props, 'disabled'));
const classes = computed(() => {
const prefixCls = componentName;
return {
[prefixCls]: true,
[`${prefixCls}--disabled`]: props.disabled
[`${prefixCls}--disabled`]: disabled.value
};
});
const fixedDecimalPlaces = (v: string | number): string => {
Expand All @@ -106,13 +108,13 @@ export default create({
if (Number(props.modelValue) !== Number(output_value)) emit('change', output_value, event);
};
const addAllow = (value = Number(props.modelValue)): boolean => {
return value < Number(props.max) && !props.disabled;
return value < Number(props.max) && !disabled.value;
};
const reduceAllow = (value = Number(props.modelValue)): boolean => {
return value > Number(props.min) && !props.disabled;
return value > Number(props.min) && !disabled.value;
};
const reduce = (event: Event) => {
if (props.disabled) return;
if (disabled.value) return;
emit('reduce', event);
let output_value = Number(props.modelValue) - Number(props.step);
if (reduceAllow() && output_value >= Number(props.min)) {
Expand All @@ -123,7 +125,7 @@ export default create({
}
};
const add = (event: Event) => {
if (props.disabled) return;
if (disabled.value) return;
emit('add', event);
let output_value = Number(props.modelValue) + Number(props.step);
if (addAllow() && output_value <= Number(props.max)) {
Expand All @@ -134,15 +136,15 @@ export default create({
}
};
const focus = (event: Event) => {
if (props.disabled) return;
if (disabled.value) return;
if (props.readonly) {
blur(event);
return;
}
emit('focus', event);
};
const blur = (event: Event) => {
if (props.disabled) return;
if (disabled.value) return;
if (props.readonly) return;
const input = event.target as HTMLInputElement;
let value = Number(input.value);
Expand Down Expand Up @@ -178,6 +180,7 @@ export default create({
return {
classes,
disabled,
change,
blur,
focus,
Expand Down
Loading

0 comments on commit 14da68a

Please sign in to comment.