Skip to content

Commit f1c86b4

Browse files
committed
feat: allow htmlLabel as an alternative to label for checkboxes and radios
If a checkbox or radio option specifies "htmlLabel" then this will be rendered _as HTML_ instead of the regular "label" being rendered as text. Use cases for this could be - showing the labels in differnt colours `<span style='color:red'>Negative</span>` - adding emphasis to particular parts of the label `<b>Reasonably</b> confident` - adding margin to the bottom of an option to separate groups of related choices `<div style='margin-bottom: 1.5em'>Last option in this group</div>`
1 parent fe1790d commit f1c86b4

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

frontend/src/components/JsonEditor.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export default {
100100
"type:": "object",
101101
"properties": {
102102
"label": {"type": "string"},
103+
"htmlLabel": {"type": "string"},
103104
"value": {
104105
"anyOf": [
105106
{"type": "string"},
@@ -108,7 +109,10 @@ export default {
108109
]
109110
}
110111
},
111-
"required": ["label", "value"]
112+
"oneOf":[
113+
{"required": ["label", "value"]},
114+
{"required": ["htmlLabel", "value"]}
115+
]
112116
},
113117
{
114118
"type": "object",

frontend/src/components/annotation/CheckboxInput.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
:state="state"
1414
v-for="(option, idx) in options"
1515
>
16-
{{ option.text }}
16+
<span v-if="option.html" v-html="option.html"></span>
17+
<span v-else>{{ option.text }}</span>
1718
<b-icon-question-circle v-if="option.helptext != null" :id="config.name + '__opt' + idx" class="annotation-help-prompt"></b-icon-question-circle>
1819
<b-tooltip v-if="option.helptext != null" :target="config.name + '__opt' + idx" :title="option.helptext"></b-tooltip>
1920
</b-form-checkbox>

frontend/src/components/annotation/RadioInput.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
:inline="config.orientation!=='vertical'"
1212
:name="config.name"
1313
>
14-
{{ option.text }}
14+
<span v-if="option.html" v-html="option.html"></span>
15+
<span v-else>{{ option.text }}</span>
1516
<b-icon-question-circle v-if="option.helptext != null" :id="config.name + '__opt' + idx" class="annotation-help-prompt"></b-icon-question-circle>
1617
<b-tooltip v-if="option.helptext != null" :target="config.name + '__opt' + idx" :title="option.helptext"></b-tooltip>
1718
</b-form-radio>

frontend/src/utils/annotations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function generateBVOptions(options, document) {
6767
} else if ("value" in option) {
6868
optionsList.push({
6969
value: option.value,
70+
html: ("htmlLabel" in option ? option.htmlLabel : null),
7071
text: ("label" in option ? option.label : option.value),
7172
helptext: ("helptext" in option ? option.helptext : null),
7273
})

frontend/tests/component/AnnotationRenderer.cy.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,27 @@ describe("AnnotationRenderer", () => {
244244

245245
})
246246

247+
it("htmlLabel wins over label if both are provided", () => {
248+
const annotationComps = [
249+
{
250+
name: "sentiment",
251+
type: "checkbox",
252+
options: [
253+
{value: "positive", label: "+ve", htmlLabel: "<span style='color:green'>Positive</span>"},
254+
{value: "neutral", label: "meh..."},
255+
{value: "negative", label: "-ve", htmlLabel: "<span style='color:red'>Negative</span>"},
256+
],
257+
}]
258+
259+
cy.mount(AnnotationRenderer, {propsData: {config: annotationComps}})
260+
261+
// the HTML label for (i.e. next sibling of) the positive option should be visible.
262+
cy.get("[name='sentiment'][value='positive'] + label").contains("Positive").should("exist")
263+
// but the text label should not
264+
cy.get("[name='sentiment'][value='positive'] + label").contains("+ve").should("not.exist")
265+
266+
})
267+
247268
})
248269

249270
it('Test dynamic options fromDocument', () => {

0 commit comments

Comments
 (0)