Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Created September 14, 2018 14:25
Show Gist options
  • Save tlaitinen/528a710d2ccfae9c7ca95a70999d94b3 to your computer and use it in GitHub Desktop.
Save tlaitinen/528a710d2ccfae9c7ca95a70999d94b3 to your computer and use it in GitHub Desktop.
import React from 'react';
import {
typedConnect,
createPropsMapper,
PropsOf
} from 'react-redux-typed-connect';
import SearchBar from '../components/search-bar';
import {Selectors, Actions} from '../actions/crud';
import * as qn from '../actions/crud-query-names';
export interface CreateCrudSearchBar<C,E,Q,RS,EI> {
selectors: Selectors<E,EI,Q,RS>;
actions: Actions<C,E,Q,EI>;
emptyQuery: Q;
queryText: (query:Q) => string;
setQueryText: (query:Q, text:string) => Q;
}
export function createCrudSearchBar<C,E,Q,RS,EI>(options:CreateCrudSearchBar<C,E,Q,RS,EI>) {
const CrudSearchBar = (props:PropsOf<typeof propsMapper>) => (
<SearchBar
placeholder={props.placeholder}
value={options.queryText(props.query || options.emptyQuery)}
onChange={text => {
props.setQueryText(props.query || options.emptyQuery, text);
}}/>
);
const propsMapper = createPropsMapper({
fromState:(state:RS, ownProps:{
queryName?:string;
placeholder?:string;
}) => ({
query: options.selectors.query(state, ownProps.queryName || qn.def)
}),
actions: (ownProps) => ({
setQueryText: (query:Q, text:string) => options.actions.fetchResultsLater(
ownProps.queryName || qn.def,
options.setQueryText(query, text),
500
)
})
})
return typedConnect(propsMapper)(CrudSearchBar);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment