Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

属性合并问题 #14

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions cypress/component/BaseScene.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Test = defineComponent({
class: 'before',
id: 'p1',
});

const style = ref({});
const { render } = useScene({
components: [
Expand All @@ -26,7 +27,7 @@ const Test = defineComponent({
},
{
name: ComponentType.Button,
props: { type: 'primary' },
props: { type: 'primary', id: 'clickMeBtn' },
children: ['ClickMe'],
events: {
onClick: () => {
Expand All @@ -45,6 +46,21 @@ const Test = defineComponent({
props: { id: 'p2', style: style },
children: ['单属性响应式样式'],
},
{
name: 'div',
vIf: false,
children: 'vIf',
},
{
name: 'div',
vShow: false,
children: 'vShow',
},
{
name: ComponentType.Button,
disabled: true,
children: 'vShow',
},
],
});
return render;
Expand All @@ -67,15 +83,15 @@ describe('BaseScene.cy.tsx', () => {
it('测试传入组件', () => {
const app = cy.mount(Test);
// 测试传入组件
app.get('button').should('contain.text', 'ClickMe');
app.get('button').should('have.class', 'fes-btn-type-primary');
app.get('#clickMeBtn').should('contain.text', 'ClickMe');
app.get('#clickMeBtn').should('have.class', 'fes-btn-type-primary');
});

it('测试响应式属性', () => {
const app = cy.mount(Test);
// 测试响应式属性
app.get('#p1').should('have.class', 'before');
app.get('button').click();
app.get('#clickMeBtn').click();
app.get('#p1').should('have.class', 'after');
app.get('#p2').should('have.css', 'color', 'rgb(255, 0, 0)');
});
Expand Down
13 changes: 10 additions & 3 deletions cypress/component/useForm.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentType, EnumOption, FormSceneContext, doInitFields, doValidate, useForm, useSceneContext } from '@koala-form/core';
import { ComponentType, EnumOption, FormSceneContext, composeRender, doInitFields, doValidate, useForm, useSceneContext } from '@koala-form/core';
import { defineComponent, reactive, ref } from 'vue';

const Test = defineComponent({
Expand All @@ -13,10 +13,12 @@ const Test = defineComponent({
const vIf = ref(true);
const vShow = ref(true);

const name = { name: 'name', label: '姓名', props: { id: 'name' }, components: { name: ComponentType.Input } };

const form = useForm({
ctx: ctx as FormSceneContext,
fields: [
{ name: 'name', label: '姓名', props: { id: 'name' }, required: true, components: { name: ComponentType.Input } },
{ ...name, required: true },
{ name: 'sex', label: '性别', vIf, vShow, props: { id: 'sex' }, options: sexOptions, components: { name: ComponentType.Select } },
{
label: ' ',
Expand Down Expand Up @@ -68,7 +70,12 @@ const Test = defineComponent({
},
],
});
return form.render;

const form2 = useForm({
fields: [{ ...name, label: '姓名2' }],
});

return composeRender([form.render, form2.render]);
},
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koala-form/core",
"version": "2.0.3",
"version": "2.0.6",
"description": "基于Vue3的中后台表单解决方案",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/disabledPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isFunction, isUndefined } from 'lodash-es';
import { ref } from 'vue';
import { Ref, computed, ref, unref } from 'vue';
import { SceneConfig, SceneContext } from '../base';
import { mergeRefProps, travelTree } from '../helper';
import { ComponentDesc } from '../scheme';
Expand All @@ -20,7 +20,7 @@ export const disabledPlugin: PluginFunction<SceneContext, SceneConfig> = (api) =
mergeRefProps(scheme, 'props', { disabled: _disabled });
} else {
const props: any = scheme.props || {};
props.disabled = node.disabled;
props.disabled = computed(() => unref(node.disabled as Ref<boolean>));
scheme.props = props;
}
});
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/vIfPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isFunction, isUndefined } from 'lodash-es';
import { Ref, ref } from 'vue';
import { Ref, computed, ref, unref } from 'vue';
import { SceneConfig, SceneContext } from '../base';
import { travelTree } from '../helper';
import { ComponentDesc } from '../scheme';
Expand All @@ -19,7 +19,7 @@ export const vIfPlugin: PluginFunction<SceneContext, SceneConfig> = (api) => {
});
scheme.vIf = vIf;
} else {
scheme.vIf = node.vIf as Ref<boolean>;
scheme.vIf = computed(() => unref(node.vIf)) as Ref<boolean>;
}
});
api.emit('started');
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugins/vShowPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isFunction, isUndefined } from 'lodash-es';
import { Ref, ref } from 'vue';
import { Ref, computed, ref, unref } from 'vue';
import { SceneConfig, SceneContext } from '../base';
import { travelTree } from '../helper';
import { ComponentDesc } from '../scheme';
Expand All @@ -19,7 +19,7 @@ export const vShowPlugin: PluginFunction<SceneContext, SceneConfig> = (api) => {
});
scheme.vShow = vShow;
} else {
scheme.vShow = node.vShow as Ref<boolean>;
scheme.vShow = computed(() => unref(node.vShow)) as Ref<boolean>;
}
});
api.emit('started');
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/scheme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isString } from 'lodash-es';
import { DefineComponent, Ref, Slot, Slots, VNodeChild, reactive } from 'vue';
import { DefineComponent, Ref, Slot, Slots, VNodeChild, isReactive, reactive } from 'vue';
import { EnumOption, Reactive, SceneContext, When } from './base';
import { travelTree, turnArray } from './helper';

Expand All @@ -9,7 +9,7 @@ export interface ComponentDesc {
vIf?: Ref<boolean> | When | boolean;
vShow?: Ref<boolean> | When | boolean;
vModels?: Record<string, ModelRef>;
disabled?: Ref<boolean> | When;
disabled?: Ref<boolean> | When | boolean;
events?: Record<string, (value: any, ...args: any[]) => void>;
slotName?: string;
slots?: Slots;
Expand Down Expand Up @@ -112,7 +112,11 @@ export const createScheme = (node: ComponentDesc | string | Field | SceneContext
} else if ((node as SceneContext).schemes) {
return { __node: node, component: '', children: (node as SceneContext).schemes };
} else {
const scheme = { __node: node, component: node.name || '', props: (node as ComponentDesc).props || reactive({}) };
let props = (node as ComponentDesc).props;
if (!isReactive(props)) {
props = reactive({ ...(props || {}) });
}
const scheme = { __node: node, component: node.name || '', props };
return scheme;
}
};
Expand Down