learn-morse-code/src/components/Legend.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

import React, { useContext } from "react"
2020-01-14 11:37:04 +01:00
import morseCode from '../data/morse-code.json'
import useMorsePlayer from "../hooks/useMorsePlayer";
import { WPMContext } from "../contexts/wpmContext.js";
2020-01-14 11:37:04 +01:00
function Legend() {
const { playMorseWord } = useMorsePlayer()
function handleClick(e) {
e.preventDefault()
2020-02-06 11:43:21 +01:00
let word = e.target.innerText
// let newWord = word
if (e.target.className === 'alpha') {
word = convertWordToMorse(word)
}
if (e.target.id === 'test') {
word = convertWordToMorse(e.target.innerText)
}
console.log(word);
playMorseWord(word)
2020-01-14 11:37:04 +01:00
}
2020-01-15 09:41:40 +01:00
2020-01-14 11:37:04 +01:00
function convertWordToMorse(word) {
let morse = ''
for (let i in word) {
morse += morseCode[word[i].toLowerCase()]
morse += ' '
}
return morse
}
2020-01-15 09:41:40 +01:00
const legend = Object.keys(morseCode).map((morse, index) =>
2020-02-06 11:43:21 +01:00
<button key={"legend_item_"+index} className="item" onClick={handleClick}>
<span className="alpha" key={"legend_btn_"+index}>{morse.toUpperCase()}</span>
<span className="morse" key={"legend_spn_"+index}>{morseCode[morse]}</span>
</button>
2020-01-15 09:41:40 +01:00
)
2020-01-14 11:37:04 +01:00
return (
<div id="legend">
2020-01-31 10:45:12 +01:00
<div id="title">
2020-02-06 11:43:21 +01:00
<h1>Morse Code</h1>
2020-01-31 10:45:12 +01:00
</div>
<div id="legend-items">
{legend}
<div>
<button id="test" onClick={handleClick}>Anya</button>
<button id="test" onClick={handleClick}>Alexandra</button>
<button id="test" onClick={handleClick}>Paris</button>
</div>
</div>
2020-01-14 11:37:04 +01:00
</div>
)
}
export default React.memo(Legend)