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: create own element for asterisk and add class in vue vanilla controlWrapper #2274

Merged
merged 3 commits into from
Feb 12, 2024
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
18 changes: 9 additions & 9 deletions packages/vue-vanilla/src/controls/ControlWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<template>
<div v-if="visible" :id="id" :class="styles.control.root">
<label :for="id + '-input'" :class="styles.control.label">
{{ computedLabel }}
<label
:for="id + '-input'"
:class="[styles.control.label, required ? styles.control.required : '']"
>
{{ label }}
<span v-if="showAsterisk" :class="styles.control.asterisk">*</span>
</label>
<div :class="styles.control.wrapper">
<slot></slot>
Expand All @@ -13,7 +17,7 @@
</template>

<script lang="ts">
import { isDescriptionHidden, computeLabel } from '@jsonforms/core';
import { isDescriptionHidden } from '@jsonforms/core';
import { defineComponent, PropType } from 'vue';
import { Styles } from '../styles';
import { Options } from '../util';
Expand Down Expand Up @@ -74,12 +78,8 @@ export default defineComponent({
!!this.appliedOptions?.showUnfocusedDescription
);
},
computedLabel(): string {
return computeLabel(
this.label,
this.required,
!!this.appliedOptions?.hideRequiredAsterisk
);
showAsterisk(): boolean {
return this.required && !this.appliedOptions?.hideRequiredAsterisk;
},
},
});
Expand Down
2 changes: 2 additions & 0 deletions packages/vue-vanilla/src/styles/defaultStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const defaultStyles: Styles = {
textarea: 'text-area',
select: 'select',
option: 'option',
asterisk: 'asterisk',
required: 'required',
},
verticalLayout: {
root: 'vertical-layout',
Expand Down
2 changes: 2 additions & 0 deletions packages/vue-vanilla/src/styles/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface Styles {
textarea?: string;
select?: string;
option?: string;
asterisk?: string;
required?: string;
};
dialog: {
root?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ const uischema = {
},
};

const schemaRequired = {
type: 'object',
properties: {
a: {
type: 'string',
},
},
required: ['a'],
};
const uischemaRequired = {
type: 'Control',
scope: '#/properties/a',
options: {
hideRequiredAsterisk: true,
},
};

describe('StringControlRenderer.vue', () => {
it('renders a string input', () => {
const wrapper = mountJsonForms('a', schema, uischema);
Expand All @@ -37,4 +54,14 @@ describe('StringControlRenderer.vue', () => {
const placeholder = input.attributes('placeholder');
expect(placeholder).to.equal('string placeholder');
});

it('should have a lable with required class and asterisk symbol', () => {
const wrapper = mountJsonForms('a', schemaRequired, uischema);
expect(wrapper.find('label.required span.asterisk').exists()).to.be.true;
});

it('should have a lable with required class but asterisk symbol is hidden', () => {
const wrapper = mountJsonForms('a', schemaRequired, uischemaRequired);
expect(wrapper.find('label.required span.asterisk').exists()).to.be.false;
});
});
Loading