@elastic/eui
Version:
Elastic UI Component Library
207 lines (206 loc) • 10.6 kB
JavaScript
var _excluded = ["children", "className", "id", "role", "element", "buttonElement", "buttonProps", "buttonClassName", "buttonContentClassName", "buttonContent", "arrowDisplay", "arrowProps", "extraAction", "paddingSize", "borders", "initialIsOpen", "forceState", "isLoading", "isLoadingMessage", "isDisabled", "onToggle"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
/*
* 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 React, { useState } from 'react';
import PropTypes from "prop-types";
import classNames from 'classnames';
import { useEuiMemoizedStyles, useGeneratedHtmlId } from '../../services';
import { EuiLoadingSpinner } from '../loading';
import { EuiAccordionTrigger } from './accordion_trigger';
import { EuiAccordionChildren } from './accordion_children';
import { euiAccordionStyles } from './accordion.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var PADDING_SIZES = ['none', 'xs', 's', 'm', 'l', 'xl'];
export var EuiAccordion = function EuiAccordion(_ref) {
var _buttonProps$id;
var children = _ref.children,
className = _ref.className,
id = _ref.id,
_ref$role = _ref.role,
role = _ref$role === void 0 ? 'group' : _ref$role,
_ref$element = _ref.element,
Element = _ref$element === void 0 ? 'div' : _ref$element,
_ref$buttonElement = _ref.buttonElement,
buttonElement = _ref$buttonElement === void 0 ? 'button' : _ref$buttonElement,
buttonProps = _ref.buttonProps,
buttonClassName = _ref.buttonClassName,
buttonContentClassName = _ref.buttonContentClassName,
buttonContent = _ref.buttonContent,
_ref$arrowDisplay = _ref.arrowDisplay,
arrowDisplay = _ref$arrowDisplay === void 0 ? 'left' : _ref$arrowDisplay,
arrowProps = _ref.arrowProps,
extraAction = _ref.extraAction,
_ref$paddingSize = _ref.paddingSize,
paddingSize = _ref$paddingSize === void 0 ? 'none' : _ref$paddingSize,
_ref$borders = _ref.borders,
borders = _ref$borders === void 0 ? 'none' : _ref$borders,
_ref$initialIsOpen = _ref.initialIsOpen,
initialIsOpen = _ref$initialIsOpen === void 0 ? false : _ref$initialIsOpen,
forceState = _ref.forceState,
_ref$isLoading = _ref.isLoading,
isLoading = _ref$isLoading === void 0 ? false : _ref$isLoading,
_ref$isLoadingMessage = _ref.isLoadingMessage,
isLoadingMessage = _ref$isLoadingMessage === void 0 ? false : _ref$isLoadingMessage,
_ref$isDisabled = _ref.isDisabled,
isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
onToggle = _ref.onToggle,
rest = _objectWithoutProperties(_ref, _excluded);
var _useState = useState(forceState ? forceState === 'open' : initialIsOpen),
_useState2 = _slicedToArray(_useState, 2),
isOpenState = _useState2[0],
setIsOpenState = _useState2[1];
var isOpen = forceState ? forceState === 'open' : isOpenState;
var onAccordionToggle = function onAccordionToggle() {
if (forceState) {
onToggle === null || onToggle === void 0 || onToggle(!isOpen);
} else {
var nextState = !isOpenState;
setIsOpenState(nextState);
onToggle === null || onToggle === void 0 || onToggle(nextState);
}
};
var generatedId = useGeneratedHtmlId();
var buttonId = (_buttonProps$id = buttonProps === null || buttonProps === void 0 ? void 0 : buttonProps.id) !== null && _buttonProps$id !== void 0 ? _buttonProps$id : generatedId;
var classes = classNames('euiAccordion', {
'euiAccordion-isOpen': isOpen
}, className);
var styles = useEuiMemoizedStyles(euiAccordionStyles);
var cssStyles = [styles.euiAccordion, borders !== 'none' && styles.borders.borders, borders !== 'none' && styles.borders[borders]];
return ___EmotionJSX(Element, _extends({
className: classes,
css: cssStyles
}, rest), ___EmotionJSX(EuiAccordionTrigger, {
ariaControlsId: id,
buttonId: buttonId
// Force button element to be a legend if the element is a fieldset
,
buttonElement: Element === 'fieldset' ? 'legend' : buttonElement,
buttonClassName: buttonClassName,
buttonContent: buttonContent,
buttonContentClassName: buttonContentClassName,
buttonProps: buttonProps,
arrowProps: arrowProps,
arrowDisplay: arrowDisplay,
isDisabled: isDisabled,
isOpen: isOpen,
onToggle: onAccordionToggle,
extraAction: isLoading ? ___EmotionJSX(EuiLoadingSpinner, null) : extraAction
}), ___EmotionJSX(EuiAccordionChildren, {
role: role,
id: id,
"aria-labelledby": buttonId,
paddingSize: paddingSize,
isLoading: isLoading,
isLoadingMessage: isLoadingMessage,
isOpen: isOpen,
initialIsOpen: initialIsOpen
}, children));
};
EuiAccordion.propTypes = {
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
id: PropTypes.string.isRequired,
/**
* Applied to the entire .euiAccordion wrapper.
* When using `fieldset`, it will enforce `buttonElement = legend` as well.
*/
element: PropTypes.oneOf(["div", "fieldset"]),
/**
* Defaults to the [group role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/group_role).
*
* If your accordion contains significant enough content to be a document
* [landmark role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/region_role#accessibility_concerns), consider using the `region` role instead.
* @default group
*/
role: PropTypes.any,
/**
* Class that will apply to the trigger for the accordion.
*/
buttonClassName: PropTypes.string,
/**
* Apply more props to the triggering button.
*
* Includes optional `paddingSize` prop which allows sizes of `s`, `m`, or `l`.
* Note: Padding will not be present on the side closest to the accordion arrow.
*/
buttonProps: PropTypes.shape({
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
paddingSize: PropTypes.oneOf(["s", "m", "l"])
}),
/**
* Class that will apply to the trigger content for the accordion.
*/
buttonContentClassName: PropTypes.string,
/**
* The content of the clickable trigger
*/
buttonContent: PropTypes.node,
/**
* Applied to the main button receiving the `onToggle` event.
* Anything other than the default `button` does not support removing the arrow display (for accessibility of focus).
*/
buttonElement: PropTypes.oneOf(["div", "legend", "button"]),
/**
* Extra props to pass to the EuiButtonIcon containing the arrow.
*/
arrowProps: PropTypes.any,
/**
* Will appear right aligned against the button. Useful for separate actions like deletions.
*/
extraAction: PropTypes.node,
/**
* The accordion will start in the open state.
*/
initialIsOpen: PropTypes.bool,
/**
* Optional callback method called on open and close with a single `isOpen` parameter
*/
onToggle: PropTypes.func,
/**
* The padding around the exposed accordion content.
*/
paddingSize: PropTypes.any,
/**
* Placement of the arrow indicator, or 'none' to hide it.
*/
arrowDisplay: PropTypes.oneOf(["left", "right", "none"]),
/**
* Optional border styling. Defaults to 'none'.
*/
borders: PropTypes.oneOf(["horizontal", "all", "none"]),
/**
* Control the opening of accordion via prop
*/
forceState: PropTypes.oneOf(["closed", "open"]),
/**
* Change `extraAction` and children into a loading spinner
*/
isLoading: PropTypes.bool,
/**
* Choose whether the loading message replaces the content. Customize the message by passing a node
*/
isLoadingMessage: PropTypes.oneOfType([PropTypes.bool.isRequired, PropTypes.node.isRequired]),
/**
* Disable the open/close interaction and visually subdues the trigger
*/
isDisabled: PropTypes.bool
};