mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 19:08:20 +00:00
Rework NiceForm
This commit is contained in:
parent
a4559a98e8
commit
46b94ec677
1 changed files with 37 additions and 44 deletions
|
@ -1,68 +1,61 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { loadConfigFromFile } from 'vite';
|
|
||||||
|
|
||||||
|
|
||||||
const props = defineModel<{
|
const props = defineModel<{
|
||||||
title: string
|
title: string
|
||||||
fields: {
|
sections: {
|
||||||
key: string,
|
title: string
|
||||||
label: string,
|
fields: {
|
||||||
component: () => Component,
|
key: string,
|
||||||
props: any,
|
label: string,
|
||||||
default: any,
|
as: string,
|
||||||
if?: () => boolean,
|
props: any,
|
||||||
}[]
|
default: any,
|
||||||
|
enabled?: (values: Record<string, any>) => Boolean,
|
||||||
|
rules?: (value: any) => true | string,
|
||||||
|
}[],
|
||||||
|
}[],
|
||||||
modelValue: any,
|
modelValue: any,
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
let { title, fields, modelValue } = $(props);
|
let { sections } = $(props);
|
||||||
|
|
||||||
let enabled = $ref({name: true} as any);
|
|
||||||
|
|
||||||
const emit = defineEmits<{
|
|
||||||
(event: 'update:modelValue'): void,
|
|
||||||
(event: 'update:enabled'): void
|
|
||||||
}>();
|
|
||||||
|
|
||||||
onMounted(async() => {
|
|
||||||
for (const field of fields) {
|
|
||||||
if (modelValue[field.key] == undefined) {
|
|
||||||
if (!field.default) {
|
|
||||||
modelValue[field.key] = {};
|
|
||||||
} else {
|
|
||||||
modelValue[field.key] = field.default;
|
|
||||||
}
|
|
||||||
enabled[field.key] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("modelValue", modelValue, enabled);
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="form">
|
<ValidationForm as="div" v-slot="{ values }" @submit="false">
|
||||||
<h2>{{ title }}</h2>
|
<template v-for="(section, index) in sections" :key="index">
|
||||||
<template v-for="(field, index) in fields" :key="index">
|
<h4 v-if="section.title">{{ section.title }}</h4>
|
||||||
<template v-if="enabled[field.key]">
|
<div class="section">
|
||||||
<label :for="field.key" v-text="field.label"/>
|
<template v-for="(field, index) in section.fields" :key="index">
|
||||||
<component :name="field.key" :is="field.component()" v-bind="field.props" v-model="modelValue[field.key]"/>
|
<template v-if="field.enabled ? field.enabled(values) : true">
|
||||||
</template>
|
<label :for="field.key" v-text="field.label" />
|
||||||
|
<Field :name="field.key" :as="field.as" :rules="field.rules" v-bind="field.props" />
|
||||||
|
<ErrorMessage :name="field.key" />
|
||||||
|
</template>
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
<p>{{ values }}</p>
|
||||||
|
</ValidationForm>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.form {
|
.section {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: auto 1fr;
|
grid-template-columns: auto 1fr;
|
||||||
padding: 0.5rem;
|
padding: 0.5rem;
|
||||||
gap: 0.5rem;
|
gap: 0.5rem;
|
||||||
}
|
}
|
||||||
.form > :is(button, .button, h2) {
|
|
||||||
|
h4,
|
||||||
|
p {
|
||||||
grid-column: 1 / 3;
|
grid-column: 1 / 3;
|
||||||
}
|
}
|
||||||
.form > :is(label) {
|
|
||||||
grid-column: 1;
|
h4 {
|
||||||
|
background-color: var(--cl-bg-hl);
|
||||||
|
padding: 0.3rem;
|
||||||
|
padding-left: 0.5rem;
|
||||||
|
;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
Loading…
Add table
Reference in a new issue