Skip to content

Commit 036ed38

Browse files
yeion7SaraVieira
authored andcommitted
Show error when rename a file with invalid name (codesandbox#2908)
* show error * fix rename file with same name not show error * remove useless style
1 parent 02f678c commit 036ed38

File tree

5 files changed

+96
-64
lines changed

5 files changed

+96
-64
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryTitleInput/elements.js renamed to packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryTitleInput/elements.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import styled from 'styled-components';
22

3-
export const InputContainer = styled.div`
3+
export const InputContainer = styled.div<{ errorMessage?: boolean }>`
44
display: inline-block;
5-
overflow: auto;
5+
66
input {
77
transition: 0.3s ease all;
88
font-family: inherit;
9-
position: absolute;
109
border: 1px solid ${props => props.theme.primary};
1110
outline: none;
1211
background-color: ${props =>
@@ -24,3 +23,9 @@ export const InputContainer = styled.div`
2423
}
2524
}
2625
`;
26+
27+
export const InputError = styled.p`
28+
font-size: 0.9em;
29+
padding: 0.25rem;
30+
margin: 0;
31+
`;

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryTitleInput/index.js

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import React, {
2+
useState,
3+
ChangeEvent,
4+
KeyboardEvent,
5+
FunctionComponent,
6+
} from 'react';
7+
import { VisuallyHidden } from 'reakit/VisuallyHidden';
8+
9+
import { ESC, ENTER } from '@codesandbox/common/lib/utils/keycodes';
10+
11+
import { InputContainer, InputError } from './elements';
12+
13+
function select(el) {
14+
if (el) {
15+
el.select();
16+
}
17+
}
18+
19+
interface Props {
20+
title: string;
21+
onCommit: (value: string, force?: boolean) => void;
22+
onChange: (value: string) => void;
23+
onCancel: () => void;
24+
error: string | null;
25+
id: string;
26+
}
27+
28+
export const EntryTitleInput: FunctionComponent<Props> = ({
29+
title,
30+
onCommit,
31+
onCancel,
32+
onChange,
33+
error,
34+
id,
35+
}) => {
36+
const [currentValue, setCurretValue] = useState(title);
37+
38+
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
39+
if (e.target) {
40+
const { value } = e.target;
41+
onChange(value);
42+
setCurretValue(value);
43+
}
44+
};
45+
46+
const handleKeyUp = (e: KeyboardEvent<HTMLInputElement>) => {
47+
if (e.keyCode === ENTER) {
48+
onCommit(currentValue.trim());
49+
} else if (e.keyCode === ESC) {
50+
onCancel();
51+
}
52+
};
53+
54+
return (
55+
<InputContainer>
56+
<VisuallyHidden>
57+
<label id={`label-${id}`} htmlFor={`input-${id}`}>
58+
{title ? `rename ${title}` : 'set name new file'}
59+
</label>
60+
</VisuallyHidden>
61+
<input
62+
onChange={handleChange}
63+
onBlur={() => onCommit(currentValue, true)}
64+
onKeyUp={handleKeyUp}
65+
ref={select}
66+
value={currentValue}
67+
id={`input-${id}`}
68+
aria-invalid={Boolean(error)}
69+
aria-labelledby={`label-${id}`}
70+
aria-describedby={`error-${id}`}
71+
/>
72+
{error && typeof error === 'string' && (
73+
<InputError role="alert" id={`error-${id}`}>
74+
{error}
75+
</InputError>
76+
)}
77+
</InputContainer>
78+
);
79+
};

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import EditIcons from './EditIcons';
1313
import { NotSyncedIconWithMargin, Right } from './elements';
1414
import EntryIcons from './EntryIcons';
1515
import EntryTitle from './EntryTitle';
16-
import EntryTitleInput from './EntryTitleInput';
16+
import { EntryTitleInput } from './EntryTitleInput';
1717

1818
interface IEntryProps {
19-
renameValidator: (id: string, title: string) => boolean;
19+
renameValidator: (id: string, title: string) => string;
2020
shortid: string;
2121
id: string;
2222
title: string;
@@ -67,7 +67,7 @@ const Entry: React.FC<IEntryProps> = ({
6767
state: incomingState = '',
6868
}) => {
6969
const [state, setState] = useState(incomingState);
70-
const [error, setError] = useState(false);
70+
const [error, setError] = useState<string | null>(null);
7171
const [hovering, setHovering] = useState(false);
7272

7373
const resetState = () => {
@@ -76,7 +76,7 @@ const Entry: React.FC<IEntryProps> = ({
7676
}
7777

7878
setState('');
79-
setError(false);
79+
setError(null);
8080
};
8181

8282
const handleValidateTitle = (titleToValidate: string) => {
@@ -97,7 +97,7 @@ const Entry: React.FC<IEntryProps> = ({
9797
const discardModuleChangesAction = () =>
9898
discardModuleChanges ? discardModuleChanges(shortid) : false;
9999

100-
const handleRename = (newTitle: string, force: boolean) => {
100+
const handleRename = (newTitle: string, force: boolean = false) => {
101101
if (newTitle === title) {
102102
resetState();
103103
return;
@@ -174,10 +174,12 @@ const Entry: React.FC<IEntryProps> = ({
174174
<EntryIcons type={type} error={moduleHasError} />
175175
{state === 'editing' ? (
176176
<EntryTitleInput
177+
id={id}
177178
title={title}
178179
onChange={handleValidateTitle}
179180
onCancel={resetState}
180181
onCommit={handleRename}
182+
error={error}
181183
/>
182184
) : (
183185
<EntryTitle title={title} />

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,8 @@ class DirectoryEntry extends React.PureComponent {
168168

169169
setOpen = open => this.setState({ open });
170170

171-
validateModuleTitle = (_, title) => {
172-
const { id } = this.props;
173-
174-
return validateTitle(id, title, this.getChildren());
175-
};
171+
validateModuleTitle = (id, title) =>
172+
validateTitle(id, title, this.getChildren());
176173

177174
validateDirectoryTitle = (id, title) => {
178175
const { root } = this.props;

0 commit comments

Comments
 (0)