|
1 | | -import React, { useState } from 'react' |
| 1 | +import React, { useState, ForwardedRef, forwardRef } from 'react' |
2 | 2 |
|
3 | 3 | import cn from 'classnames' |
4 | 4 |
|
5 | 5 | import { Icon } from './Icon' |
6 | 6 |
|
7 | 7 | type Props = { |
8 | 8 | searchable?: boolean |
| 9 | + ref?: ForwardedRef<HTMLInputElement> |
9 | 10 | className?: string |
10 | 11 | } & React.ComponentPropsWithoutRef<'input'> |
11 | 12 |
|
12 | | -export const Input: React.FC<Props> = ({ |
13 | | - searchable = false, |
14 | | - onFocus, |
15 | | - onBlur, |
16 | | - className, |
17 | | - ...rest |
18 | | -}) => { |
19 | | - const [isFocused, setIsFocused] = useState(false) |
| 13 | +export const Input: React.FC<Props> = forwardRef( |
| 14 | + ( |
| 15 | + { searchable = false, onFocus, onBlur, className, ...rest }, |
| 16 | + ref: ForwardedRef<HTMLInputElement>, |
| 17 | + ) => { |
| 18 | + const [isFocused, setIsFocused] = useState(false) |
| 19 | + const [isInputEmpty, setIsInputEmpty] = useState(true) |
20 | 20 |
|
21 | | - const handleFocus = (e: any) => { |
22 | | - setIsFocused(true) |
23 | | - if (onFocus) { |
24 | | - onFocus(e) |
| 21 | + const handleFocus = (e: any) => { |
| 22 | + setIsFocused(true) |
| 23 | + if (onFocus) { |
| 24 | + onFocus(e) |
| 25 | + } |
25 | 26 | } |
26 | | - } |
27 | 27 |
|
28 | | - const handleBlur = (e: any) => { |
29 | | - setIsFocused(false) |
30 | | - if (onBlur && e) { |
31 | | - onBlur(e) |
| 28 | + const handleBlur = (e: any) => { |
| 29 | + setIsFocused(false) |
| 30 | + if (onBlur && e) { |
| 31 | + onBlur(e) |
| 32 | + } |
32 | 33 | } |
33 | | - } |
34 | 34 |
|
35 | | - return ( |
36 | | - <div |
37 | | - className={cn( |
38 | | - 'flex items-center rounded px-3 py-2', |
39 | | - { |
40 | | - shadow: isFocused, |
41 | | - }, |
42 | | - className, |
43 | | - )} |
44 | | - > |
45 | | - <input |
46 | | - onFocus={handleFocus} |
47 | | - onBlur={handleBlur} |
48 | | - className="w-full outline-none bg-transparent dark:placeholder-black-400 text-sm" |
49 | | - {...rest} |
50 | | - /> |
51 | | - {searchable && ( |
52 | | - <Icon |
53 | | - name="search-line" |
54 | | - className={cn( |
55 | | - 'ml-2', |
56 | | - isFocused |
57 | | - ? 'text-gray-400 dark:text-gray-300' |
58 | | - : 'text-gray-300 dark:text-gray-400', |
59 | | - )} |
| 35 | + const handleInput = (e: any) => { |
| 36 | + if (e.target.value === '') { |
| 37 | + setIsInputEmpty(true) |
| 38 | + } else { |
| 39 | + setIsInputEmpty(false) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return ( |
| 44 | + <div |
| 45 | + className={cn( |
| 46 | + 'flex items-center rounded px-3 py-2 text-sm relative', |
| 47 | + { |
| 48 | + shadow: isFocused, |
| 49 | + }, |
| 50 | + className, |
| 51 | + )} |
| 52 | + > |
| 53 | + <input |
| 54 | + ref={ref} |
| 55 | + onFocus={handleFocus} |
| 56 | + onBlur={handleBlur} |
| 57 | + onInput={handleInput} |
| 58 | + className="w-full outline-none bg-transparent dark:placeholder-black-400" |
| 59 | + {...rest} |
60 | 60 | /> |
61 | | - )} |
62 | | - </div> |
63 | | - ) |
64 | | -} |
| 61 | + {searchable && ( |
| 62 | + <> |
| 63 | + {isInputEmpty && ( |
| 64 | + <span className="text-black-400 absolute right-8">Alt+K</span> |
| 65 | + )} |
| 66 | + <Icon |
| 67 | + name="search-line" |
| 68 | + className={cn( |
| 69 | + 'ml-2', |
| 70 | + isFocused |
| 71 | + ? 'text-gray-400 dark:text-gray-300' |
| 72 | + : 'text-gray-300 dark:text-gray-400', |
| 73 | + )} |
| 74 | + /> |
| 75 | + </> |
| 76 | + )} |
| 77 | + </div> |
| 78 | + ) |
| 79 | + }, |
| 80 | +) |
0 commit comments