Skip to content

Commit

Permalink
Add aria label field to select list
Browse files Browse the repository at this point in the history
  • Loading branch information
agustinbusso committed Nov 5, 2024
1 parent b009cc4 commit c4f241d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/components/FormSelectList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
v-if="options.renderAs === 'dropdown'"
:option-value="optionsKey"
:option-content="optionsValue"
:option-aria-label="optionsAriaLabel"
v-uni-id="name"
v-model="valueProxy"
:placeholder="placeholder ? placeholder : $t('Select...')"
Expand All @@ -26,6 +27,7 @@
:name="name"
:option-value="optionsKey"
:option-content="optionsValue"
:option-aria-label="optionsAriaLabel"
:options="selectListOptionsWithSelected"
:react-options="reactOptions"
:emit-objects="options.valueTypeReturned === 'object'"
Expand All @@ -39,6 +41,7 @@
:name="name"
:option-value="optionsKey"
:option-content="optionsValue"
:option-aria-label="optionsAriaLabel"
:options="selectListOptionsWithSelected"
:react-options="reactOptions"
:emit-objects="options.valueTypeReturned === 'object'"
Expand Down Expand Up @@ -159,7 +162,8 @@ export default {
valueTypeReturned: this.options.valueTypeReturned,
dataName: this.options.dataName,
value: this.options.value,
key: this.options.key
key: this.options.key,
ariaLabel: this.options.optionAriaLabel
};
},
valueProxy: {
Expand Down Expand Up @@ -213,6 +217,16 @@ export default {
}
return "__content__";
},
optionsAriaLabel() {
if (
this.options.dataSource &&
(this.options.dataSource === "provideData" ||
this.isCollection)
) {
return "ariaLabel";
}
return "__ariaLabel__";
},
classList() {
return {
"has-errors":
Expand Down Expand Up @@ -384,6 +398,7 @@ export default {
formatCollectionRecordResults(record) {
let content = get(record, this.collectionOptions.labelField);
let value = get(record, this.collectionOptions.valueField);
let ariaLabel = get(record, this.collectionOptions.ariaLabelField || this.collectionOptions.labelField);
// Special handler for file uploads
if (typeof content === 'object' && ('name' in content)) {
Expand All @@ -395,7 +410,8 @@ export default {
return {
value: String(value),
content: String(content)
content: String(content),
ariaLabel: String(ariaLabel)
};
},
includeFilterInPmql(pmql) {
Expand Down Expand Up @@ -557,9 +573,19 @@ export default {
? Mustache.render(this.options.value, item)
: Mustache.render(`{{${this.options.value || "content"}}}`, item);
// Modified ariaLabel handling
let itemAriaLabel = itemContent;
if (this.options.optionAriaLabel) {
itemAriaLabel = this.options.optionAriaLabel.indexOf("{{") >= 0
? Mustache.render(this.options.optionAriaLabel, item)
: Mustache.render(`{{${this.options.optionAriaLabel || "ariaLabel"}}}`, item);
}
Mustache.escape = escape; // Reset mustache to original escape function
parsedOption[this.optionsValue] = itemContent;
parsedOption[this.optionsAriaLabel] = itemAriaLabel;
if (this.options.valueTypeReturned === "object") {
parsedOption = suffix.length > 0 ? get(item, suffix) : item;
if (!parsedOption.hasOwnProperty(this.optionsValue)) {
Expand All @@ -569,6 +595,13 @@ export default {
}
});
}
if (!parsedOption.hasOwnProperty(this.optionsAriaLabel)) {
Object.defineProperty(parsedOption, this.optionsAriaLabel, {
get() {
return itemAriaLabel;
}
});
}
}
resultList.push(parsedOption);
});
Expand All @@ -580,9 +613,15 @@ export default {
}
const suffix = this.attributeParent(this.options.value);
let contentProperty = this.options.value;
let ariaLabelProperty = this.options.ariaLabel || this.options.value;
if (contentProperty.indexOf("{{") === -1) {
contentProperty = `{{ ${contentProperty} }}`;
}
if (ariaLabelProperty.indexOf("{{") === -1) {
ariaLabelProperty = `{{ ${ariaLabelProperty} }}`;
}
if (!parsedOption.hasOwnProperty(this.optionsValue)) {
Object.defineProperty(parsedOption, this.optionsValue, {
get() {
Expand All @@ -597,6 +636,22 @@ export default {
}
});
}
if (!parsedOption.hasOwnProperty(this.optionsAriaLabel)) {
Object.defineProperty(parsedOption, this.optionsAriaLabel, {
get() {
// note this = parsedOption
let data = {};
if (suffix) {
set(data, suffix, this);
} else {
data = this;
}
return Mustache.render(ariaLabelProperty, data);
}
});
}
return parsedOption;
},
stripMustache(str) {
Expand Down
6 changes: 6 additions & 0 deletions src/components/FormSelectList/CheckboxView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
v-model="selected"
v-bind="$attrs"
:disabled="isReadOnly"
:aria-label="getOptionAriaLabel(option)"
@change="$emit('input', selected)"
>
<label :class="labelClass" v-uni-for="getOptionId(option, index)">
Expand All @@ -32,6 +33,7 @@ export default {
'value',
'optionValue',
'optionContent',
'optionAriaLabel',
'options',
'error',
'helper',
Expand Down Expand Up @@ -75,6 +77,10 @@ export default {
getOptionContent(option) {
return option[this.optionContent || 'content'];
},
getOptionAriaLabel(option) {
const ariaLabel = option[this.optionAriaLabel || "ariaLabel"];
return (!ariaLabel || ariaLabel === "") ? this.getOptionContent(option) : ariaLabel;
},
getOptionId(option, index) {
return `${this.name}-${this.getOptionValue(option)}-${index}`;
}
Expand Down
6 changes: 6 additions & 0 deletions src/components/FormSelectList/OptionboxView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
v-model="selected"
v-bind="$attrs"
:disabled="isReadOnly"
:aria-label="getOptionAriaLabel(option)"
>
<label :class="labelClass" v-uni-for="getOptionId(option, index)">
{{getOptionContent(option)}}
Expand Down Expand Up @@ -38,6 +39,7 @@ export default {
'controlClass',
'emitObjects',
'emitArray',
'optionAriaLabel'
],
data() {
return {
Expand Down Expand Up @@ -77,6 +79,10 @@ export default {
getOptionContent(option) {
return option[this.optionContent || 'content'];
},
getOptionAriaLabel(option) {
const ariaLabel = option[this.optionAriaLabel || "ariaLabel"];
return (!ariaLabel || ariaLabel === "") ? this.getOptionContent(option) : ariaLabel;
},
getOptionId(option, index) {
return `${this.name}-${this.getOptionValue(option)}-${index}`;
}
Expand Down

0 comments on commit c4f241d

Please sign in to comment.