Skip to content

Commit

Permalink
feat: Add Send button to CommandInput component
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoth committed Oct 21, 2024
1 parent 044ee05 commit 35efb31
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
47 changes: 44 additions & 3 deletions src/components/input.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
.command-input-container {
display: flex;
align-items: flex-start;
width: 100%;
margin-bottom: 10px;
}

textarea {
flex-grow: 1;
form-sizing: normal;
border: 1px solid #ccc;
border-radius: 5px;
border-radius: 5px 0 0 5px;
padding: 10px;
width: 98%;
height: 75px;
font-size: 0.8em;
font-family: Monaco, Consolas, "Liberation Mono", "Courier New", Courier, monospace;
}
resize: vertical;
}

.send-button {
height: 75px;
padding: 0 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 0 5px 5px 0;
cursor: pointer;
font-size: 1em;
transition: background-color 0.3s;
}

.send-button:hover {
background-color: #45a049;
}

@media (max-width: 600px) {
.command-input-container {
flex-direction: column;
}

textarea {
width: 100%;
border-radius: 5px 5px 0 0;
}

.send-button {
width: 100%;
height: 40px;
border-radius: 0 0 5px 5px;
}
}
35 changes: 23 additions & 12 deletions src/components/input.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useRef } from "react";
import { CommandHistory } from "../CommandHistory"; // Assuming CommandHistory is in CommandHistory.js
import { CommandHistory } from "../CommandHistory";
import "./input.css";

type SendFunction = (text: string) => void;
Expand All @@ -18,9 +18,7 @@ const CommandInput = ({ onSend, inputRef }: Props) => {

if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
onSend(input);
commandHistory.addCommand(input);
setInput("");
handleSend();
} else if (e.key === "ArrowUp") {
e.preventDefault();
const prevCommand = commandHistory.navigateUp(input);
Expand All @@ -32,15 +30,28 @@ const CommandInput = ({ onSend, inputRef }: Props) => {
}
};

const handleSend = () => {
if (input.trim()) {
onSend(input);
commandHistoryRef.current.addCommand(input);
setInput("");
}
};

return (
<textarea
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
id="command-input"
ref={inputRef}
autoFocus
/>
<div className="command-input-container">
<textarea
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
id="command-input"
ref={inputRef}
autoFocus
/>
<button onClick={handleSend} className="send-button">
Send
</button>
</div>
);
};

Expand Down

0 comments on commit 35efb31

Please sign in to comment.