Skip to content

Commit 747c013

Browse files
authored
Merge pull request #12 from ghiscoding/feat/text-formatter-string-patterns
feat: add string pattern options as alternative to override text formats
2 parents da064e6 + c72148e commit 747c013

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

demo/src/examples/example09.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export default class Example {
1818
}) as EventListener);
1919

2020
this.ms1 = multipleSelect(elm) as MultipleSelectInstance;
21-
this.ms2 = multipleSelect('#select', { showOkButton: true }) as MultipleSelectInstance;
21+
this.ms2 = multipleSelect('#select', {
22+
filter: true,
23+
showOkButton: true,
24+
}) as MultipleSelectInstance;
2225
}
2326

2427
unmount() {

demo/src/i18n/i18n.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ export default class Example {
66
mount() {
77
this.ms1 = multipleSelect('select', {
88
filter: true,
9-
formatSelectAll() {
10-
return '[Tout sélectionner]';
11-
},
9+
showOkButton: true,
10+
11+
// 1. you could use the text format callback functions
1212
formatAllSelected() {
1313
return 'Tous sélectionnés';
1414
},
@@ -18,6 +18,19 @@ export default class Example {
1818
formatNoMatchesFound() {
1919
return 'Aucun résultat';
2020
},
21+
formatOkButton() {
22+
return 'Fermer';
23+
},
24+
formatSelectAll() {
25+
return '[Tout sélectionner]';
26+
},
27+
28+
// 2. OR you could also use string pattern instead of the callback functions
29+
// allSelectedText: 'Tous sélectionnés',
30+
// countSelectedText: '# de % selectionnés',
31+
// noMatchesFoundText: 'Aucun résultat',
32+
// okButtonText: 'Fermer',
33+
// selectAllText: 'Tout sélectionner',
2134
}) as MultipleSelectInstance;
2235
}
2336

lib/src/MultipleSelectInstance.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export class MultipleSelectInstance {
376376
const saInputElm = createDomElement('input', { type: 'checkbox', checked: this.allSelected });
377377
saInputElm.dataset.name = 'selectAll';
378378
saLabelElm.appendChild(saInputElm);
379-
saLabelElm.appendChild(createDomElement('span', { textContent: this.options.formatSelectAll() }));
379+
saLabelElm.appendChild(createDomElement('span', { textContent: this.formatSelectAll() }));
380380
this.selectAllParentElm.appendChild(saLabelElm);
381381
this.dropElm.appendChild(this.selectAllParentElm);
382382
}
@@ -388,7 +388,7 @@ export class MultipleSelectInstance {
388388
this.okButtonElm = createDomElement('button', {
389389
className: 'ms-ok-button',
390390
type: 'button',
391-
textContent: this.options.formatOkButton(),
391+
textContent: this.formatOkButton(),
392392
});
393393
this.dropElm.appendChild(this.okButtonElm);
394394
}
@@ -464,7 +464,7 @@ export class MultipleSelectInstance {
464464
rows.push(...this.initListItem(row));
465465
});
466466

467-
rows.push(`<li class="ms-no-results">${this.options.formatNoMatchesFound()}</li>`);
467+
rows.push(`<li class="ms-no-results">${this.formatNoMatchesFound()}</li>`);
468468

469469
return rows;
470470
}
@@ -904,12 +904,12 @@ export class MultipleSelectInstance {
904904
spanElm.innerHTML = this.options.sanitizer ? this.options.sanitizer(placeholder) : placeholder;
905905
} else if (sl < this.options.minimumCountSelected) {
906906
html = getSelectOptionHtml();
907-
} else if (this.options.formatAllSelected() && sl === this.dataTotal) {
908-
html = this.options.formatAllSelected();
907+
} else if (this.formatAllSelected() && sl === this.dataTotal) {
908+
html = this.formatAllSelected();
909909
} else if (this.options.ellipsis && sl > this.options.minimumCountSelected) {
910910
html = `${textSelects.slice(0, this.options.minimumCountSelected).join(this.options.displayDelimiter)}...`;
911-
} else if (this.options.formatCountSelected(sl, this.dataTotal) && sl > this.options.minimumCountSelected) {
912-
html = this.options.formatCountSelected(sl, this.dataTotal);
911+
} else if (this.formatCountSelected(sl, this.dataTotal) && sl > this.options.minimumCountSelected) {
912+
html = this.formatCountSelected(sl, this.dataTotal);
913913
} else {
914914
html = getSelectOptionHtml();
915915
}
@@ -1397,4 +1397,29 @@ export class MultipleSelectInstance {
13971397

13981398
return widthNoScroll - widthWithScroll;
13991399
}
1400+
1401+
// five text formatters, it could be string patterns or formatter callback functions
1402+
1403+
formatAllSelected() {
1404+
return this.options.allSelectedText || this.options.formatAllSelected();
1405+
}
1406+
1407+
formatCountSelected(selectedCount: number, totalCount: number) {
1408+
if (this.options.countSelectedText) {
1409+
return this.options.countSelectedText.replace('#', `${selectedCount}`).replace('%', `${totalCount}`);
1410+
}
1411+
return this.options.formatCountSelected(selectedCount, totalCount);
1412+
}
1413+
1414+
formatNoMatchesFound() {
1415+
return this.options.noMatchesFoundText || this.options.formatNoMatchesFound();
1416+
}
1417+
1418+
formatOkButton() {
1419+
return this.options.okButtonText || this.options.formatOkButton();
1420+
}
1421+
1422+
formatSelectAll() {
1423+
return this.options.selectAllText || this.options.formatSelectAll();
1424+
}
14001425
}

