SearchBar Funcional
This commit is contained in:
@@ -2,7 +2,7 @@ import React, {Fragment, useEffect, useState} from "react";
|
||||
import queryString from "query-string";
|
||||
|
||||
import {searchArtist, searchDisc, searchSong} from "../services/search_service";
|
||||
import SearchBar from "./SearchBar";
|
||||
import {SearchBar} from "./SearchBar";
|
||||
import {Paginate} from "./Paginate";
|
||||
import {Link} from "react-router-dom";
|
||||
import {Tab, TabList, TabPanel, Tabs} from "react-tabs";
|
||||
|
||||
@@ -1,33 +1,22 @@
|
||||
import React from "react";
|
||||
import React, {useState} from "react";
|
||||
import {FaSearch} from 'react-icons/fa';
|
||||
|
||||
export default class SearchBar extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
query: this.props.query ? this.props.query : ''
|
||||
};
|
||||
}
|
||||
export const SearchBar = (props) => {
|
||||
const [query, setQuery] = useState(props.query ? props.query : '')
|
||||
const onQueryChanged = props.onQueryChanged;
|
||||
|
||||
handleChange = event => {
|
||||
const query = event.target.value;
|
||||
this.setState({query: query});
|
||||
};
|
||||
const handleChange = event => setQuery(event.target.value);
|
||||
|
||||
handleSubmit = (who) => (event) => {
|
||||
const query = this.state.query;
|
||||
if (!query) return;
|
||||
|
||||
if (event.key === 'Enter' || who === 'button') {
|
||||
const onQueryChanged = this.props.onQueryChanged;
|
||||
if (onQueryChanged) onQueryChanged(query)
|
||||
const handleSubmit = (who) => (event) => {
|
||||
if ((query && onQueryChanged) && (event.key === 'Enter' || who === 'button')) {
|
||||
onQueryChanged(query)
|
||||
}
|
||||
};
|
||||
|
||||
render = () => (
|
||||
return (
|
||||
<div className='input-with-icon'>
|
||||
<input className='full-width' value={this.state.query} onKeyUp={this.handleSubmit('input')} onChange={this.handleChange}/>
|
||||
<button onClick={this.handleSubmit('button')}><FaSearch/></button>
|
||||
<input className='full-width' value={query} onKeyUp={handleSubmit('input')} onChange={handleChange}/>
|
||||
<button onClick={handleSubmit('button')}><FaSearch/></button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user