Skip to content

Commit

Permalink
fix(vue): fix the problem that the component class name will be overw…
Browse files Browse the repository at this point in the history
…ritten rather than merged (#2260)
  • Loading branch information
MisicDemone committed Sep 30, 2021
1 parent 3b8676b commit 7305373
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
49 changes: 49 additions & 0 deletions packages/vue/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const Input = defineComponent({
return () => {
const field = fieldRef.value
return h('input', {
class: 'test-input',
attrs: {
...attrs,
value: props.value,
Expand Down Expand Up @@ -147,6 +148,54 @@ test('render field', async () => {
expect(getByTestId('mm-children')).not.toBeUndefined()
})

test('render field with html attrs', async () => {
const form = createForm()

const { getByTestId, container } = render(
defineComponent({
name: 'TestComponent',
setup() {
return {
form,
Input,
Decorator,
}
},
template: `<FormProvider :form="form">
<Field
name="aa"
:decorator="[Decorator, {
'data-testid': 'decorator',
class: {
'test-class': true
},
style: {
marginRight: '10px'
}
}]"
:component="[Input, {
class: {
'test-class': true
},
style: {
marginLeft: '10px'
}
}]"
/>
</FormProvider>`,
})
)
expect(form.mounted).toBeTruthy()
expect(form.query('aa').take().mounted).toBeTruthy()
expect(getByTestId('aa').className.indexOf('test-input') !== -1).toBeTruthy()
expect(getByTestId('aa').className.indexOf('test-class') !== -1).toBeTruthy()
expect(getByTestId('aa').style.marginLeft).toEqual('10px')
expect(
getByTestId('decorator').className.indexOf('test-class') !== -1
).toBeTruthy()
expect(getByTestId('decorator').style.marginRight).toEqual('10px')
})

test('ReactiveField', () => {
render({
template: `<ReactiveField :field="null" />`,
Expand Down
8 changes: 7 additions & 1 deletion packages/vue/src/components/ReactiveField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ export default observer(
) ?? field.decorator[0]) as VueComponent
const decoratorData = toJS(field.decorator[1]) || {}
const style = decoratorData?.style
const classes = decoratorData?.class
delete decoratorData.style
delete decoratorData.class
return {
default: () =>
h(
decorator,
{
style,
class: classes,
attrs: decoratorData,
},
{
Expand Down Expand Up @@ -170,7 +173,9 @@ export default observer(
}

const style = originData?.style
delete originData?.style
const classes = originData?.class
delete originData.style
delete originData.class
const attrs = {
disabled: !isVoidField(field)
? field.pattern === 'disabled' || field.pattern === 'readPretty'
Expand All @@ -185,6 +190,7 @@ export default observer(
const componentData = {
attrs,
style,
class: classes,
on: events,
}

Expand Down

0 comments on commit 7305373

Please sign in to comment.