-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,80 @@ | ||
import React, { useState } from 'react' | ||
import React, { useState, ForwardedRef, forwardRef } from 'react' | ||
|
||
import cn from 'classnames' | ||
|
||
import { Icon } from './Icon' | ||
|
||
type Props = { | ||
searchable?: boolean | ||
ref?: ForwardedRef<HTMLInputElement> | ||
className?: string | ||
} & React.ComponentPropsWithoutRef<'input'> | ||
|
||
export const Input: React.FC<Props> = ({ | ||
searchable = false, | ||
onFocus, | ||
onBlur, | ||
className, | ||
...rest | ||
}) => { | ||
const [isFocused, setIsFocused] = useState(false) | ||
export const Input: React.FC<Props> = forwardRef( | ||
( | ||
{ searchable = false, onFocus, onBlur, className, ...rest }, | ||
ref: ForwardedRef<HTMLInputElement>, | ||
) => { | ||
const [isFocused, setIsFocused] = useState(false) | ||
const [isInputEmpty, setIsInputEmpty] = useState(true) | ||
|
||
const handleFocus = (e: any) => { | ||
setIsFocused(true) | ||
if (onFocus) { | ||
onFocus(e) | ||
const handleFocus = (e: any) => { | ||
setIsFocused(true) | ||
if (onFocus) { | ||
onFocus(e) | ||
} | ||
} | ||
} | ||
|
||
const handleBlur = (e: any) => { | ||
setIsFocused(false) | ||
if (onBlur && e) { | ||
onBlur(e) | ||
const handleBlur = (e: any) => { | ||
setIsFocused(false) | ||
if (onBlur && e) { | ||
onBlur(e) | ||
} | ||
} | ||
} | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
'flex items-center rounded px-3 py-2', | ||
{ | ||
shadow: isFocused, | ||
}, | ||
className, | ||
)} | ||
> | ||
<input | ||
onFocus={handleFocus} | ||
onBlur={handleBlur} | ||
className="w-full outline-none bg-transparent dark:placeholder-black-400 text-sm" | ||
{...rest} | ||
/> | ||
{searchable && ( | ||
<Icon | ||
name="search-line" | ||
className={cn( | ||
'ml-2', | ||
isFocused | ||
? 'text-gray-400 dark:text-gray-300' | ||
: 'text-gray-300 dark:text-gray-400', | ||
)} | ||
const handleInput = (e: any) => { | ||
if (e.target.value === '') { | ||
setIsInputEmpty(true) | ||
} else { | ||
setIsInputEmpty(false) | ||
} | ||
} | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
'flex items-center rounded px-3 py-2 text-sm relative', | ||
{ | ||
shadow: isFocused, | ||
}, | ||
className, | ||
)} | ||
> | ||
<input | ||
ref={ref} | ||
onFocus={handleFocus} | ||
onBlur={handleBlur} | ||
onInput={handleInput} | ||
className="w-full outline-none bg-transparent dark:placeholder-black-400" | ||
{...rest} | ||
/> | ||
)} | ||
</div> | ||
) | ||
} | ||
{searchable && ( | ||
<> | ||
{isInputEmpty && ( | ||
<span className="text-black-400 absolute right-8">Alt+K</span> | ||
)} | ||
<Icon | ||
name="search-line" | ||
className={cn( | ||
'ml-2', | ||
isFocused | ||
? 'text-gray-400 dark:text-gray-300' | ||
: 'text-gray-300 dark:text-gray-400', | ||
)} | ||
/> | ||
</> | ||
)} | ||
</div> | ||
) | ||
}, | ||
) |
e8f9816
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
evm-codes – ./
evm-codes.vercel.app
evm-codes-smlxl.vercel.app
www.evm.codes
evm-codes-git-main-smlxl.vercel.app
evm.codes