mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-29 19:00:43 +02:00
Add wrapped element getters + fix some types
This commit is contained in:
parent
452c8fa666
commit
049ccc8b5a
|
@ -37,16 +37,18 @@ import {
|
|||
diff,
|
||||
} from './lib/utils';
|
||||
|
||||
const USER_DEFAULTS = /** @type {Partial<import('../../types/index').Choices.Options>} */ ({});
|
||||
/**
|
||||
* @typedef {import('../../types/index').Choices.Choice} Choice
|
||||
* @typedef {import('../../types/index').Choices.Options} Options
|
||||
*/
|
||||
|
||||
/** @type {Partial<Options>} */
|
||||
const USER_DEFAULTS = {};
|
||||
|
||||
/**
|
||||
* Choices
|
||||
* @author Josh Johnson<josh@joshuajohnson.co.uk>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('../../types/index').Choices.Choice} Choice
|
||||
*/
|
||||
class Choices {
|
||||
static get defaults() {
|
||||
return Object.preventExtensions({
|
||||
|
@ -61,9 +63,10 @@ class Choices {
|
|||
|
||||
/**
|
||||
* @param {string | HTMLInputElement | HTMLSelectElement} element
|
||||
* @param {Partial<import('../../types/index').Choices.Options>} userConfig
|
||||
* @param {Partial<Options>} userConfig
|
||||
*/
|
||||
constructor(element = '[data-choice]', userConfig = {}) {
|
||||
/** @type {Partial<Options>} */
|
||||
this.config = merge.all(
|
||||
[DEFAULT_CONFIG, Choices.defaults.options, userConfig],
|
||||
// When merging array configs, replace with a copy of the userConfig array,
|
||||
|
@ -148,7 +151,7 @@ class Choices {
|
|||
* or when calculated direction is different from the document
|
||||
* @type {HTMLElement['dir']}
|
||||
*/
|
||||
this._direction = this.passedElement.element.dir;
|
||||
this._direction = this.passedElement.dir;
|
||||
|
||||
if (!this._direction) {
|
||||
const { direction: elementDirection } = window.getComputedStyle(
|
||||
|
@ -202,7 +205,7 @@ class Choices {
|
|||
}
|
||||
|
||||
// If element has already been initialised with Choices, fail silently
|
||||
if (this.passedElement.element.getAttribute('data-choice') === 'active') {
|
||||
if (this.passedElement.isActive) {
|
||||
if (!this.config.silent) {
|
||||
console.warn(
|
||||
'Trying to initialise Choices on element already initialised',
|
||||
|
|
|
@ -11,6 +11,14 @@ export default class WrappedElement {
|
|||
this.isDisabled = false;
|
||||
}
|
||||
|
||||
get isActive() {
|
||||
return this.element.dataset.choice === 'active';
|
||||
}
|
||||
|
||||
get dir() {
|
||||
return this.element.dir;
|
||||
}
|
||||
|
||||
get value() {
|
||||
return this.element.value;
|
||||
}
|
||||
|
|
|
@ -39,6 +39,22 @@ describe('components/wrappedElement', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('isActive getter', () => {
|
||||
it('returns whether the "data-choice" attribute is set to "active"', () => {
|
||||
instance.element.dataset.choice = 'active';
|
||||
expect(instance.isActive).to.equal(true);
|
||||
|
||||
instance.element.dataset.choice = 'inactive';
|
||||
expect(instance.isActive).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('dir getter', () => {
|
||||
it('returns the direction of the element', () => {
|
||||
expect(instance.dir).to.equal(instance.element.dir);
|
||||
});
|
||||
});
|
||||
|
||||
describe('conceal', () => {
|
||||
let originalStyling;
|
||||
|
||||
|
|
Loading…
Reference in a new issue