@elastic/eui
Version:
Elastic UI Component Library
131 lines (130 loc) • 5.36 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
/*
* 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, useMemo, forwardRef, useContext } from 'react';
import { useEuiMemoizedStyles } from '../../services';
import { EuiLoadingSpinner } from '../loading';
import { EuiButtonEmpty, EuiButtonIcon } from '../button';
import { EuiI18n, useEuiI18n } from '../i18n';
import { EuiPopover, EuiPopoverTitle } from '../popover';
import { EuiText } from '../text';
import { EuiToolTip } from '../tool_tip';
import { EuiMarkdownContext } from './markdown_context';
import { euiMarkdownEditorFooterStyles } from './markdown_editor_footer.styles';
import { EuiMarkdownEditorHelpButton } from './markdown_editor_help_button';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var EuiMarkdownEditorFooter = /*#__PURE__*/forwardRef(function (props, ref) {
var uiPlugins = props.uiPlugins,
isUploadingFiles = props.isUploadingFiles,
openFiles = props.openFiles,
errors = props.errors,
hasUnacceptedItems = props.hasUnacceptedItems,
dropHandlers = props.dropHandlers;
var styles = useEuiMemoizedStyles(euiMarkdownEditorFooterStyles);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
isPopoverOpen = _useState2[0],
setIsPopoverOpen = _useState2[1];
var onButtonClick = function onButtonClick() {
return setIsPopoverOpen(function (isPopoverOpen) {
return !isPopoverOpen;
});
};
var closePopover = function closePopover() {
return setIsPopoverOpen(false);
};
var uploadButton;
var supportedFileTypes = useMemo(function () {
return dropHandlers.map(function (_ref) {
var supportedFiles = _ref.supportedFiles;
return supportedFiles.join(', ');
}).sort().join(', ');
}, [dropHandlers]);
var ariaLabels = {
uploadingFiles: useEuiI18n('euiMarkdownEditorFooter.uploadingFiles', 'Click to upload files'),
openUploadModal: useEuiI18n('euiMarkdownEditorFooter.openUploadModal', 'Open upload files modal'),
unsupportedFileType: useEuiI18n('euiMarkdownEditorFooter.unsupportedFileType', 'File type not supported'),
supportedFileTypes: useEuiI18n('euiMarkdownEditorFooter.supportedFileTypes', 'Supported files: {supportedFileTypes}', {
supportedFileTypes: supportedFileTypes
}),
showSyntaxErrors: useEuiI18n('euiMarkdownEditorFooter.showSyntaxErrors', 'Show errors')
};
var _useContext = useContext(EuiMarkdownContext),
readOnly = _useContext.readOnly;
if (isUploadingFiles) {
uploadButton = ___EmotionJSX(EuiButtonIcon, {
size: "s",
iconType: EuiLoadingSpinner,
"aria-label": ariaLabels.uploadingFiles,
isDisabled: readOnly
});
} else if (dropHandlers.length > 0 && hasUnacceptedItems) {
uploadButton = ___EmotionJSX(EuiToolTip, {
content: ariaLabels.supportedFileTypes
}, ___EmotionJSX(EuiButtonEmpty, {
css: styles.euiMarkdownEditorFooter__uploadError,
className: "euiMarkdownEditorFooter__uploadError",
autoFocus: true,
size: "s",
iconType: "paperClip",
color: "danger",
"aria-label": "".concat(ariaLabels.unsupportedFileType, ". ").concat(ariaLabels.supportedFileTypes, ". ").concat(ariaLabels.uploadingFiles),
onClick: openFiles,
isDisabled: readOnly
}, ariaLabels.unsupportedFileType));
} else if (dropHandlers.length > 0) {
uploadButton = ___EmotionJSX(EuiButtonIcon, {
size: "s",
iconType: "paperClip",
color: "text",
"aria-label": ariaLabels.openUploadModal,
onClick: openFiles,
isDisabled: readOnly
});
}
var errorsButton;
if (errors && errors.length) {
errorsButton = ___EmotionJSX(EuiPopover, {
button: ___EmotionJSX(EuiButtonEmpty, {
iconType: "error",
size: "s",
color: "danger",
"aria-label": ariaLabels.showSyntaxErrors,
onClick: onButtonClick,
isDisabled: readOnly
}, errors.length),
isOpen: isPopoverOpen,
closePopover: closePopover,
panelPaddingSize: "s",
anchorPosition: "upCenter"
}, ___EmotionJSX("div", {
css: styles.euiMarkdownEditorFooter__popover,
className: "euiMarkdownEditorFooter__popover"
}, ___EmotionJSX(EuiPopoverTitle, null, ___EmotionJSX(EuiI18n, {
token: "euiMarkdownEditorFooter.errorsTitle",
default: "Errors"
})), errors.map(function (message, idx) {
return ___EmotionJSX(EuiText, {
size: "s",
key: idx
}, message.toString());
})));
}
return ___EmotionJSX("div", {
ref: ref,
css: styles.euiMarkdownEditorFooter,
className: "euiMarkdownEditorFooter",
"data-test-subj": "euiMarkdownEditorFooter"
}, ___EmotionJSX("div", {
css: styles.euiMarkdownEditorFooter__actions,
className: "euiMarkdownEditorFooter__actions"
}, uploadButton, errorsButton), ___EmotionJSX(EuiMarkdownEditorHelpButton, {
uiPlugins: uiPlugins
}));
});
EuiMarkdownEditorFooter.displayName = 'EuiMarkdownEditorFooter';