import React, { useState } from "react";
import "./PersonName.css";
import { TextField, Button } from '@mui/material';

const PersonName = ({ name, setName }: any) => {
  const [edit, setEdit] = useState(name);

  const sendName = () => {
    setName(edit);
  }
  
  const nameChange = (event: any) => {
    setEdit(event.target.value);
  }
  
  const nameKeyPress = (event: any) => {
    if (event.key === "Enter") {
      setName(edit ? edit : name);
    }
  }

  return (
    <div className="PersonName">
      <TextField className="nameInput"
        onChange={nameChange}
        onKeyPress={nameKeyPress}
        label="Enter your name"
        variant="outlined"
        value={edit}
        />
        <Button onClick={sendName}>Set</Button>
    </div>
  );
};

export { PersonName };
