|
| 1 | +import { |
| 2 | + Chip, |
| 3 | + ChipGroup, |
| 4 | + PickOptional, |
| 5 | + ToolbarChip, |
| 6 | + ToolbarContentContext, |
| 7 | + ToolbarContext, |
| 8 | + ToolbarFilterProps, |
| 9 | + ToolbarItem, |
| 10 | +} from '@patternfly/react-core'; |
| 11 | +import * as React from 'react'; |
| 12 | +import * as ReactDOM from 'react-dom'; |
| 13 | + |
| 14 | +interface ToolbarFilterState { |
| 15 | + isMounted: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +class CustomToolbarFilter extends React.Component<ToolbarFilterProps, ToolbarFilterState> { |
| 19 | + static displayName = 'CustomToolbarFilter'; |
| 20 | + static contextType = ToolbarContext; |
| 21 | + context!: React.ContextType<typeof ToolbarContext>; |
| 22 | + static defaultProps: PickOptional<ToolbarFilterProps> = { |
| 23 | + chips: [] as (string | ToolbarChip)[], |
| 24 | + showToolbarItem: true, |
| 25 | + }; |
| 26 | + |
| 27 | + constructor(props: ToolbarFilterProps) { |
| 28 | + super(props); |
| 29 | + this.state = { |
| 30 | + isMounted: false, |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + componentDidMount() { |
| 35 | + const { categoryName, chips } = this.props; |
| 36 | + this.context.updateNumberFilters( |
| 37 | + typeof categoryName !== 'string' && categoryName.hasOwnProperty('key') |
| 38 | + ? categoryName.key |
| 39 | + : categoryName.toString(), |
| 40 | + chips ? chips.length : 0, |
| 41 | + ); |
| 42 | + this.setState({ isMounted: true }); |
| 43 | + } |
| 44 | + |
| 45 | + componentDidUpdate() { |
| 46 | + const { categoryName, chips } = this.props; |
| 47 | + this.context.updateNumberFilters( |
| 48 | + typeof categoryName !== 'string' && categoryName.hasOwnProperty('key') |
| 49 | + ? categoryName.key |
| 50 | + : categoryName.toString(), |
| 51 | + chips ? chips.length : 0, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + render() { |
| 56 | + const { |
| 57 | + children, |
| 58 | + chips, |
| 59 | + deleteChipGroup, |
| 60 | + deleteChip, |
| 61 | + chipGroupExpandedText, |
| 62 | + chipGroupCollapsedText, |
| 63 | + categoryName, |
| 64 | + showToolbarItem, |
| 65 | + isExpanded, |
| 66 | + expandableChipContainerRef, |
| 67 | + ...props |
| 68 | + } = this.props; |
| 69 | + const { isExpanded: managedIsExpanded, chipGroupContentRef } = this.context; |
| 70 | + const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded; |
| 71 | + const categoryKey = |
| 72 | + typeof categoryName !== 'string' && categoryName.hasOwnProperty('key') |
| 73 | + ? categoryName.key |
| 74 | + : categoryName.toString(); |
| 75 | + |
| 76 | + const chipGroup = |
| 77 | + chips !== undefined && chips.length ? ( |
| 78 | + <ToolbarItem variant="chip-group"> |
| 79 | + <ChipGroup |
| 80 | + key={categoryKey} |
| 81 | + categoryName={typeof categoryName === 'string' ? categoryName : categoryName.name} |
| 82 | + isClosable={deleteChipGroup !== undefined} |
| 83 | + onClick={() => deleteChipGroup && deleteChipGroup(categoryName)} |
| 84 | + collapsedText={chipGroupCollapsedText} |
| 85 | + expandedText={chipGroupExpandedText} |
| 86 | + > |
| 87 | + {chips.map((chip) => |
| 88 | + typeof chip === 'string' ? ( |
| 89 | + <Chip key={chip} onClick={() => deleteChip && deleteChip(categoryKey, chip)}> |
| 90 | + {chip} |
| 91 | + </Chip> |
| 92 | + ) : ( |
| 93 | + <Chip key={chip.key} onClick={() => deleteChip && deleteChip(categoryKey, chip)}> |
| 94 | + {chip.node} |
| 95 | + </Chip> |
| 96 | + ), |
| 97 | + )} |
| 98 | + </ChipGroup> |
| 99 | + </ToolbarItem> |
| 100 | + ) : null; |
| 101 | + |
| 102 | + if (!_isExpanded && this.state.isMounted) { |
| 103 | + return ( |
| 104 | + <React.Fragment> |
| 105 | + {showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>} |
| 106 | + {chipGroupContentRef && |
| 107 | + chipGroupContentRef.current !== null && |
| 108 | + chipGroupContentRef.current.firstElementChild !== null && |
| 109 | + ReactDOM.createPortal(chipGroup, chipGroupContentRef.current.firstElementChild)} |
| 110 | + </React.Fragment> |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + return ( |
| 115 | + <ToolbarContentContext.Consumer> |
| 116 | + {({ chipContainerRef }) => ( |
| 117 | + <React.Fragment> |
| 118 | + {showToolbarItem && <ToolbarItem {...props}>{children}</ToolbarItem>} |
| 119 | + {chipContainerRef.current && |
| 120 | + ReactDOM.createPortal(chipGroup, chipContainerRef.current as Element)} |
| 121 | + {expandableChipContainerRef && |
| 122 | + expandableChipContainerRef.current && |
| 123 | + ReactDOM.createPortal(chipGroup, expandableChipContainerRef.current)} |
| 124 | + </React.Fragment> |
| 125 | + )} |
| 126 | + </ToolbarContentContext.Consumer> |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export { CustomToolbarFilter }; |
0 commit comments