1.1.4 • Published 6 years ago

react-combo-search v1.1.4

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

react-combo-search

React component encapsulating the logic for filtering with select dropdown and text / dates. Comes with react-combo-select and react-datetime out of the box, but can take render props for select and date picker so you are not forced to use these two packages.

Usability

Peer dependencies: React and react-dom. Optional dependencies: react-combo-select, react-datetime *font-awesome(peer of react-combo-select) and moment(peer of react-datetime)

import { ComboSearch } from 'react-combo-search';

and include css file (you may include this in different way) *if you use react-combo-select and react-datetime you are gonna need their css also

require('../node_modules/react-combo-search/css/style.css');

Demo

We prepared code sandbox demo. Unfortunately, we haven't found the time to add some generic data for you to filter, but you can freely play around with component ot get the feel of what it does. See demo here

props/options

onSearch and selectData

There are two mandatory props: first one is "onSearch" which gets fired on submit. Second one is "selectData", which is a required prop in react-combo-select and populates select with options

import React, {Component} from 'react';
import ComboSearch from 'react-combo-search';
// You should not forget to include css

export default class FakeComponent extends Component {

	constructor(props) {
		super(props);

		this.searchCallback = this.searchCallback.bind(this);
	}

	searchCallback(data) {
		console.log(data);
		// you may do something with form data here
		// like call API by async action creator, do client side filtering
		// and transform the data first if you need it in slightly different format
	}

	render() {
		return (
			<div>
				<ComboSearch
					onSearch={this.searchCallback}
					selectData={['Name', 'Surname', 'Title', 'Date of birth']}
					datePickerCriteria='Date of birth'
				/>
			</div>
		);
	}
}

classNames

If you decide on using existing components, you may want to style them differently. You can also pass any prop to style these two in additionalSelectProps and additionalDatePickerProps, see next table.

PropertyDefaultDescription
classNames---Object containing classnames listed below
classNames.wrapperComboSearchComponent root element
classNames.datePickerRadioWrapperComboSearch__datePickerElement wrapping radio buttons and date picker input
classNames.radioGroupWrapperComboSearch__RadioWrapperElement wrapping radio buttons component
classNames.datePickerWrapperComboSearch__datePickerWrapperElement wrapping date picker component
classNames.textInputComboSearch__input InputBoxText input field
classNames.buttonButton Button--actionApply button, should you choose to use it
radioGroupClassNames---Object containing classnames for RadioGroup component listed below
radioGroupClassNames.wrapperRadioGroupComponent root element
radioGroupClassNames.labelRadioGroup__labelClass name for label
radioGroupClassNames.fakeRadioRadioGroup__fakeRadioOuter span that can act as a fake radio button
radioGroupClassNames.fakeRadioInnerRadioGroup__fakeRadioInnerInner span that can act as a part of a fake radio button
filterBarClassNames---Object containing classnames for FilterBar component listed below
filterBarClassNames.wrapperFilterBarComponent root element
filterBarClassNames.filterFilterBar__filterClass name for individual filter, black card wih filter info
filterBarClassNames.removeButtonFilterBar__filterCloseSpan tag that destroys filter on click
filterBarClassNames.textFilterBar__filterTextParagraph tag containing text inside the filter card

All props

PropertyTypeDefaultDescription
onSearchfunctionnoneCallback to invoke on filter apply, gets passed form data as only argument
selectDataarray of objectsnoneData for react-combo-select to populate select with options, with value and text props
datePickerCriteriastring or arraynoneOne or more of the options from selectData prop on which we want to open a date picker, either a string or an array of strings (value prop)
selectRenderFnfunctionnoneFunction that returns jsx for your custom select component. Since select is a controlled component this function will get called like selectRenderFn(selectData, selectedText, selectedValue, changeCallback, ...yourArguments), where returning component sets its options to selectData, text to selectedText(optional), value to selectedValue and onChange to changeCallback.
selectRenderFnArgsarraynoneArray of arguments that will get passed to selectRenderFn that you passed. Eg 1, 2, 3 will get spread as arguments
datePickerRenderFnfunctionnoneSimilar to selectRenderFn, will get called as datePickerRenderFn(changeCallback, ..yourArgs)
datePickerRenderFnArgsarraynoneI'll make sure they find their way to datePickerRenderFn
simpleVersionbooleanfalseIf true, filter bar will not show and instead of accumulating filters, every submit will be separate
showRadioButtonsbooleantrueIf false, the buttons will not render
hasButtonbooleanfalseShows "apply" button, and doesn't submit upon selecting date.
isInFetchingStatebooleanfalseIf true, component will go into state where submit, destroying filters and date picker is disabled and there is visual indication that some data is being filtered
selectDefaultValueobjectFirst item in selectData propoption that is preselected in react-combo-select on component render, with value nad text props
validationCallbackfunction(value) => { return value && value.length >= 3; }Function to run to validate text input, will get called with value as argument
inputErrorMessagestring"This field is required and should be at least 3 characters long"Message to display if validation fails
dateFormatstring"DD MMM YYYY"Prop to pass to date picker, takes any valid moment.js format
validDateFilterfunctionnoneFunction to validate which dates can be picked. See react-datetime docs for more info
additionalSelectPropsobjectnoneObject to spread on react-combo-select as props. Takes any props react-combo-select takes
additionalDatePickerPropsobjectnoneObject to spread on react-datetime as props. Takes any props react-datetime takes

Customizing and contributing

You can contact us in case you need some feature or want to contribute, but keep in mind we don't want to go overboard with trying to make this component a "swiss knife". It's possible that you may need additional className props, we haven't fully tested custom styling. Feel free to contact us should that be a case. You can also freely fork and play around with the project.

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago