Skip to content

Commit 8f16514

Browse files
committed
Allow remove item icon/text to be localized
1 parent 3e658fe commit 8f16514

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/scripts/defaults.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export const DEFAULT_CONFIG: Options = {
7070
uniqueItemText: 'Only unique values can be added',
7171
customAddItemText: 'Only values matching specific conditions can be added',
7272
addItemText: (value) => `Press Enter to add <b>"${sanitise(value)}"</b>`,
73+
removeItemIconText: (value) => `Remove item`,
74+
removeItemLabelText: (value) => `Remove item: ${sanitise(value)}`,
7375
maxItemText: (maxItemCount) => `Only ${maxItemCount} values can be added`,
7476
valueComparer: (value1, value2) => value1 === value2,
7577
fuseOptions: {

src/scripts/interfaces/options.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ export interface Options {
139139
*/
140140
addItemText: string | Types.NoticeStringFunction;
141141

142+
/**
143+
* The text/icon for the remove button. To access the item's value, pass a function with a `value` argument (see the **default config** [https://github.com/jshjohnson/Choices#setup] for an example), otherwise pass a string.
144+
*
145+
* **Input types affected:** text
146+
*
147+
* @default
148+
* ```
149+
* (value) => `Remove item`;
150+
* ```
151+
*/
152+
removeItemIconText: string | Types.NoticeStringFunction;
153+
154+
/**
155+
* The text for the remove button's aria label. To access the item's value, pass a function with a `value` argument (see the **default config** [https://github.com/jshjohnson/Choices#setup] for an example), otherwise pass a string.
156+
*
157+
* **Input types affected:** text
158+
*
159+
* @default
160+
* ```
161+
* (value) => `Remove item: ${value}`;
162+
* ```
163+
*/
164+
removeItemLabelText: string | Types.NoticeStringFunction;
165+
142166
/**
143167
* Whether a user can remove items.
144168
*

src/scripts/templates.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,23 @@ const templates = {
132132
div.classList.remove(itemSelectable);
133133
}
134134
div.dataset.deletable = '';
135-
/** @todo This MUST be localizable, not hardcoded! */
136-
const REMOVE_ITEM_TEXT = 'Remove item';
135+
136+
let REMOVE_ITEM_ICON =
137+
typeof this.config.removeItemIconText === 'function'
138+
? this.config.removeItemIconText(value)
139+
: this.config.removeItemIconText;
140+
let REMOVE_ITEM_LABEL =
141+
typeof this.config.removeItemLabelText === 'function'
142+
? this.config.removeItemLabelText(value)
143+
: this.config.removeItemLabelText;
137144
const removeButton = Object.assign(document.createElement('button'), {
138145
type: 'button',
139146
className: button,
140-
[allowHTML ? 'innerHTML' : 'innerText']: REMOVE_ITEM_TEXT,
147+
[allowHTML ? 'innerHTML' : 'innerText']: REMOVE_ITEM_ICON,
141148
});
142149
removeButton.setAttribute(
143150
'aria-label',
144-
`${REMOVE_ITEM_TEXT}: '${value}'`,
151+
`${REMOVE_ITEM_LABEL}: '${value}'`,
145152
);
146153
removeButton.dataset.button = '';
147154
div.appendChild(removeButton);

0 commit comments

Comments
 (0)