import * as React from 'react'; import { themr, ThemedComponentClass } from '@friendsofreactjs/react-css-themr'; import Label from '../Label'; import Accordion from '../Accordion'; import { ItemType } from './ComboBox'; import { COMBOBOX } from '../ThemeIdentifiers'; import * as baseTheme from './ComboBox.scss'; import Icon from '../Icon'; import { Table } from '../Table'; import { classNames } from '@shopify/react-utilities/styles'; export interface ComboItemProps { type?: ItemType; key?: string; value: any; column?: any; renderer?(value: any, type?: string): React.ReactElement; } export interface AccordianItem { header: React.ReactElement; children: React.ReactElement; } export interface Props { item: ComboItemProps; serverSort?: any; clickHandler?(value: string | null | boolean, key?: string): void; theme?: any; activeIndex: number; } class ComboBoxItem extends React.PureComponent { render() { const { serverSort, item, theme, } = this.props; const { key, type, value, renderer, column: columnConfig } = item; switch (type) { case 'Accordian' : const accordianItems: AccordianItem[] = []; value.forEach((val: any, index: number) => { const headerKey = key; const indexStr: string = index.toString(); if (val.children.length === 0) { return null; } accordianItems.push({ header: renderer ? renderer(val, 'header') : , children: val.children.map((child: any, index: number) => { const data = headerKey ? child[headerKey] : (key ? child[key] : child); return (
{renderer ? renderer(child, 'children') : data}
); }) }); }); return (
); case 'Tabular': return (
); default: return (
{this.getItem(value, key, renderer)}
); } } // get the parent of the clicked element, to fetch the selected value private findParent(dom: HTMLElement | null): boolean | string | null { if (dom && dom.getAttribute('data-value')) { return dom.getAttribute('data-value'); } if (dom && dom.getAttribute('data-isparent')) { return false; } return dom ? this.findParent(dom.parentElement) : false; } // Get fetching key which get the field key private getDataKey(dom: HTMLElement | null, event: any): boolean | string | null { if (dom && dom.getAttribute('data-key')) { return dom.getAttribute('data-key'); } if (event.currentTarget.getAttribute('data-key')) { return event.currentTarget.getAttribute('data-key'); } return dom ? this.getDataKey(dom.parentElement, event) : false; } private handleClick = (event: any) => { const target = event.target; const dataValue: boolean | string | any = this.findParent(target); if (dataValue && this.props.clickHandler) { const hasKey = this.getDataKey(target, event); const valueToPass = hasKey ? JSON.parse(dataValue) : dataValue; console.log(valueToPass, hasKey); this.props.clickHandler(valueToPass, hasKey as string); } } private handleRowClick = (data: any) => { const dataId: number = data; const { item : { value, key } } = this.props; const findIndex = value.findIndex((i: any) => i.id === dataId); if (findIndex !== -1 && this.props.clickHandler) { console.log(findIndex); this.props.clickHandler(value[findIndex], key as string); } } private getItem = (value: any, key: string | undefined, renderer: any) => { const { theme, activeIndex } = this.props; if (value.length === 0) { return (
No Data Found
); } return value.map((val: any, index: number) => { const data = key ? val[key] : val; const classes = classNames(theme.comboboxItem, activeIndex === index && theme.active); return (
{renderer ? renderer(val) : data}
); }); } } export default themr(COMBOBOX, baseTheme)(ComboBoxItem) as ThemedComponentClass;