feat: implemented data attribute to allow userConfig

This commit is contained in:
Lars Noack 2024-02-15 15:38:39 +01:00
parent c04cadc629
commit 4df9e31c09
4 changed files with 31 additions and 23 deletions

View file

@ -236,6 +236,14 @@ var Choices = /** @class */function () {
userConfig = {};
}
var _this = this;
var passedElement = typeof element === 'string' ? document.querySelector(element) : element;
if (!(passedElement instanceof HTMLInputElement || passedElement instanceof HTMLSelectElement)) {
throw TypeError('Expected one of the following types text|select-one|select-multiple');
}
// read from data attributes if necessary
if (userConfig.allowHTML === undefined && "allowHtml" in passedElement.dataset) {
userConfig.allowHTML = passedElement.dataset.allowHtml === 'true';
}
if (userConfig.allowHTML === undefined) {
console.warn('Deprecation warning: allowHTML will default to false in a future release. To render HTML in Choices, you will need to set it to true. Setting allowHTML will suppress this message.');
}
@ -251,10 +259,6 @@ var Choices = /** @class */function () {
if (invalidConfigOptions.length) {
console.warn('Unknown config option(s) passed', invalidConfigOptions.join(', '));
}
var passedElement = typeof element === 'string' ? document.querySelector(element) : element;
if (!(passedElement instanceof HTMLInputElement || passedElement instanceof HTMLSelectElement)) {
throw TypeError('Expected one of the following types text|select-one|select-multiple');
}
this._isTextElement = passedElement.type === constants_1.TEXT_TYPE;
this._isSelectOneElement = passedElement.type === constants_1.SELECT_ONE_TYPE;
this._isSelectMultipleElement = passedElement.type === constants_1.SELECT_MULTIPLE_TYPE;
@ -346,7 +350,6 @@ var Choices = /** @class */function () {
if (additionalOptionContainer) {
var optionContainer_1 = document.getElementById(additionalOptionContainer);
if (optionContainer_1) {
console.log('optionContainer', optionContainer_1);
Array.from(optionContainer_1.children).forEach(function (option) {
var value = option.getAttribute("value") || "";
_this._presetChoices.push({

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -152,6 +152,27 @@ class Choices implements Choices {
| HTMLSelectElement = '[data-choice]',
userConfig: Partial<Options> = {},
) {
const passedElement =
typeof element === 'string' ? document.querySelector(element) : element;
if (
!(
passedElement instanceof HTMLInputElement ||
passedElement instanceof HTMLSelectElement
)
) {
throw TypeError(
'Expected one of the following types text|select-one|select-multiple',
);
}
// read from data attributes if necessary
if (userConfig.allowHTML === undefined && "allowHtml" in passedElement.dataset) {
userConfig.allowHTML = passedElement.dataset.allowHtml === 'true';
}
if (userConfig.allowHTML === undefined) {
console.warn(
'Deprecation warning: allowHTML will default to false in a future release. To render HTML in Choices, you will need to set it to true. Setting allowHTML will suppress this message.',
@ -173,20 +194,6 @@ class Choices implements Choices {
);
}
const passedElement =
typeof element === 'string' ? document.querySelector(element) : element;
if (
!(
passedElement instanceof HTMLInputElement ||
passedElement instanceof HTMLSelectElement
)
) {
throw TypeError(
'Expected one of the following types text|select-one|select-multiple',
);
}
this._isTextElement = passedElement.type === TEXT_TYPE;
this._isSelectOneElement = passedElement.type === SELECT_ONE_TYPE;
this._isSelectMultipleElement = passedElement.type === SELECT_MULTIPLE_TYPE;
@ -311,8 +318,6 @@ class Choices implements Choices {
const optionContainer = document.getElementById(additionalOptionContainer);
if (optionContainer) {
console.log('optionContainer', optionContainer);
Array.from(optionContainer.children).forEach((option: HTMLElement) => {
let value = option.getAttribute("value") || "";