2021-04-21 01:57:18 +00:00
|
|
|
import React, {useState} from "react"
|
|
|
|
import Style from "./InputWithIcon.module.css"
|
|
|
|
import classNames from "classnames"
|
|
|
|
import make_icon from "../utils/make_icon"
|
|
|
|
|
|
|
|
|
2021-04-23 00:18:06 +00:00
|
|
|
/**
|
|
|
|
* Like an {@link Input}, but with an icon on the left side.
|
|
|
|
*
|
|
|
|
* Has a state which stores whether the inner input element is focused or not to change the style of the whole element
|
|
|
|
* on focus.
|
|
|
|
*
|
|
|
|
* @param icon - The FontAwesome IconDefinition of the icon that should be rendered in the input.
|
|
|
|
* @param className - Additional class(es) to be added to the outer container.
|
|
|
|
* @param props - Additional props to be passed to the outer container.
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
export default function InputWithIcon({ icon, className, ...props }) {
|
2021-04-21 01:57:18 +00:00
|
|
|
const [isFocused, setFocused] = useState(false);
|
|
|
|
|
|
|
|
return (
|
2021-04-23 00:18:06 +00:00
|
|
|
<div className={classNames(Style.InputWithIcon, isFocused ? Style.Focused : null, className)}>
|
|
|
|
<div className={Style.IconPart}>
|
2021-04-21 01:57:18 +00:00
|
|
|
{make_icon(icon)}
|
|
|
|
</div>
|
|
|
|
<input
|
2021-04-23 00:18:06 +00:00
|
|
|
className={Style.InputPart}
|
2021-04-21 01:57:18 +00:00
|
|
|
onFocus={
|
|
|
|
event => {
|
|
|
|
setFocused(true)
|
2021-04-23 00:18:06 +00:00
|
|
|
if(props.onFocus) {
|
|
|
|
props.onFocus(event)
|
2021-04-21 01:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onBlur={
|
|
|
|
event => {
|
|
|
|
setFocused(false)
|
2021-04-23 00:18:06 +00:00
|
|
|
if(props.onBlur) {
|
|
|
|
props.onBlur(event)
|
2021-04-21 01:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|