FormNode.vue 1.5 KB
Newer Older
Z
zhaoke 已提交
1 2 3 4 5
<template>
  <ZModal
    :showModal="showModalRef"
    @onCancel="cancel"
    @onOk="submit"
Z
zhaoke 已提交
6
    :title="t('create')"
Z
zhaoke 已提交
7
    :contentStyle="{width: '400px'}"
Z
zhaoke 已提交
8
  >
Z
zhaoke 已提交
9
    <Form>
Z
zhaoke 已提交
10
      <FormItem name="name" :label="t('name')" :info="validateInfos.name">
H
Hao Sun 已提交
11
        <input type="text" v-model="modelRef.name" />
Z
zhaoke 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
      </FormItem>
    </Form>
  </ZModal>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import {
  computed,
  defineExpose,
  onMounted,
  withDefaults,
  ref,
  defineProps,
  defineEmits,
Z
zhaoke 已提交
27
  watch,
Z
zhaoke 已提交
28 29
} from "vue";
import { useForm } from "@/utils/form";
aaronchen2k2k's avatar
aaronchen2k2k 已提交
30 31
import Form from "@/components/Form.vue";
import FormItem from "@/components/FormItem.vue";
Z
zhaoke 已提交
32 33 34 35 36 37 38 39 40

export interface FormWorkspaceProps {
  show?: boolean;
}
const { t } = useI18n();
const props = withDefaults(defineProps<FormWorkspaceProps>(), {
  show: false,
});

Z
zhaoke 已提交
41 42 43 44 45 46 47 48
watch(props, () => {
    if(!props.show){
        setTimeout(() => {
            validateInfos.value = {};
        }, 200);
    }
})

Z
zhaoke 已提交
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
const showModalRef = computed(() => {
  return props.show;
});

const cancel = () => {
  emit("cancel", {});
};

const modelRef = ref({});
const rulesRef = ref({
  name: [{ required: true, msg: t("pls_name") }],
});

const { validate, reset, validateInfos } = useForm(modelRef, rulesRef);

const emit = defineEmits<{
  (type: "submit", event: {}): void;
  (type: "cancel", event: {}): void;
}>();

const submit = () => {
  if (validate()) {
    emit("submit", modelRef.value);
  }
};

const clearFormData = () => {
  modelRef.value = {};
};

defineExpose({
  clearFormData,
});
</script>