import * as React from 'react'; import Modal from 'react-modal'; import { injectIntl, FormattedMessage } from 'react-intl'; import type { IntlShape } from 'react-intl'; import PrimaryButton from '../../../components/primary-button/PrimaryButton'; import Button from '../../../components/button/Button'; import messages from '../messages'; import { CLASS_MODAL_CONTENT, CLASS_MODAL_OVERLAY, CLASS_MODAL, ERROR_CODE_ITEM_NAME_TOO_LONG, ERROR_CODE_ITEM_NAME_IN_USE, } from '../../../constants'; type Props = { appElement: HTMLElement, errorCode: string, intl: IntlShape, isLoading: boolean, isOpen: boolean, onCancel: any, onCreate: any, parentElement: HTMLElement }; /* eslint-disable jsx-a11y/label-has-for */ const CreateFolderDialog = ({ isOpen, onCreate, onCancel, isLoading, errorCode, parentElement, appElement, intl, }: Props) => { let textInput = null; let error; /** * Appends the extension and calls rename function */ const create = () => { if (textInput && textInput.value) { onCreate(textInput.value); } }; /** * Grabs reference to the input element */ const ref = input => { textInput = input; if (textInput instanceof HTMLInputElement) { textInput.focus(); textInput.select(); } }; /** * Handles enter key down */ const onKeyDown = ({ key }) => { switch (key) { case 'Enter': create(); break; default: break; } }; switch (errorCode) { case ERROR_CODE_ITEM_NAME_IN_USE: error = messages.createDialogErrorInUse; break; case ERROR_CODE_ITEM_NAME_TOO_LONG: error = messages.createDialogErrorTooLong; break; default: error = errorCode ? messages.createDialogErrorInvalid : null; break; } return ( parentElement} portalClassName={CLASS_MODAL} >
); }; export default injectIntl(CreateFolderDialog);