Skip to content

Commit

Permalink
add label directly without computedLabel, add "required" style class,…
Browse files Browse the repository at this point in the history
… move tests
  • Loading branch information
davewwww committed Feb 12, 2024
1 parent a9c5155 commit b613637
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
21 changes: 9 additions & 12 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 }}<span v-if="showAsterisk" :class="styles.control.asterisk">*</span>
<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,16 +78,9 @@ export default defineComponent({
!!this.appliedOptions?.showUnfocusedDescription
);
},
computedLabel(): string {
return computeLabel(
this.label,
this.required,
true
);
},
showAsterisk(): boolean {
showAsterisk(): boolean {
return this.required && !this.appliedOptions?.hideRequiredAsterisk;
}
},
},
});
</script>
1 change: 1 addition & 0 deletions packages/vue-vanilla/src/styles/defaultStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const defaultStyles: Styles = {
select: 'select',
option: 'option',
asterisk: 'asterisk',
required: 'required',
},
verticalLayout: {
root: 'vertical-layout',
Expand Down
1 change: 1 addition & 0 deletions packages/vue-vanilla/src/styles/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Styles {
select?: string;
option?: string;
asterisk?: string;
required?: string;
};
dialog: {
root?: string;
Expand Down
28 changes: 0 additions & 28 deletions packages/vue-vanilla/tests/unit/complex/ObjectRenderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@ const uischema = {
type: 'Control',
scope: '#',
};

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

describe('ObjectRenderer.vue', () => {
it('renders a fieldset', () => {
const wrapper = mountJsonForms(
Expand Down Expand Up @@ -66,14 +48,4 @@ describe('ObjectRenderer.vue', () => {
const inputs = wrapper.findAll('input');
expect(inputs.length).to.equal(2);
});

it('renders a string input with an asterisk', () => {
const wrapper = mountJsonForms('a', schemaRequired, uischema);
expect(wrapper.find('label span.asterisk').exists()).to.be.true;
});

it('renders a string input but hide asterisk', () => {
const wrapper = mountJsonForms('a', schemaRequired, uischemaRequired);
expect(wrapper.find('label span.asterisk').exists()).to.be.false;
});
});
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;
});
});

0 comments on commit b613637

Please sign in to comment.