Skip to content

Commit

Permalink
vue-vanilla: Add css classes for required labels and the asterisk in …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
davewwww committed Feb 12, 2024
1 parent ce541c5 commit 2e1854d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
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;
});
});

0 comments on commit 2e1854d

Please sign in to comment.