feat: implemented improved additional choices

This commit is contained in:
Lars Noack 2024-02-15 16:29:36 +01:00
parent 4df9e31c09
commit 80a95b21f1
10 changed files with 74 additions and 43 deletions

View file

@ -295,6 +295,13 @@ Pass an array of objects:
}]
```
### choicesContainer
You can't place HTML directly inside option or select tags.
This is a security feature, as it prevents XSS (cross-site scripting) attacks.
If you want to do it regardless, you can set `allowHTML` to `true`, and set the choicesContainer to a valid query selector string or html element.
The children of the `choicesContainer` will be used instead of the option tags inside the select.
### renderChoiceLimit
**Type:** `Number` **Default:** `-1`

View file

@ -346,26 +346,30 @@ var Choices = /** @class */function () {
* data-choices-container
* is the attribute that defines the id of the additional option container
*/
var additionalOptionContainer = this.passedElement.element.getAttribute('data-choices-container');
if (additionalOptionContainer) {
var optionContainer_1 = document.getElementById(additionalOptionContainer);
if (optionContainer_1) {
Array.from(optionContainer_1.children).forEach(function (option) {
var value = option.getAttribute("value") || "";
_this._presetChoices.push({
value: value,
label: option.innerHTML.trim(),
selected: option.hasAttribute("selected"),
disabled: option.hasAttribute("disabled") || optionContainer_1.hasAttribute("disabled"),
placeholder: value === '' || option.hasAttribute('placeholder'),
customProperties: (0, utils_1.parseCustomProperties)(option.dataset.customProperties)
});
});
optionContainer_1.remove();
var choicesContainer = null;
var choiceContainerQuery = this.passedElement.element.dataset.choicesContainer || userConfig.choicesContainer;
if (choiceContainerQuery) {
if (choiceContainerQuery instanceof HTMLElement) {
choicesContainer = choiceContainerQuery;
} else {
console.warn("Could not find a container with id of ".concat(additionalOptionContainer, ", that is defined in the data-choices-container attribute"));
choicesContainer = this.passedElement.element.closest(choiceContainerQuery) || document.getElementById(choiceContainerQuery) || document.querySelector(choiceContainerQuery);
}
}
if (choicesContainer) {
var disableAll_1 = choicesContainer.hasAttribute("disabled");
Array.from(choicesContainer.children).forEach(function (option) {
var value = option.getAttribute("value") || "";
_this._presetChoices.push({
value: value,
label: option.innerHTML.trim(),
selected: option.hasAttribute("selected"),
disabled: option.hasAttribute("disabled") || disableAll_1,
placeholder: value === '' || option.hasAttribute('placeholder'),
customProperties: (0, utils_1.parseCustomProperties)(option.dataset.customProperties)
});
});
choicesContainer.remove();
}
this._render = this._render.bind(this);
this._onFocus = this._onFocus.bind(this);
this._onBlur = this._onBlur.bind(this);
@ -2900,6 +2904,7 @@ exports.DEFAULT_CLASSNAMES = {
exports.DEFAULT_CONFIG = {
items: [],
choices: [],
choicesContainer: null,
silent: false,
renderChoiceLimit: -1,
maxItemCount: -1,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1 +1 @@
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../../src/scripts/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAG/C,eAAO,MAAM,kBAAkB,EAAE,UA2BhC,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,OA+C5B,CAAC"}
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../../src/scripts/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAG/C,eAAO,MAAM,kBAAkB,EAAE,UA2BhC,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,OAgD5B,CAAC"}

View file

@ -80,6 +80,13 @@ export interface Options {
* @default []
*/
choices: Choice[];
/**
* You can't place HTML directly inside option or select tags.
* This is a security feature, as it prevents XSS (cross-site scripting) attacks.
* If you want to do it regardless, you can set `allowHTML` to `true`, and set the choicesContainer to a valid query selector string or html element.
* The children of the `choicesContainer` will be used instead of the option tags inside the select.
*/
choicesContainer: HTMLElement | string | null;
/**
* The amount of choices to be rendered within the dropdown list `("-1" indicates no limit)`. This is useful if you have a lot of choices where it is easier for a user to use the search area to find a choice.
*

View file

@ -1 +1 @@
{"version":3,"file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CAAC;IAEhB;;;;;;;;OAQG;IACH,cAAc,EAAE,CAAC,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;IAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,yBAAyB,EAAE,CAAC,CAAC,QAAQ,EAAE,KAAK,CAAC,OAAO,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;CACvE"}

View file

@ -312,33 +312,36 @@ class Choices implements Choices {
* is the attribute that defines the id of the additional option container
*/
const additionalOptionContainer = this.passedElement.element.getAttribute('data-choices-container');
let choicesContainer: null | HTMLElement = null;
if (additionalOptionContainer) {
const optionContainer = document.getElementById(additionalOptionContainer);
if (optionContainer) {
Array.from(optionContainer.children).forEach((option: HTMLElement) => {
let value = option.getAttribute("value") || "";
this._presetChoices.push({
value: value,
label: option.innerHTML.trim(),
selected: option.hasAttribute("selected"),
disabled: option.hasAttribute("disabled") || optionContainer.hasAttribute("disabled"),
placeholder: value === '' || option.hasAttribute('placeholder'),
customProperties: parseCustomProperties(option.dataset.customProperties),
});
});
optionContainer.remove();
let choiceContainerQuery = this.passedElement.element.dataset.choicesContainer || userConfig.choicesContainer;
if (choiceContainerQuery) {
if (choiceContainerQuery instanceof HTMLElement) {
choicesContainer = choiceContainerQuery;
} else {
console.warn(
`Could not find a container with id of ${additionalOptionContainer}, that is defined in the data-choices-container attribute`,
);
choicesContainer = this.passedElement.element.closest(choiceContainerQuery) || document.getElementById(choiceContainerQuery) || document.querySelector(choiceContainerQuery);
}
}
if (choicesContainer ) {
const disableAll = choicesContainer.hasAttribute("disabled");
Array.from(choicesContainer.children).forEach((option: HTMLElement) => {
let value = option.getAttribute("value") || "";
this._presetChoices.push({
value: value,
label: option.innerHTML.trim(),
selected: option.hasAttribute("selected"),
disabled: option.hasAttribute("disabled") || disableAll,
placeholder: value === '' || option.hasAttribute('placeholder'),
customProperties: parseCustomProperties(option.dataset.customProperties),
});
});
choicesContainer.remove();
}
this._render = this._render.bind(this);
this._onFocus = this._onFocus.bind(this);
this._onBlur = this._onBlur.bind(this);

View file

@ -34,6 +34,7 @@ export const DEFAULT_CLASSNAMES: ClassNames = {
export const DEFAULT_CONFIG: Options = {
items: [],
choices: [],
choicesContainer: null,
silent: false,
renderChoiceLimit: -1,
maxItemCount: -1,

View file

@ -84,6 +84,14 @@ export interface Options {
*/
choices: Choice[];
/**
* You can't place HTML directly inside option or select tags.
* This is a security feature, as it prevents XSS (cross-site scripting) attacks.
* If you want to do it regardless, you can set `allowHTML` to `true`, and set the choicesContainer to a valid query selector string or html element.
* The children of the `choicesContainer` will be used instead of the option tags inside the select.
*/
choicesContainer: HTMLElement | string | null;
/**
* The amount of choices to be rendered within the dropdown list `("-1" indicates no limit)`. This is useful if you have a lot of choices where it is easier for a user to use the search area to find a choice.
*