MyForm.vue 2.6 KB
Newer Older
hucy's avatar
hucy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
<template>
  <q-form ref="myForm" class="my-form row fit">
    <div
      :class="['item', item.col || 'col-12']"
      v-for="(item, index) in state.config"
      :key="index"
    >
      <template v-if="item.solt">
        <div>
          <slot :name="item.solt"></slot>
        </div>
      </template>
      <template v-else>
        <q-input
          v-if="item.type !== 'select'"
          :type="item.type"
          v-model="formValue[item.fild]"
          v-bind="item.bind"
          :disable="
            item.bind.disable === undefined ? state.disable : item.bind.disable
          "
          :readonly="
            item.bind.readonly === undefined
              ? state.readonly
              : item.bind.readonly
          "
        />
        <q-select
          v-if="item.type === 'select'"
          v-bind="item.bind"
          v-model="formValue[item.fild]"
          :disable="
            item.bind.disable === undefined ? state.disable : item.bind.disable
          "
          :readonly="
            item.bind.readonly === undefined
              ? state.readonly
              : item.bind.readonly
          "
        />
      </template>
    </div>
  </q-form>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, watch } from 'vue';
import { cloneDeep, isObjEqual } from 'src/common/utils';

interface Props {
  config: any;
  modelValue: any;
  disable?: boolean;
  readonly?: boolean;
}
const props = withDefaults(defineProps<Props>(), {
  config: () => {
    return {};
  },
  modelValue: () => {
    return {};
  },
  disable: false,
  readonly: false,
});

defineExpose({
  validate,
  reset,
});

const emit = defineEmits<{
  (e: 'update:modelValue', value: any): void;
}>();

const myForm = ref<any>(null);

const state = reactive({
  config: cloneDeep(props.config),
  disable: props.disable,
  readonly: props.readonly,
});
const formValue = ref<any>(cloneDeep(props.modelValue));

watch(
  () => props.config,
  (val) => {
    state.config = cloneDeep(val);
  },
  {
    deep: true,
  }
);

watch(
  () => props.modelValue,
  (val: any) => {
    if (!isObjEqual(val, formValue.value)) {
      formValue.value = val;
    }
  },
  {
    deep: true,
  }
);

watch(
  formValue.value,
  (val) => {
    emit('update:modelValue', val);
  },
  { deep: true }
);

onMounted(() => {
  //
});

async function validate() {
  return await myForm.value.validate().then((success: any) => {
    if (success) {
      return true;
    } else {
      return false;
    }
  });
}

function reset() {
  myForm.value.resetValidation();
}
</script>

<style lang="scss" scoped>
.item {
  padding: $padding-sm;
}
</style>