Advertisement
STANAANDREY

dictionary

Aug 10th, 2021
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import './App.css';
  3. import CSS from 'csstype';
  4. let words = new Set<string>();
  5.  
  6. export default function App() {
  7.   let textInput = React.createRef<HTMLInputElement>();
  8.   let srcInput = React.createRef<HTMLInputElement>();
  9.   let [srcStyle, setSrcStyle] = React.useState<CSS.Properties>({
  10.     backgroundColor: 'white'
  11.   });
  12.   const [message, setMessage] = React.useState<string>('');
  13.   const [submitDisabled, setsubmitDisabled] = React.useState<boolean>(true);
  14.  
  15.   const updateSrcZone = () => {
  16.     const { value: word } = srcInput.current!;
  17.     if (words.has(word)) {
  18.       setSrcStyle({
  19.         backgroundColor: 'green'
  20.       });
  21.       setMessage(`Word "${word}" exists!`);
  22.     } else {
  23.       setSrcStyle({
  24.         backgroundColor: 'red'
  25.       })
  26.       setMessage(`Word "${word}" doesn't exists!`);
  27.    }
  28.  }
  29.  
  30.  const handleClick = (event: React.MouseEvent<HTMLButtonElement>): void => {
  31.    event.preventDefault();
  32.    words.add(textInput.current!.value);
  33.    if (srcInput.current?.value) {
  34.      updateSrcZone();
  35.    }
  36.    textInput.current!.value = '';
  37.    setsubmitDisabled(true);
  38.  }
  39.  
  40.  const handleSrcChange = (): void => {
  41.    updateSrcZone();
  42.  }
  43.  
  44.  return (
  45.    <div className="App">
  46.      <h1>DICTIONARY</h1>
  47.      <br />
  48.      <input
  49.        type="text"
  50.        ref={textInput}
  51.        onChange={() => setsubmitDisabled(Boolean(!textInput.current?.value.length))}
  52.      />
  53.      <button onClick={handleClick} disabled={submitDisabled}>submit</button>
  54.      <br /><br /><br />
  55.      <p>type a word to search for:</p>
  56.      <input
  57.        type="text" style={srcStyle}
  58.        className="search-bar"
  59.        ref={srcInput}
  60.        onChange={handleSrcChange}
  61.      />
  62.      <br />
  63.      <i className="message"> {message}</i>
  64.      <br />
  65.    </div>
  66.  );
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement