Files
aurora-admin/frameworks/FormModal.vue2.vue
T

72 lines
2.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="aa-formmodal-mask" v-if="visible" @click.self="$emit('cancel')">
<div class="aa-formmodal">
<div class="aa-formmodal-head">
<span>{{ title }}</span>
<button class="aa-formmodal-close" @click="$emit('cancel')">×</button>
</div>
<div class="aa-formmodal-body">
<div class="aa-form-row" v-for="f in fields" :key="f.key">
<label class="aa-form-label"><span class="req" v-if="f.required">*</span>{{ f.label }}</label>
<input
class="aa-form-input"
:class="{ 'is-error': errors[f.key] }"
:value="form[f.key]"
:placeholder="f.placeholder"
@input="onInput(f.key, $event)"
/>
<div class="aa-form-err" v-if="errors[f.key]">{{ errors[f.key] }}</div>
</div>
</div>
<div class="aa-formmodal-foot">
<button class="aa-btn" @click="$emit('cancel')">取消</button>
<button class="aa-btn aa-btn--primary" @click="submit">提交</button>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'AaFormModal',
props: {
visible: { type: Boolean, default: false },
title: { type: String, default: '表单' },
fields: {
type: Array,
default: () => [
{ key: 'name', label: '姓名', required: true, placeholder: '请输入姓名' },
{ key: 'email', label: '邮箱', required: true, placeholder: 'name@example.com' },
{ key: 'role', label: '角色', placeholder: '如:管理员' }
]
},
value: { type: Object, default: () => ({}) }
},
data() {
return { form: Object.assign({}, this.value), errors: {} };
},
watch: {
visible(v) { if (v) { this.form = Object.assign({}, this.value); this.errors = {}; } }
},
methods: {
onInput(key, e) {
this.$set(this.form, key, e.target.value);
if (this.errors[key]) this.$delete(this.errors, key);
},
submit() {
var errors = {};
this.fields.forEach(function (f) {
var v = (this.form[f.key] || '').trim();
if (f.required && !v) errors[f.key] = f.label + '不能为空';
if (f.key === 'email' && v && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v)) errors[f.key] = '邮箱格式不正确';
}, this);
if (Object.keys(errors).length) { this.errors = errors; return; }
this.$emit('submit', Object.assign({}, this.form));
this.$emit('cancel');
}
}
};
</script>
<style src="./FormModal.css"></style>