diff --git a/src/elements/content-explorer/ContentExplorer.js b/src/elements/content-explorer/ContentExplorer.js index 7983e76e47..e458fe66c4 100644 --- a/src/elements/content-explorer/ContentExplorer.js +++ b/src/elements/content-explorer/ContentExplorer.js @@ -1782,8 +1782,6 @@ class ContentExplorer extends Component { item={selected} isLoading={isLoading} errorCode={errorCode} - parentElement={this.rootElement} - appElement={this.appElement} /> ) : null} {canShare && selected && !!this.appElement ? ( diff --git a/src/elements/content-explorer/RenameDialog.js b/src/elements/content-explorer/RenameDialog.js.flow similarity index 100% rename from src/elements/content-explorer/RenameDialog.js rename to src/elements/content-explorer/RenameDialog.js.flow diff --git a/src/elements/content-explorer/RenameDialog.scss b/src/elements/content-explorer/RenameDialog.scss new file mode 100644 index 0000000000..4f2a8cdba7 --- /dev/null +++ b/src/elements/content-explorer/RenameDialog.scss @@ -0,0 +1,18 @@ +@import '../common/variables'; + +.be-modal-rename { + width: 400px; + + .be-modal-btns { + display: flex; + justify-content: center; + } + + .be-modal-error { + color: $red; + } + + input { + width: 100%; + } +} diff --git a/src/elements/content-explorer/RenameDialog.tsx b/src/elements/content-explorer/RenameDialog.tsx new file mode 100644 index 0000000000..75270b1a65 --- /dev/null +++ b/src/elements/content-explorer/RenameDialog.tsx @@ -0,0 +1,114 @@ +import * as React from 'react'; +import { useIntl, FormattedMessage } from 'react-intl'; +import { Modal } from '@box/blueprint-web'; +import type { BoxItem } from '../../common/types/core'; + +import { ERROR_CODE_ITEM_NAME_TOO_LONG, ERROR_CODE_ITEM_NAME_IN_USE } from '../../constants'; + +import messages from '../common/messages'; + +import './RenameDialog.scss'; + +export interface RenameDialogProps { + errorCode: string; + isLoading: boolean; + isOpen: boolean; + item: BoxItem; + onCancel: () => void; + onRename: (nameWithoutExt: string, extension: string) => void; +} + +/* eslint-disable jsx-a11y/label-has-for */ +const RenameDialog = ({ errorCode, isLoading, isOpen, item, onCancel, onRename }: RenameDialogProps) => { + const { formatMessage } = useIntl(); + + let textInput = null; + let error; + + const { name = '', extension } = item; + const ext = extension ? `.${extension}` : ''; + const nameWithoutExt = extension ? name.replace(ext, '') : name; + + /** + * Appends the extension and calls rename function + */ + const rename = () => { + if (textInput && textInput.value) { + if (textInput.value === nameWithoutExt) { + onCancel(); + } else { + onRename(textInput.value, ext); + } + } + }; + + /** + * 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': + rename(); + break; + default: + break; + } + }; + + switch (errorCode) { + case ERROR_CODE_ITEM_NAME_IN_USE: + error = messages.renameDialogErrorInUse; + break; + case ERROR_CODE_ITEM_NAME_TOO_LONG: + error = messages.renameDialogErrorTooLong; + break; + default: + error = errorCode ? messages.renameDialogErrorInvalid : null; + break; + } + + return ( + + + + {error ? ( +
+ +
+ ) : null} + + +
+ + + {formatMessage(messages.rename)} + + + {formatMessage(messages.cancel)} + + +
+
+ ); +}; + +export default RenameDialog;