|
1 | 1 | import ContentEditable from "../lib/ContentEditable" |
2 | 2 | import "./App.css" |
3 | | -import { useState } from "react" |
| 3 | +import { useEffect, useState } from "react" |
4 | 4 |
|
5 | 5 | function App() { |
| 6 | + const [emptyContent, setEmptyContent] = useState<string | undefined>( |
| 7 | + undefined |
| 8 | + ) |
6 | 9 | const [content, setContent] = useState("") |
| 10 | + const [isFocused, setIsFocused] = useState(false) |
| 11 | + const [isBlurred, setIsBlurred] = useState(false) |
| 12 | + const [keyDown, setKeyDown] = useState("") |
| 13 | + const [keyUp, setKeyUp] = useState("") |
| 14 | + const [messageHistory, setMessageHistory] = useState<string[]>([ |
| 15 | + "Type something and press on 'Send' to send another message...", |
| 16 | + ]) |
| 17 | + |
| 18 | + const truncateString = (str: string, limit: number) => { |
| 19 | + return str.length > limit ? str.slice(0, limit) + "..." : str |
| 20 | + } |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + setEmptyContent(undefined) |
| 24 | + }, [emptyContent]) |
| 25 | + |
| 26 | + const saveMessageInHistory = (message: string) => { |
| 27 | + setMessageHistory([...messageHistory, message]) |
| 28 | + setContent("") |
| 29 | + setEmptyContent("") |
| 30 | + } |
| 31 | + |
7 | 32 | return ( |
8 | | - <div className="main-container full-width"> |
9 | | - <div style={{ whiteSpace: "pre-wrap" }}>{content}</div> |
| 33 | + <div className="full-width main-container"> |
| 34 | + <div className="full-width message-history-container"> |
| 35 | + {messageHistory.map((message, index) => ( |
| 36 | + <div key={index} className="message-item"> |
| 37 | + {message} |
| 38 | + </div> |
| 39 | + ))} |
| 40 | + </div> |
10 | 41 | <ContentEditable |
11 | 42 | placeholder="Type here" |
12 | 43 | containerClassName="full-width input-container" |
13 | 44 | contentEditableClassName="full-width input-element" |
14 | 45 | placeholderClassName="input-placeholder" |
| 46 | + updatedContent={emptyContent} |
15 | 47 | onChange={(content) => setContent(content)} |
| 48 | + onFocus={() => { |
| 49 | + setIsFocused(true) |
| 50 | + setIsBlurred(false) |
| 51 | + }} |
| 52 | + onBlur={() => { |
| 53 | + setIsFocused(false) |
| 54 | + setIsBlurred(true) |
| 55 | + }} |
| 56 | + onKeyDown={(e) => setKeyDown(e.key)} |
| 57 | + onKeyUp={(e) => setKeyUp(e.key)} |
16 | 58 | /> |
| 59 | + <div className="full-width metrics-section"> |
| 60 | + <div className="metrics-section__left-box"> |
| 61 | + <div> |
| 62 | + Content: <b>{truncateString(content, 200)}</b> |
| 63 | + </div> |
| 64 | + <div> |
| 65 | + Is focused:{" "} |
| 66 | + <b className={isFocused ? "truthy" : "falsy"}> |
| 67 | + {isFocused ? "true" : "false"} |
| 68 | + </b> |
| 69 | + </div> |
| 70 | + <div> |
| 71 | + Is blurred:{" "} |
| 72 | + <b className={isBlurred ? "truthy" : "falsy"}> |
| 73 | + {isBlurred ? "true" : "false"} |
| 74 | + </b> |
| 75 | + </div> |
| 76 | + <div> |
| 77 | + Key down: <b>{keyDown}</b> |
| 78 | + </div> |
| 79 | + <div> |
| 80 | + Key up: <b>{keyUp}</b> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <div> |
| 84 | + <button onClick={() => saveMessageInHistory(content)}>Send</button> |
| 85 | + </div> |
| 86 | + </div> |
17 | 87 | </div> |
18 | 88 | ) |
19 | 89 | } |
|
0 commit comments