@elastic/eui
Version:
Elastic UI Component Library
85 lines (78 loc) • 2.45 kB
JavaScript
/*
* 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.
*/
import { FLYOUT_SIZES } from '../const';
import { LEVEL_CHILD } from './const';
/**
* Checks if a size is a named size (s, m, l)
*/
export function isNamedSize(size) {
return FLYOUT_SIZES.includes(size);
}
/**
* Validates that a managed flyout only uses named sizes
*/
export function validateManagedFlyoutSize(size, flyoutId, level) {
if (level === LEVEL_CHILD && !isNamedSize(size)) {
var namedSizes = FLYOUT_SIZES.join(', ');
return {
type: 'INVALID_SIZE_TYPE',
message: "Child flyout ".concat(flyoutId, " must use a named size (").concat(namedSizes, "). Received: ").concat(size),
flyoutId: flyoutId,
level: level,
size: size
};
}
return null;
}
/**
* Validates size combinations for parent-child flyouts
*/
export function validateSizeCombination(parentSize, childSize) {
var sizes = [parentSize, childSize];
// Parent and child can't both be 'm'
if (sizes.every(function (s) {
return s === 'm';
})) {
return {
type: 'INVALID_SIZE_COMBINATION',
message: 'Parent and child flyouts cannot both be size "m"'
};
}
// Parent and child can't both be 'fill'
if (sizes.every(function (s) {
return s === 'fill';
})) {
return {
type: 'INVALID_SIZE_COMBINATION',
message: 'Parent and child flyouts cannot both be size "fill"'
};
}
// Flyout can't be 'l' if the other in the pair is not "fill"
if (sizes.includes('l') && !sizes.includes('fill')) {
return {
type: 'INVALID_SIZE_COMBINATION',
message: 'Flyouts cannot be size "l" unless the other flyout is "fill"'
};
}
return null;
}
/**
* Creates a user-friendly error message for validation errors
*/
export function createValidationErrorMessage(error) {
console.error(error);
var prefix = "EuiFlyout validation error";
switch (error.type) {
case 'INVALID_SIZE_TYPE':
case 'INVALID_SIZE_COMBINATION':
case 'INVALID_FLYOUT_MENU_TITLE':
return "".concat(prefix, ": ").concat(error.message);
default:
return "".concat(prefix, ": Unknown validation error");
}
}