Skip to content

Instantly share code, notes, and snippets.

@diegomais
Created April 15, 2021 21:00
Show Gist options
  • Save diegomais/af02a1bf3b9a0441d07148dfbf0844f7 to your computer and use it in GitHub Desktop.
Save diegomais/af02a1bf3b9a0441d07148dfbf0844f7 to your computer and use it in GitHub Desktop.
SearchBar with onChangeDebounce
import React from 'react';
import PropTypes from 'prop-types';
let timeout;
const SearchBar = ({placeholder, buttonLabel, inputDelay, onChange, onButtonClick}) => {
const onChangeDebounce = (evt) => {
timeout && clearTimeout(timeout);
const target = evt.target;
timeout = setTimeout(() => onChange({target}), inputDelay);
};
return (
<div className="row mb-3">
<div className='col-md-9'>
<div className="form-group mb-2">
<label htmlFor="search-input" className="sr-only">{placeholder}</label>
<input
className="form-control form-control-lg"
id="search-input"
placeholder={placeholder}
onChange={onChangeDebounce}
/>
</div>
</div>
<div className='col-md-3'>
<button
className="btn btn-lg btn-block btn-primary mb-2 text-nowrap"
onClick={onButtonClick}
>
{buttonLabel}
</button>
</div>
</div>
);
};
SearchBar.propTypes = {
placeholder: PropTypes.string,
buttonLabel: PropTypes.string,
inputDelay: PropTypes.number, // milliseconds
onChange: PropTypes.func,
onButtonClick: PropTypes.func
};
SearchBar.defaultProps = {
placeholder: 'Pesquise...',
buttonLabel: 'Botão',
inputDelay: 200,
onChange: () => {
},
onButtonClick: () => {
}
}
export default SearchBar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment