Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding language string for removing an item #863

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Or include Choices directly:
loadingText: 'Loading...',
noResultsText: 'No results found',
noChoicesText: 'No choices to choose from',
removeItemText: 'Remove item',
itemSelectText: 'Press to select',
addItemText: (value) => {
return `Press Enter to add <b>"${value}"</b>`;
Expand Down
7 changes: 6 additions & 1 deletion src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,12 @@ class Choices {

const addItemToFragment = (item: Item): void => {
// Create new list element
const listItem = this._getTemplate('item', item, removeItemButton);
const listItem = this._getTemplate(
'item',
item,
removeItemButton,
this.config.removeItemText,
);
// Append it to list
fragment.appendChild(listItem);
};
Expand Down
1 change: 1 addition & 0 deletions src/scripts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const DEFAULT_CONFIG: Options = {
loadingText: 'Loading...',
noResultsText: 'No results found',
noChoicesText: 'No choices to choose from',
removeItemText: 'Remove item',
itemSelectText: 'Press to select',
uniqueItemText: 'Only unique values can be added',
customAddItemText: 'Only values matching specific conditions can be added',
Expand Down
9 changes: 9 additions & 0 deletions src/scripts/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,15 @@ export interface Options {
*/
noChoicesText: string | Types.stringFunction;

/**
* The text that is used in the aria-label of the remove button.
*
* **Input types affected:** select-multiple
*
* @default 'Remove item'
*/
removeItemText: string | Types.stringFunction;

/**
* The text that is shown when a user hovers over a selectable choice.
*
Expand Down
11 changes: 4 additions & 7 deletions src/scripts/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const templates = {
placeholder: isPlaceholder,
}: Item,
removeItemButton: boolean,
removeItemText: string,
): HTMLDivElement {
const div = Object.assign(document.createElement('div'), {
className: item,
Expand Down Expand Up @@ -126,17 +127,13 @@ const templates = {
div.classList.remove(itemSelectable);
}
div.dataset.deletable = '';
/** @todo This MUST be localizable, not hardcoded! */
const REMOVE_ITEM_TEXT = 'Remove item';

const removeButton = Object.assign(document.createElement('button'), {
type: 'button',
className: button,
innerHTML: REMOVE_ITEM_TEXT,
innerHTML: removeItemText,
});
removeButton.setAttribute(
'aria-label',
`${REMOVE_ITEM_TEXT}: '${value}'`,
);
removeButton.setAttribute('aria-label', `${removeItemText}: '${value}'`);
removeButton.dataset.button = '';
div.appendChild(removeButton);
}
Expand Down