useFormItem.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import type { UnwrapRef, Ref, WritableComputedRef, DeepReadonly } from 'vue';
  2. import { reactive, readonly, computed, getCurrentInstance, watchEffect, unref, nextTick, toRaw } from 'vue';
  3. import { Form } from 'ant-design-vue';
  4. import { FormItemContext } from 'ant-design-vue/es/form/FormItemContext';
  5. import { isEqual } from 'lodash-es';
  6. export function useRuleFormItem<T extends Recordable, K extends keyof T, V = UnwrapRef<T[K]>>(
  7. props: T,
  8. key?: K,
  9. changeEvent?,
  10. emitData?: Ref<any[] | undefined>
  11. ): [WritableComputedRef<V>, (val: V) => void, DeepReadonly<V>, FormItemContext];
  12. export function useRuleFormItem<T extends Recordable>(props: T, key: keyof T = 'value', changeEvent = 'change', emitData?: Ref<any[]>) {
  13. const instance = getCurrentInstance();
  14. const emit = instance?.emit;
  15. const formItemContext = Form.useInjectFormItemContext();
  16. const innerState = reactive({
  17. value: props[key],
  18. });
  19. const defaultState = readonly(innerState);
  20. const setState = (val: UnwrapRef<T[keyof T]>): void => {
  21. innerState.value = val as T[keyof T];
  22. };
  23. watchEffect(() => {
  24. innerState.value = props[key];
  25. });
  26. const state: any = computed({
  27. get() {
  28. //修复多选时空值显示问题(兼容值为0的情况)
  29. return innerState.value == null || innerState.value === '' ? [] : innerState.value;
  30. },
  31. set(value) {
  32. if (isEqual(value, defaultState.value)) return;
  33. innerState.value = value as T[keyof T];
  34. nextTick(() => {
  35. emit?.(changeEvent, value, ...(toRaw(unref(emitData)) || []));
  36. // https://antdv.com/docs/vue/migration-v3-cn
  37. // antDv3升级后需要调用这个方法更新校验的值
  38. nextTick(() => formItemContext.onFieldChange());
  39. });
  40. },
  41. });
  42. return [state, setState, defaultState, formItemContext];
  43. }