Skip to content

Commit 2e1854d

Browse files
authored
vue-vanilla: Add css classes for required labels and the asterisk in ControlWrapper (#2274)
* feat: create own element for asterisk and add class in vue vanilla controlWrapper #2238 * add label directly without computedLabel, add "required" style class Fix #2238
1 parent ce541c5 commit 2e1854d

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

packages/vue-vanilla/src/controls/ControlWrapper.vue

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<template>
22
<div v-if="visible" :id="id" :class="styles.control.root">
3-
<label :for="id + '-input'" :class="styles.control.label">
4-
{{ computedLabel }}
3+
<label
4+
:for="id + '-input'"
5+
:class="[styles.control.label, required ? styles.control.required : '']"
6+
>
7+
{{ label }}
8+
<span v-if="showAsterisk" :class="styles.control.asterisk">*</span>
59
</label>
610
<div :class="styles.control.wrapper">
711
<slot></slot>
@@ -13,7 +17,7 @@
1317
</template>
1418

1519
<script lang="ts">
16-
import { isDescriptionHidden, computeLabel } from '@jsonforms/core';
20+
import { isDescriptionHidden } from '@jsonforms/core';
1721
import { defineComponent, PropType } from 'vue';
1822
import { Styles } from '../styles';
1923
import { Options } from '../util';
@@ -74,12 +78,8 @@ export default defineComponent({
7478
!!this.appliedOptions?.showUnfocusedDescription
7579
);
7680
},
77-
computedLabel(): string {
78-
return computeLabel(
79-
this.label,
80-
this.required,
81-
!!this.appliedOptions?.hideRequiredAsterisk
82-
);
81+
showAsterisk(): boolean {
82+
return this.required && !this.appliedOptions?.hideRequiredAsterisk;
8383
},
8484
},
8585
});

packages/vue-vanilla/src/styles/defaultStyles.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export const defaultStyles: Styles = {
1111
textarea: 'text-area',
1212
select: 'select',
1313
option: 'option',
14+
asterisk: 'asterisk',
15+
required: 'required',
1416
},
1517
verticalLayout: {
1618
root: 'vertical-layout',

packages/vue-vanilla/src/styles/styles.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export interface Styles {
2525
textarea?: string;
2626
select?: string;
2727
option?: string;
28+
asterisk?: string;
29+
required?: string;
2830
};
2931
dialog: {
3032
root?: string;

packages/vue-vanilla/tests/unit/controls/StringControlRenderer.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ const uischema = {
1313
},
1414
};
1515

16+
const schemaRequired = {
17+
type: 'object',
18+
properties: {
19+
a: {
20+
type: 'string',
21+
},
22+
},
23+
required: ['a'],
24+
};
25+
const uischemaRequired = {
26+
type: 'Control',
27+
scope: '#/properties/a',
28+
options: {
29+
hideRequiredAsterisk: true,
30+
},
31+
};
32+
1633
describe('StringControlRenderer.vue', () => {
1734
it('renders a string input', () => {
1835
const wrapper = mountJsonForms('a', schema, uischema);
@@ -37,4 +54,14 @@ describe('StringControlRenderer.vue', () => {
3754
const placeholder = input.attributes('placeholder');
3855
expect(placeholder).to.equal('string placeholder');
3956
});
57+
58+
it('should have a lable with required class and asterisk symbol', () => {
59+
const wrapper = mountJsonForms('a', schemaRequired, uischema);
60+
expect(wrapper.find('label.required span.asterisk').exists()).to.be.true;
61+
});
62+
63+
it('should have a lable with required class but asterisk symbol is hidden', () => {
64+
const wrapper = mountJsonForms('a', schemaRequired, uischemaRequired);
65+
expect(wrapper.find('label.required span.asterisk').exists()).to.be.false;
66+
});
4067
});

0 commit comments

Comments
 (0)