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

feat: inject custom-control-wrapper in controlWrapper vue vanilla to replace the default controlWrapper #2275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions packages/vue-vanilla/src/controls/ControlWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<template>
<div v-if="visible" :id="id" :class="styles.control.root">
<component
:is="customControlWrapper.component"
v-bind="customControlWrapper.props"
v-if="visible && customControlWrapper && customControlWrapper.component"
>
<slot></slot>
</component>

<div v-else-if="visible" :id="id" :class="styles.control.root">
<label :for="id + '-input'" :class="styles.control.label">
{{ computedLabel }}
</label>
Expand All @@ -14,9 +22,9 @@

<script lang="ts">
import { isDescriptionHidden, computeLabel } from '@jsonforms/core';
import { defineComponent, PropType } from 'vue';
import { defineComponent, inject, PropType } from 'vue';
import { Styles } from '../styles';
import { Options } from '../util';
import { type CustomControllWrapper, Options } from '../util';

export default defineComponent({
name: 'ControlWrapper',
Expand Down Expand Up @@ -65,6 +73,17 @@ export default defineComponent({
type: Object as PropType<Styles>,
},
},
setup(props: any) {
const customControlWrapper = inject<CustomControllWrapper | undefined>(
'custom-control-wrapper',
undefined
);
if (customControlWrapper?.component) {
customControlWrapper.props = { ...props, ...customControlWrapper?.props };
}

return { customControlWrapper };
},
computed: {
showDescription(): boolean {
return !isDescriptionHidden(
Expand Down
6 changes: 6 additions & 0 deletions packages/vue-vanilla/src/util/customControlWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Component } from 'vue';

export interface CustomControllWrapper {
component: Component;
props?: Record<string, any>;
}
1 change: 1 addition & 0 deletions packages/vue-vanilla/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './options';
export * from './composition';
export * from './customControlWrapper';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai';
import { mountJsonFormsWithCustomControlWrapper } from '../util';

const schema = {
type: 'string',
};
const uischema = {
type: 'Control',
scope: '#',
};

describe('CustomControlWrapper.vue', () => {
it('renders a custom control wrapper', () => {
const wrapper = mountJsonFormsWithCustomControlWrapper(
'',
schema,
uischema
);
expect(wrapper.find('.customWrapper').exists()).to.be.true;
});
});
65 changes: 65 additions & 0 deletions packages/vue-vanilla/tests/unit/util/CustomControlWrapper.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<template>
<div v-if="visible" :id="id" class="customWrapper">
{{ label }}: <slot></slot>
</div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
name: 'CustomControlWrapper',
props: {
id: {
required: true,
type: String,
},
description: {
required: false as const,
type: String,
default: undefined,
},
errors: {
required: false as const,
type: String,
default: undefined,
},
label: {
required: false as const,
type: String,
default: undefined,
},
appliedOptions: {
required: false as const,
type: Object,
default: undefined,
},
visible: {
required: false as const,
type: Boolean,
default: true,
},
required: {
required: false as const,
type: Boolean,
default: false,
},
isFocused: {
required: false as const,
type: Boolean,
default: false,
},
styles: {
required: true,
type: Object,
},
},
});
</script>


<style scoped>
.customWrapper {
display: flex;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script lang="ts">
import { defineComponent } from 'vue';
// eslint-disable-next-line vue/no-unused-components
import CustomControlWrapper from './CustomControlWrapper.vue';
import TestComponent from './TestComponent.vue';

export default defineComponent({
name: 'TestComponentWithCustomWrapper',
components: {
TestComponent,
},
provide() {
return {
'custom-control-wrapper': { component: CustomControlWrapper },
};
},
});
</script>

<template>
<TestComponent />
</template>
12 changes: 12 additions & 0 deletions packages/vue-vanilla/tests/unit/util/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mount } from '@vue/test-utils';
import TestComponent from './TestComponent.vue';
import TestComponentWithCustomControlWrapper from './TestComponentWithCustomControlWrapper.vue';

export const mountJsonForms = (
data: any,
Expand All @@ -11,3 +12,14 @@ export const mountJsonForms = (
props: { initialData: data, schema, uischema, config },
});
};

export const mountJsonFormsWithCustomControlWrapper = (
data: any,
schema: any,
uischema?: any,
config?: any
) => {
return mount(TestComponentWithCustomControlWrapper, {
props: { initialData: data, schema, uischema, config },
});
};