Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
feat(accessibility): allow modification of attributes on pin and clea…
Browse files Browse the repository at this point in the history
…r buttons (#920)
  • Loading branch information
JonathanMontane committed Oct 28, 2019
1 parent bcbaec4 commit 41cbd09
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/source/documentation.html.md.erb
Expand Up @@ -205,6 +205,31 @@ Control whether the default styles should be used.

**Default**: Styles are enabled.

**Important**: This parameter can only be set at instantiation.
</td>
</tr>
<tr>
<td markdown="1">
<div class="api-entry" id="api-options-appId"><code>accessibility</code></div>

Type: **Object**
</td>
<td markdown="1">
an object containing a `pinButton` and/or `clearButton` object which is composed of html attributes to apply to the corresponding button

**Example**:
```
accessibility: {
pinButton: {
'aria-label': 'use browser geolocation',
'tab-index': 12,
},
clearButton: {
'tab-index': 13,
}
}
```

**Important**: This parameter can only be set at instantiation.
</td>
</tr>
Expand Down
23 changes: 23 additions & 0 deletions src/places.js
Expand Up @@ -17,10 +17,19 @@ insertCss(css, { prepend: true });
import errors from './errors';
import createReverseGeocodingSource from './createReverseGeocodingSource';

const applyAttributes = (elt, attrs) => {
Object.entries(attrs).forEach(([name, value]) => {
elt.setAttribute(name, `${value}`);
});

return elt;
};

export default function places(options) {
const {
container,
style,
accessibility,
autocompleteOptions: userAutocompleteOptions = {},
} = options;

Expand Down Expand Up @@ -119,6 +128,13 @@ export default function places(options) {
const clear = document.createElement('button');
clear.setAttribute('type', 'button');
clear.setAttribute('aria-label', 'clear');
if (
accessibility &&
accessibility.clearButton &&
accessibility.clearButton instanceof Object
) {
applyAttributes(clear, accessibility.clearButton);
}
clear.classList.add(`${prefix}-input-icon`);
clear.classList.add(`${prefix}-icon-clear`);
clear.innerHTML = clearIcon;
Expand All @@ -128,6 +144,13 @@ export default function places(options) {
const pin = document.createElement('button');
pin.setAttribute('type', 'button');
pin.setAttribute('aria-label', 'focus');
if (
accessibility &&
accessibility.pinButton &&
accessibility.pinButton instanceof Object
) {
applyAttributes(pin, accessibility.pinButton);
}
pin.classList.add(`${prefix}-input-icon`);
pin.classList.add(`${prefix}-icon-pin`);
pin.innerHTML = pinIcon;
Expand Down
13 changes: 13 additions & 0 deletions test/npm-lib/index.html
Expand Up @@ -11,6 +11,19 @@
appId: 'plFMJJT5O9PC',
apiKey: 'a0f8e2d480b9d119f485836d77db4e53',
container: document.querySelector('#search-box'),
autocompleteOptions: {
ariaLabel: 'address search'
},
accessibility: {
pinButton: {
'aria-label': 'use browser geolocation',
'tab-index': 2
},
clearButton: {
'arial-label': 'clear search',
'tab-index': 2
}
},
debug: true,
});
</script>
Expand Down

0 comments on commit 41cbd09

Please sign in to comment.