Skip to content

Instantly share code, notes, and snippets.

@SeanRoberts
Last active January 9, 2018 14:50
Show Gist options
  • Save SeanRoberts/f485f56197fd8712dad78edcdb61d89c to your computer and use it in GitHub Desktop.
Save SeanRoberts/f485f56197fd8712dad78edcdb61d89c to your computer and use it in GitHub Desktop.
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 });
}
}
<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