Skip to content

Commit

Permalink
feat: allow htmlLabel as an alternative to label for checkboxes and r…
Browse files Browse the repository at this point in the history
…adios

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>`
  • Loading branch information
ianroberts committed Nov 3, 2024
1 parent fe1790d commit c20c92d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion frontend/src/components/annotation/CheckboxInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
:state="state"
v-for="(option, idx) in options"
>
{{ option.text }}
<span v-if="option.html" v-html="option.html"></span>
<span v-else>{{ option.text }}</span>
<b-icon-question-circle v-if="option.helptext != null" :id="config.name + '__opt' + idx" class="annotation-help-prompt"></b-icon-question-circle>
<b-tooltip v-if="option.helptext != null" :target="config.name + '__opt' + idx" :title="option.helptext"></b-tooltip>
</b-form-checkbox>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/annotation/RadioInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
:inline="config.orientation!=='vertical'"
:name="config.name"
>
{{ option.text }}
<span v-if="option.html" v-html="option.html"></span>
<span v-else>{{ option.text }}</span>
<b-icon-question-circle v-if="option.helptext != null" :id="config.name + '__opt' + idx" class="annotation-help-prompt"></b-icon-question-circle>
<b-tooltip v-if="option.helptext != null" :target="config.name + '__opt' + idx" :title="option.helptext"></b-tooltip>
</b-form-radio>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/utils/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function generateBVOptions(options, document) {
} else if ("value" in option) {
optionsList.push({
value: option.value,
html: ("htmlLabel" in option ? option.htmlLabel : null),
text: ("label" in option ? option.label : option.value),
helptext: ("helptext" in option ? option.helptext : null),
})
Expand Down
21 changes: 21 additions & 0 deletions frontend/tests/component/AnnotationRenderer.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ describe("AnnotationRenderer", () => {

})

it("htmlLabel wins over label if both are provided", () => {
const annotationComps = [
{
name: "sentiment",
type: "checkbox",
options: [
{value: "positive", label: "+ve", htmlLabel: "<span style='color:green'>Positive</span>"},
{value: "neutral", label: "meh..."},
{value: "negative", label: "-ve", htmlLabel: "<span style='color:red'>Negative</span>"},
],
}]

cy.mount(AnnotationRenderer, {propsData: {config: annotationComps}})

// the HTML label for (i.e. next sibling of) the positive option should be visible.
cy.get("[name='sentiment'][value='positive'] + label").contains("Positive").should("exist")
// but the text label should not
cy.get("[name='sentiment'][value='positive'] + label").contains("+ve").should("not.exist")

})

})

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

0 comments on commit c20c92d

Please sign in to comment.