refatcor: make ProjectName a functional component (#3695)

This commit is contained in:
Aakansha Doshi 2021-06-04 21:22:09 +05:30 committed by GitHub
parent 0d19e9210c
commit d0867d1c3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
import "./TextInput.scss"; import "./TextInput.scss";
import React, { Component } from "react"; import React, { useState } from "react";
import { focusNearestParent } from "../utils"; import { focusNearestParent } from "../utils";
import "./ProjectName.scss"; import "./ProjectName.scss";
@ -12,22 +12,18 @@ type Props = {
isNameEditable: boolean; isNameEditable: boolean;
}; };
type State = { export const ProjectName = (props: Props) => {
fileName: string; const [fileName, setFileName] = useState<string>(props.value);
};
export class ProjectName extends Component<Props, State> { const handleBlur = (event: any) => {
state = {
fileName: this.props.value,
};
private handleBlur = (event: any) => {
focusNearestParent(event.target); focusNearestParent(event.target);
const value = event.target.value; const value = event.target.value;
if (value !== this.props.value) { if (value !== props.value) {
this.props.onChange(value); props.onChange(value);
} }
}; };
private handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => { const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
if (event.key === "Enter") { if (event.key === "Enter") {
event.preventDefault(); event.preventDefault();
if (event.nativeEvent.isComposing || event.keyCode === 229) { if (event.nativeEvent.isComposing || event.keyCode === 229) {
@ -37,29 +33,25 @@ export class ProjectName extends Component<Props, State> {
} }
}; };
public render() {
return ( return (
<div className="ProjectName"> <div className="ProjectName">
<label className="ProjectName-label" htmlFor="filename"> <label className="ProjectName-label" htmlFor="filename">
{`${this.props.label}${this.props.isNameEditable ? "" : ":"}`} {`${props.label}${props.isNameEditable ? "" : ":"}`}
</label> </label>
{this.props.isNameEditable ? ( {props.isNameEditable ? (
<input <input
className="TextInput" className="TextInput"
onBlur={this.handleBlur} onBlur={handleBlur}
onKeyDown={this.handleKeyDown} onKeyDown={handleKeyDown}
id="filename" id="filename"
value={this.state.fileName} value={fileName}
onChange={(event) => onChange={(event) => setFileName(event.target.value)}
this.setState({ fileName: event.target.value })
}
/> />
) : ( ) : (
<span className="TextInput TextInput--readonly" id="filename"> <span className="TextInput TextInput--readonly" id="filename">
{this.props.value} {props.value}
</span> </span>
)} )}
</div> </div>
); );
} };
}