lib/src/interfaces/multipleSelectOption.interface.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export interface MultipleSelectOption extends MultipleSelectLocale {
77
/** defaults to 10, when using "autoAdjustDropHeight" we might want to add a bottom (or top) padding instead of taking the entire available space */
88
adjustedHeightPadding: number;
99

10+
/** Use optional string to override "All selected" text instead of `formatAllSelected()`, the latter should be prefered */
11+
allSelectedText?: string;
12+
1013
/** Auto-adjust the Drop menu height to fit with available space */
1114
autoAdjustDropHeight?: boolean;
1215

@@ -19,6 +22,9 @@ export interface MultipleSelectOption extends MultipleSelectLocale {
1922
/** HTML container to use for the drop menu, e.g. 'body' */
2023
container?: string | HTMLElement | null;
2124

25+
/** Use optional string to override selected count text "# of % selected" instead of `formatCountSelected()`, the latter should be prefered */
26+
countSelectedText?: string;
27+
2228
/** provide custom data */
2329
data?: any | any[];
2430

@@ -94,6 +100,12 @@ export interface MultipleSelectOption extends MultipleSelectLocale {
94100
/** Provide a name to the multiple select element. By default this option is set to ''. */
95101
name?: string;
96102

103+
/** Use optional string to override text when filtering "No matches found" instead of `formatNoMatchesFound()`, the latter should be prefered */
104+
noMatchesFoundText?: string;
105+
106+
/** Use optional string to override "OK" button text instead of `formatOkButton()`, the latter should be prefered */
107+
okButtonText?: string;
108+
97109
/** Should we open the dropdown while hovering the select? */
98110
openOnHover?: boolean;
99111

@@ -106,6 +118,9 @@ export interface MultipleSelectOption extends MultipleSelectLocale {
106118
/** Whether or not Multiple Select show select all checkbox. */
107119
selectAll?: boolean;
108120

121+
/** Use optional string to override "Select All" checkbox text instead of `formatSelectAll()`, the latter should be prefered */
122+
selectAllText?: string;
123+
109124
/** Whether or not Multiple Select allows you to select only one option.By default this option is set to false. */
110125
single?: boolean;
111126

0 commit comments

Comments
 (0)