Skip to content

Commit

Permalink
refatcor: make ProjectName a functional component (#3695)
Browse files Browse the repository at this point in the history
  • Loading branch information
ad1992 committed Jun 4, 2021
1 parent 0d19e92 commit d0867d1
Showing 1 changed file with 30 additions and 38 deletions.
68 changes: 30 additions & 38 deletions src/components/ProjectName.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./TextInput.scss";

import React, { Component } from "react";
import React, { useState } from "react";
import { focusNearestParent } from "../utils";

import "./ProjectName.scss";
Expand All @@ -12,22 +12,18 @@ type Props = {
isNameEditable: boolean;
};

type State = {
fileName: string;
};
export class ProjectName extends Component<Props, State> {
state = {
fileName: this.props.value,
};
private handleBlur = (event: any) => {
export const ProjectName = (props: Props) => {
const [fileName, setFileName] = useState<string>(props.value);

const handleBlur = (event: any) => {
focusNearestParent(event.target);
const value = event.target.value;
if (value !== this.props.value) {
this.props.onChange(value);
if (value !== props.value) {
props.onChange(value);
}
};

private handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
if (event.key === "Enter") {
event.preventDefault();
if (event.nativeEvent.isComposing || event.keyCode === 229) {
Expand All @@ -37,29 +33,25 @@ export class ProjectName extends Component<Props, State> {
}
};

public render() {
return (
<div className="ProjectName">
<label className="ProjectName-label" htmlFor="filename">
{`${this.props.label}${this.props.isNameEditable ? "" : ":"}`}
</label>
{this.props.isNameEditable ? (
<input
className="TextInput"
onBlur={this.handleBlur}
onKeyDown={this.handleKeyDown}
id="filename"
value={this.state.fileName}
onChange={(event) =>
this.setState({ fileName: event.target.value })
}
/>
) : (
<span className="TextInput TextInput--readonly" id="filename">
{this.props.value}
</span>
)}
</div>
);
}
}
return (
<div className="ProjectName">
<label className="ProjectName-label" htmlFor="filename">
{`${props.label}${props.isNameEditable ? "" : ":"}`}
</label>
{props.isNameEditable ? (
<input
className="TextInput"
onBlur={handleBlur}
onKeyDown={handleKeyDown}
id="filename"
value={fileName}
onChange={(event) => setFileName(event.target.value)}
/>
) : (
<span className="TextInput TextInput--readonly" id="filename">
{props.value}
</span>
)}
</div>
);
};

1 comment on commit d0867d1

@vercel
Copy link

@vercel vercel bot commented on d0867d1 Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.