refactor(form): 优化代码

This commit is contained in:
roymondchen 2024-01-09 14:50:25 +08:00
parent 5c54f50365
commit a10ae0ddd1
8 changed files with 43 additions and 44 deletions

View File

@ -312,6 +312,7 @@ const type = computed((): string => {
}); });
} }
if (type === 'form') return ''; if (type === 'form') return '';
if (type === 'container') return '';
return type?.replace(/([A-Z])/g, '-$1').toLowerCase() || (items.value ? '' : 'text'); return type?.replace(/([A-Z])/g, '-$1').toLowerCase() || (items.value ? '' : 'text');
}); });

View File

@ -11,15 +11,14 @@
@input="inputHandler" @input="inputHandler"
@keyup="keyUpHandler($event)" @keyup="keyUpHandler($event)"
> >
<template #append v-if="config.append"> <template #append v-if="appendConfig">
<span v-if="typeof config.append === 'string'">{{ config.append }}</span>
<TMagicButton <TMagicButton
v-if="typeof config.append === 'object' && config.append.type === 'button'" v-if="appendConfig.type === 'button'"
style="color: #409eff" style="color: #409eff"
:size="size" :size="size"
@click.prevent="buttonClickHandler" @click.prevent="buttonClickHandler"
> >
{{ config.append.text }} {{ appendConfig.text }}
</TMagicButton> </TMagicButton>
</template> </template>
</TMagicInput> </TMagicInput>
@ -36,7 +35,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { inject, ref } from 'vue'; import { computed, inject, ref } from 'vue';
import { TMagicButton, TMagicInput, TMagicPopover } from '@tmagic/design'; import { TMagicButton, TMagicInput, TMagicPopover } from '@tmagic/design';
import { isNumber } from '@tmagic/utils'; import { isNumber } from '@tmagic/utils';
@ -59,6 +58,26 @@ useAddField(props.prop);
const mForm = inject<FormState | undefined>('mForm'); const mForm = inject<FormState | undefined>('mForm');
const appendConfig = computed(() => {
if (typeof props.config.append === 'string') {
return {
text: props.config.append,
type: 'button',
handler: undefined,
};
}
if (props.config.append && typeof props.config.append === 'object') {
if (props.config.append.value === 0) {
return false;
}
return props.config.append;
}
return false;
});
const popoverVisible = ref(false); const popoverVisible = ref(false);
const confirmTrimHandler = () => { const confirmTrimHandler = () => {
@ -83,10 +102,9 @@ const inputHandler = (v: string) => {
}; };
const buttonClickHandler = () => { const buttonClickHandler = () => {
if (typeof props.config.append === 'string') return; if (!appendConfig.value) return;
if (typeof appendConfig.value.handler === 'function') {
if (props.config.append?.handler) { appendConfig.value.handler(mForm, {
props.config.append.handler(mForm, {
model: props.model, model: props.model,
values: mForm?.values, values: mForm?.values,
}); });
@ -150,11 +168,3 @@ const keyUpHandler = ($event: KeyboardEvent) => {
emit('change', props.model[props.name]); emit('change', props.model[props.name]);
}; };
</script> </script>
<style lang="scss" scoped>
.m-form-validate__warning {
color: var(--el-color-warning);
font-size: 12px;
width: 100%;
line-height: 1.4;
}
</style>

View File

@ -49,6 +49,7 @@ import Timerange from './fields/Timerange.vue';
import { setConfig } from './utils/config'; import { setConfig } from './utils/config';
import Form from './Form.vue'; import Form from './Form.vue';
import FormDialog from './FormDialog.vue'; import FormDialog from './FormDialog.vue';
import type { FormConfig } from './schema';
import './theme/index.scss'; import './theme/index.scss';
@ -88,6 +89,10 @@ export { default as MSelect } from './fields/Select.vue';
export { default as MCascader } from './fields/Cascader.vue'; export { default as MCascader } from './fields/Cascader.vue';
export { default as MDynamicField } from './fields/DynamicField.vue'; export { default as MDynamicField } from './fields/DynamicField.vue';
export const createForm = function (config: FormConfig) {
return config;
};
export interface InstallOptions { export interface InstallOptions {
[key: string]: any; [key: string]: any;
} }

View File

@ -320,8 +320,9 @@ export interface TextConfig extends FormItem, Input {
| string | string
| { | {
text: string; text: string;
value?: 0 | 1;
type: 'button'; type: 'button';
handler: ( handler?: (
mForm: FormState | undefined, mForm: FormState | undefined,
data: { data: {
model: any; model: any;

View File

@ -2,6 +2,7 @@
@use "./form-drawer.scss"; @use "./form-drawer.scss";
@use "./form.scss"; @use "./form.scss";
@use "./date-time.scss"; @use "./date-time.scss";
@use "./text.scss";
@use "./link.scss"; @use "./link.scss";
@use "./fieldset.scss"; @use "./fieldset.scss";
@use "./group-list.scss"; @use "./group-list.scss";

View File

@ -0,0 +1,6 @@
.m-form-validate__warning {
color: var(--el-color-warning);
font-size: 12px;
width: 100%;
line-height: 1.4;
}

View File

@ -1,25 +0,0 @@
/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { FormConfig } from '../schema';
export const createForm = function (config: FormConfig) {
return config;
};
export default createForm;

View File

@ -20,7 +20,7 @@ import { describe, expect, test } from 'vitest';
import { FormConfig } from '@tmagic/form'; import { FormConfig } from '@tmagic/form';
import createForm from '../../../src/utils/createForm'; import { createForm } from '../../../src';
describe('createForm', () => { describe('createForm', () => {
test('正常创建', () => { test('正常创建', () => {