@elastic/eui
Version:
Elastic UI Component Library
86 lines (83 loc) • 3.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.setupEuiMatchers = exports.isEuiDisabled = exports.getEuiDisabledState = exports.euiMatchers = exports.default = void 0;
var _utils = require("../../utils");
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
var NATIVE_DISABLED_ATTR = 'disabled';
var CUSTOM_DISABLED_ATTR = 'aria-disabled';
var toBeEuiDisabled = function toBeEuiDisabled(element) {
var _getEuiDisabledState = getEuiDisabledState(element),
isDisabled = _getEuiDisabledState.isDisabled,
canBeDisabled = _getEuiDisabledState.canBeDisabled,
isNativelyDisabled = _getEuiDisabledState.isNativelyDisabled;
return {
message: function message() {
if (isDisabled) {
var method = isNativelyDisabled ? "`".concat(NATIVE_DISABLED_ATTR, "`") : "`".concat(CUSTOM_DISABLED_ATTR, "=\"true\"`");
if (!canBeDisabled) {
return "Element cannot be disabled (based on its role), but it was disabled via ".concat(method);
} else {
return "Expected element NOT to be disabled, but it was disabled via ".concat(method);
}
} else {
return "Expected element to be disabled via either `".concat(NATIVE_DISABLED_ATTR, "` or `").concat(CUSTOM_DISABLED_ATTR, "=\"true\"` attribute, but found neither");
}
},
pass: isDisabled
};
};
var toBeEuiEnabled = function toBeEuiEnabled(element) {
var _getEuiDisabledState2 = getEuiDisabledState(element),
isDisabled = _getEuiDisabledState2.isDisabled,
isNativelyDisabled = _getEuiDisabledState2.isNativelyDisabled,
isAriaDisabled = _getEuiDisabledState2.isAriaDisabled;
return {
message: function message() {
var attributes = [isNativelyDisabled ? "`".concat(NATIVE_DISABLED_ATTR, "`") : undefined, isAriaDisabled ? "`".concat(CUSTOM_DISABLED_ATTR, "=\"true\"`") : undefined].filter(function (item) {
return item !== undefined;
}).join(' and ');
return "Expected element NOT to have attributes: ".concat(attributes, ".");
},
pass: !isDisabled
};
};
var euiMatchers = exports.euiMatchers = {
toBeEuiDisabled: toBeEuiDisabled,
toBeEuiEnabled: toBeEuiEnabled
};
var setupEuiMatchers = exports.setupEuiMatchers = function setupEuiMatchers() {
expect.extend(euiMatchers);
};
var _default = exports.default = euiMatchers;
/* Utilities */
/**
* Retrieve an element's disabled state details.
* Checks wheather the element has an `disabled` attribute or `aria-disabled="true"` attribute
* @returns { isDisabled: boolean; canBeDisabled: boolean; isNativelyDisabled: boolean; isAriaDisabled: boolean }
*/
var getEuiDisabledState = exports.getEuiDisabledState = function getEuiDisabledState(element) {
var canBeDisabled = (0, _utils.elementCanBeDisabled)(element);
var isNativelyDisabled = element.hasAttribute('disabled');
var isAriaDisabled = element.getAttribute('aria-disabled') === 'true';
var isDisabled = canBeDisabled && (isNativelyDisabled || isAriaDisabled);
return {
isDisabled: isDisabled,
canBeDisabled: canBeDisabled,
isNativelyDisabled: isNativelyDisabled,
isAriaDisabled: isAriaDisabled
};
};
/**
* Checks if an element is disabled via `disabled` attribute or `aria-disabled="true"`.
*/
var isEuiDisabled = exports.isEuiDisabled = function isEuiDisabled(element) {
return getEuiDisabledState(element).isDisabled;
};