import React from 'react'; import { FormControl, InputGroup, ListGroup } from 'react-bootstrap'; import { deepClone } from '../../../lib/helpers/objectMethods'; import { ISelection } from './interfaces/ISelection'; import { ISelectionItem } from './interfaces/ISelectionItem'; export interface SelectionProps { selection: ISelection; onItemSelected?: (item: T) => void; allowUserSelect?: boolean; } export interface LayoutState { selected: string | number; search: string; } class Selection extends React.Component, LayoutState> { private _currentId = 0; _generateId(){ const id = this._currentId; this._currentId += 1; return id; } constructor(props) { super(props); this.state = { search: '', selected: null } } /** * Function to Filter the Selection. * @param search The Search Term. */ protected _filter(search: string) { this.setState({ search: search.toLocaleLowerCase(), selected: this.state.selected }); } /** * Function used to Select an item * @param item which will be selected */ protected _select(item: ISelectionItem) { console.log('select item',item) this.setState({ search: this.state.search, selected: this.props.allowUserSelect ? item.id : -1 }); if (this.props.allowUserSelect && typeof this.props.onItemSelected === "function") { this.props.onItemSelected(item.template); } } public render() { const _this = this; // Filter only Valid items. // Therefore iterate over the Groups and // Filter the Items with the Search String // and there Labels / Keywords. // Afterwards filter the groups and remove // Empty Groups const items = deepClone(this.props).selection.map(item => { if ((item as { groupName: string, items: ISelectionItem[] }).items !== undefined) { // Filter the Items, that match the Selection (item as { groupName: string, items: ISelectionItem[] }).items = (item as { groupName: string, items: ISelectionItem[] }).items.map(item => { item.id = _this._generateId(); return item; }).filter(value => // Filter the Keywords value.keywords.toLowerCase().includes(_this.state.search) || // Filter the Lable. value.label.toLowerCase().includes(_this.state.search) ); } else { (item as any).id = _this._generateId(); } // Return the Item return item; }).filter( item => { // Filter the Element by the lenght of the contained elements. if ((item as { groupName: string, items: ISelectionItem[] }).items !== undefined) { // Filter the Keywords return (item as { groupName: string, items: ISelectionItem[] }).items.length > 0; } else { // Filter the Keywords return (item as ISelectionItem).keywords.toLowerCase().includes(_this.state.search) || // Filter the Lable. (item as ISelectionItem).label.toLowerCase().includes(_this.state.search) } } ); let idx = 0; return (<> {/* A Search Field. */} {this.props.allowUserSelect ? Search { _this._filter(event.currentTarget.value); }} /> : '' } { items.length > 0 ? items.map(item => { idx++; if ((item as { groupName: string, items: ISelectionItem[] }).items !== undefined) { return (

{(item as { groupName: string, items: ISelectionItem[] }).groupName}
{(item as { groupName: string, items: ISelectionItem[] }).items.map(template => { // Raise the ID idx++; // Store the Item Index const idxOfItem = idx; // Based on the selected Item assign the Color // dark = selected // light = unselected const variant = _this.props.allowUserSelect ? (template.id === _this.state.selected ? "dark" : "light") : template.color || 'light'; // Return the Template variant={variant} return ( { _this._select(template) }}>{template.label} ); })}
); } else { // Store the Item Index const idxOfItem = idx; // Based on the selected Item assign the Color // dark = selected // light = unselected const variant = _this.props.allowUserSelect ? ((item as ISelectionItem).id === _this.state.selected ? "dark" : "light") : (item as ISelectionItem).color || 'light'; // Return the Template variant={variant} return ( { _this._select(item as ISelectionItem) }}>{(item as ISelectionItem).label} ) } }) : Nothing Found } ); } } export default Selection;