Last active
January 9, 2018 14:50
-
-
Save SeanRoberts/f485f56197fd8712dad78edcdb61d89c to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import { debounce } from 'lodash'; | |
import PropTypes from 'prop-types'; | |
export default class Filters extends React.Component { | |
static propTypes = { | |
initialFilters: PropTypes.objectOf(PropTypes.string), | |
render: PropTypes.func.isRequired, | |
afterChange: PropTypes.func, | |
}; | |
static defaultProps = { | |
initialFilters: {}, | |
afterChange: () => {}, | |
}; | |
constructor(props) { | |
super(props); | |
this.state = { filters: props.initialFilters }; | |
this.debouncedAfterChange = debounce(this.props.afterChange, 500); | |
} | |
onChange = (name, value) => { | |
this.setState( | |
state => ({ filters: { ...state.filters, [name]: value } }), | |
() => this.debouncedAfterChange(this.state.filters), | |
); | |
}; | |
render() { | |
const { filters } = this.state; | |
const { onChange } = this; | |
return this.props.render({ filters, onChange }); | |
} | |
} |
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
<Filters | |
initialFilters={{ keyword: '' }} | |
afterChange={this.props.onFetchProducts} | |
render={({ filters, onChange }) => ( | |
<div className="Form__Row"> | |
<Field | |
label="Search Services" | |
placeholder="e.g. 'Social Media' or 'SEO'" | |
value={filters.keyword} | |
onChange={e => onChange('keyword', e.target.value)} | |
name="search-projects" | |
/> | |
</div> | |
)} | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment