|
| 1 | +import { ClassProps } from '@fiori-for-react/core/types/ClassProps'; |
| 2 | +import React, { PureComponent, ReactNode } from 'react'; |
| 3 | +import styles from './FilterItem.jss'; |
| 4 | +import { Label } from '../../webComponents/Label'; |
| 5 | +import { Input } from '../../webComponents/Input'; |
| 6 | +import { Select } from '../../webComponents/Select'; |
| 7 | +import { StandardListItem } from '../../webComponents/StandardListItem'; |
| 8 | +import { BusyIndicator } from '../BusyIndicator'; |
| 9 | +import { FilterType } from '../../enums/FilterType'; |
| 10 | +import { Event, StyleClassHelper } from '@fiori-for-react/utils'; |
| 11 | +import { withStyles } from '@fiori-for-react/core/utils/withStyles'; |
| 12 | +import { CommonProps } from '@fiori-for-react/core/interfaces'; |
| 13 | +import { ListItemTypes } from '../..'; |
| 14 | + |
| 15 | +export interface FilterItemPropTypes extends CommonProps { |
| 16 | + placeholder?: string; |
| 17 | + renderText?: (item?: any) => JSX.Element; |
| 18 | + type?: FilterType; |
| 19 | + label?: string; |
| 20 | + filterItems?: any[]; |
| 21 | + onChange?: (event: Event) => void; |
| 22 | + loading?: boolean; |
| 23 | + children?: ReactNode; |
| 24 | + valueParamName?: string; |
| 25 | + changeEventName?: string; |
| 26 | +} |
| 27 | + |
| 28 | +interface FilterItemInternalProps extends FilterItemPropTypes, ClassProps {} |
| 29 | + |
| 30 | +@withStyles(styles) |
| 31 | +export class FilterItem extends PureComponent<FilterItemPropTypes> { |
| 32 | + state = { |
| 33 | + selectedText: '' |
| 34 | + }; |
| 35 | + |
| 36 | + static defaultProps = { |
| 37 | + placeholder: '', |
| 38 | + renderText: (item) => (item && item.text) || '', |
| 39 | + type: FilterType.Default, |
| 40 | + filterItems: [], |
| 41 | + label: '', |
| 42 | + onChange: () => null, |
| 43 | + loading: false |
| 44 | + }; |
| 45 | + |
| 46 | + private onSelect = (e) => { |
| 47 | + const selectedKey = e.getParameter('selectedItem').getAttribute('data-key'); |
| 48 | + const { filterItems } = this.props; |
| 49 | + const selectedItem = this._getItemByKey(selectedKey, filterItems) || filterItems[0]; |
| 50 | + this.setState({ selectedItem }); |
| 51 | + this.props.onChange(Event.of(this, e.getOriginalEvent(), { selectedItem })); |
| 52 | + }; |
| 53 | + |
| 54 | + _getItemByKey(key, items) { |
| 55 | + return items.filter((item) => item.key === key)[0]; |
| 56 | + } |
| 57 | + |
| 58 | + private getFilterComponent = () => { |
| 59 | + const { type, filterItems, placeholder, children, loading } = this.props as FilterItemInternalProps; |
| 60 | + switch (type) { |
| 61 | + case FilterType.Default: |
| 62 | + return <Input placeholder={placeholder} onChange={this.onSelect} style={{ width: '100%' }} />; |
| 63 | + case FilterType.Select: |
| 64 | + if (loading) { |
| 65 | + return <BusyIndicator />; |
| 66 | + } |
| 67 | + return ( |
| 68 | + <Select onChange={this.onSelect} style={{ width: '100%' }}> |
| 69 | + {filterItems.map((item) => ( |
| 70 | + <StandardListItem type={ListItemTypes.Active} key={item.key} data-key={item.key}> |
| 71 | + {item.text} |
| 72 | + </StandardListItem> |
| 73 | + ))} |
| 74 | + </Select> |
| 75 | + ); |
| 76 | + case FilterType.Custom: |
| 77 | + if (loading) { |
| 78 | + return <BusyIndicator />; |
| 79 | + } |
| 80 | + return ( |
| 81 | + <div> |
| 82 | + {React.Children.map(children, (child) => { |
| 83 | + return React.cloneElement(child as React.ReactElement<any>, { |
| 84 | + [this.props.changeEventName]: this.onSelect, |
| 85 | + valueParameter: this.props.valueParamName, |
| 86 | + style: { width: '100%' } |
| 87 | + }); |
| 88 | + })} |
| 89 | + </div> |
| 90 | + ); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + render() { |
| 95 | + const { label, classes, style, tooltip } = this.props as FilterItemInternalProps; |
| 96 | + const filterItemClasses = StyleClassHelper.of(classes.filterItem); |
| 97 | + |
| 98 | + return ( |
| 99 | + <div className={filterItemClasses.toString()} style={style} title={tooltip}> |
| 100 | + <Label>{label}</Label> |
| 101 | + {this.getFilterComponent()} |
| 102 | + </div> |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments