import React from 'react'; import _ from 'lodash'; import PropTypes from 'prop-types'; import AssociationPickerResults from './AssociationPickerResults'; class AssociationPickerWindow extends React.Component { constructor(props) { super(props); this.state = { ...props, selectedItems: [] }; this.selectItem = this.selectItem.bind(this); } selectItem(item) { const operation = this.updateSelectedItems(item); if ('onSelectItem' in this.props) { this.props.onSelectItem(item, operation); } } updateSelectedItems(item) { let operation, newItems; if (item._selected) { newItems = _.filter(this.state.selectedItems, r => r.id !== item.id); operation = 'removed'; } else { newItems = [...this.state.selectedItems, item]; operation = 'added'; } this.setState(...this.state, { selectedItems: newItems }); return operation; } render() { const { q, results } = this.props; return (

Select item

{this.props.pagination()} {this.props.allowMultiple ? ( ) : null}
); } } AssociationPickerWindow.propTypes = { onSelectItem: PropTypes.func, q: PropTypes.string, results: PropTypes.array, onFilterChange: PropTypes.func, allowCreate: PropTypes.bool, pagination: PropTypes.func, allowMultiple: PropTypes.bool, onClickDone: PropTypes.func, }; export default AssociationPickerWindow;