From 3f681abc6f9bbd0dd5a931dc7b1daab4454edfcd Mon Sep 17 00:00:00 2001 From: Matheus Albino Date: Tue, 2 Jul 2024 21:31:34 -0300 Subject: [PATCH] Some updates --- src/App.jsx | 54 +++++++++++++++++++++++++++++++++++++++------- src/App.module.css | 35 ++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 62d8a8b..8b8c5e8 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -13,9 +13,11 @@ import './global.css' export function App() { const [selectedOption, setSelectedOption] = useState(GREP_A_WORD); const [inputValue, setInputValue] = useState(''); - const [wordCount, setWordCount] = useState(''); + const [wordCount, setWordCount] = useState(-1); const [isCleanFormButtonDisabled, setIsCleanFormButtonDisabled] = useState(true); const [isSubmitButtonDisabled, setIsSubmitButtonDisabled] = useState(true); + const [foundWord, setFoundWord] = useState(''); + const [foundWords, setFoundWords] = useState([]); const inputRef = useRef(null); @@ -41,23 +43,41 @@ export function App() { const inputValueStr = inputValue.toLowerCase(); if (!wordCountObject.hasOwnProperty(inputValueStr)) { - setWordCount('no'); + setWordCount(0); return; } - setWordCount(wordCountObject[inputValueStr]); + setFoundWord(inputValueStr); + + const wordCount = wordCountObject[inputValueStr] + + setWordCount(wordCount); + + const foundWordObj = { + "word": inputValueStr, + "count": wordCount + } + + let index = foundWords.findIndex(word => wordCount > word.count); + if (index === -1) { + index = foundWords.length; + } + + const newArray = [...foundWords.slice(0, index), foundWordObj, ...foundWords.slice(index)]; + setFoundWords(newArray); } function handleCleanForm() { event.preventDefault(); setInputValue(''); - setWordCount(''); + setWordCount(-1); inputRef.current.focus(); } function onKeyUpInput() { const enterKeyCharCode = 13; + if (event.keyCode !== enterKeyCharCode) { return; } @@ -126,15 +146,33 @@ export function App() { } - { wordCount && + { foundWord && <>
-

There are

-

{wordCount}

-

occurrences of this word in my diary!

+

There are{' '}

+

{wordCount}

+

{' '}occurrences of the word{' "'}

+

{foundWord}

+

{'" '}in my diary!

} + +
+ { foundWords.length !== 0 && +

Found words

+ } + { foundWords.map(wordObj => { + return( + <> +
+ {wordObj.word} + {wordObj.count} +
+ + ) + }) } +
) diff --git a/src/App.module.css b/src/App.module.css index 08174b1..0e7eaae 100644 --- a/src/App.module.css +++ b/src/App.module.css @@ -113,6 +113,37 @@ font-size: 2rem; } -.result .wordCount { - +.result .highlight { + color: var(--green-300); + text-decoration: underline; + font-weight: bold; +} + +.foundWordsList { + display: flex; + flex-direction: column; + /* justify-content: center; */ + /* text-align: center; */ +} + +.foundWordsList h2 { + text-align: center; + margin-bottom: 1rem; +} + +.row { + display: flex; + align-items: center; + justify-content: space-between; + width: 24vw; + border: solid 1px; + padding: 0.5rem; +} + +.row span { + font-size: 1.25rem; +} + +.row span + span { + /* margin-top: 0.5rem; */ }