/
App.js
58 lines (53 loc) · 1.88 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { Profiler, useEffect, useRef, useState } from 'react';
import MaterialIcon from '@material/react-material-icon';
import TextField, { Input } from '@material/react-text-field';
import { debounce } from 'throttle-debounce';
import '@material/react-material-icon/dist/material-icon.min.css';
import '@material/react-text-field/dist/text-field.min.css';
import { getProfileData, logProfileData } from '../profiler';
import Emojis from '../Emojis';
import styles from './App.module.css';
import './App.css';
const DEBOUNCE_MS_SEARCH = 400;
export default () => {
const [emojiInputValue, setEmojiInputValue] = useState('');
const [emojiSearchName, setEmojiSearchName] = useState('');
// we use `useRef` so that we don't redefine the function on every update
// and in turn erase the debounce functionality.
const setDebouncedEmojiSearchName = useRef(
debounce(DEBOUNCE_MS_SEARCH, setEmojiSearchName)
);
// set the search term using `debounce` which introduces an intentional dely
// - hence the side-effect.
useEffect(() => {
setDebouncedEmojiSearchName.current(emojiInputValue);
}, [emojiInputValue]);
return (
<div className={styles.root}>
<TextField
className={styles.input}
label="Emoji Name"
trailingIcon={<MaterialIcon icon="search" />}
>
<Input
value={emojiInputValue}
onChange={event => setEmojiInputValue(event.currentTarget.value)}
spellCheck="false"
type="text"
id="emoji"
data-test-id="emojiSearchInput"
isValid
/>
</TextField>
<h2 className={styles.term}>{emojiInputValue || 'Search'}</h2>
<Profiler
id="Emojis"
onRender={(...profileData) =>
logProfileData(getProfileData(profileData))
}
>
<Emojis emojiSearchName={emojiSearchName} />
</Profiler>
</div>
);
};