Skip to content

Instantly share code, notes, and snippets.

@d88naimi
Created July 22, 2021 00:22
Show Gist options
  • Save d88naimi/5f7d63d29ec41e555d6a53ebf9f4e8ce to your computer and use it in GitHub Desktop.
Save d88naimi/5f7d63d29ec41e555d6a53ebf9f4e8ce to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import cx from "classnames";
import omit from "lodash/omit";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faSearch } from "@fortawesome/free-solid-svg-icons";
import { Input } from "../Input";
import styles from "./Search.less";
class Search extends Component {
state = {
term: String(),
};
componentDidMount() {
this.setState({
term: this.props.value,
});
}
onChange = (evt) => {
this.setState(
{
term: evt.target.value,
},
() => {
if (this.props.onChange) {
this.props.onChange(
this.state.term,
this.props.name,
this.props.datatype
);
}
}
);
};
onSubmit = () => {
if (this.props.onSubmit) {
this.props.onSubmit(
this.state.term,
this.props.name,
this.props.datatype
);
}
};
onKeyUp = () => {
if (this.props.onKeyUp) {
this.props.onKeyUp(this.state.term, this.props.name, this.props.datatype);
}
};
render() {
return (
<div className={cx(styles.Search, this.props.className)}>
<Input
{...omit(this.props, "innerRef")}
ref={this.props.innerRef}
type="search"
className={styles.Input}
onChange={this.onChange}
/>
<FontAwesomeIcon className={styles.RoundedSearchIcon} icon={faSearch} />
</div>
);
}
}
const SearchRef = React.forwardRef((props, ref) => (
<Search innerRef={ref} {...props} />
));
export { SearchRef as Search };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment