|
1 | | -import { Button, Collapse, FormGroup } from '@mui/material' |
| 1 | +import React, { ChangeEvent, useMemo, useState } from 'react' |
| 2 | +import { Button, FormGroup, TextField, styled } from '@mui/material' |
2 | 3 | import { |
3 | 4 | IFetch, |
4 | 5 | IGraphqlAggregation, |
5 | 6 | IGraphqlAggregationOption, |
6 | 7 | IGraphqlViewMoreFacetOption, |
| 8 | + LoadStatus, |
7 | 9 | } from '@elastic-suite/gally-admin-shared' |
| 10 | +import { IFilterChange } from '../../types' |
| 11 | + |
| 12 | +const SearchOption = styled(TextField)(() => ({ |
| 13 | + width: '100%', |
| 14 | +})) |
| 15 | + |
| 16 | +const NoOption = styled('div')(({ theme }) => ({ |
| 17 | + color: '#AA0000', |
| 18 | + backgroundColor: '#FFEEEE', |
| 19 | + marginBottom: theme.spacing(1), |
| 20 | + padding: theme.spacing(1), |
| 21 | +})) |
8 | 22 |
|
9 | 23 | interface IProps { |
10 | 24 | filter: IGraphqlAggregation |
11 | 25 | id: string |
12 | | - loadMore: (filter: IGraphqlAggregation) => void |
| 26 | + loadMore: (filter: IGraphqlAggregation, optionSearch?: string) => void |
13 | 27 | moreOptions?: IFetch<IGraphqlViewMoreFacetOption[]> |
14 | | - renderOption: (option: IGraphqlAggregationOption) => JSX.Element |
| 28 | + renderOption: ( |
| 29 | + option: IGraphqlAggregationOption, |
| 30 | + onOptionChange: IFilterChange |
| 31 | + ) => JSX.Element |
| 32 | + onOptionChange: IFilterChange |
| 33 | +} |
| 34 | + |
| 35 | +function getFirstLabelsString(items: IGraphqlAggregationOption[]): string { |
| 36 | + const labels = items.slice(0, 2).map((i) => i.label) |
| 37 | + if (items.length > 2) { |
| 38 | + labels.push('...') |
| 39 | + } |
| 40 | + return labels.join(', ') |
15 | 41 | } |
16 | 42 |
|
17 | 43 | function FacetLoadMore(props: IProps): JSX.Element { |
18 | | - const { filter, id, loadMore, moreOptions, renderOption } = props |
19 | | - const open = moreOptions?.data?.length > 0 |
| 44 | + const { filter, id, loadMore, moreOptions, renderOption, onOptionChange } = |
| 45 | + props |
| 46 | + |
| 47 | + const [optionSearch, setOptionSearch] = useState<string>('') |
| 48 | + const [showMore, setShowMore] = useState<boolean>(false) |
20 | 49 |
|
21 | | - function handleToggleMore(): void { |
22 | | - loadMore(filter) |
| 50 | + const searchOptionLabel = useMemo(() => { |
| 51 | + const labels = getFirstLabelsString(filter.options) |
| 52 | + return labels ? `Search (${labels})` : 'Search' |
| 53 | + }, [filter.options]) |
| 54 | + |
| 55 | + function handleToggleMore(showMoreEnabled: boolean): void { |
| 56 | + setShowMore(showMoreEnabled) |
| 57 | + if (showMoreEnabled) { |
| 58 | + loadMore(filter, optionSearch) |
| 59 | + } |
23 | 60 | } |
24 | 61 |
|
| 62 | + function handleOptionSearchChange(value: string): void { |
| 63 | + setOptionSearch(value) |
| 64 | + if (!value) { |
| 65 | + setShowMore(false) |
| 66 | + } else { |
| 67 | + setShowMore(true) |
| 68 | + } |
| 69 | + loadMore(filter, value) |
| 70 | + } |
| 71 | + |
| 72 | + function handleOptionChange(filter: IGraphqlAggregation, value: string) { |
| 73 | + return () => { |
| 74 | + // Empty the search facet field when a value is selected. |
| 75 | + setOptionSearch('') |
| 76 | + onOptionChange(filter, value)() |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const options = |
| 81 | + showMore && |
| 82 | + !moreOptions?.error && |
| 83 | + moreOptions?.status === LoadStatus.SUCCEEDED |
| 84 | + ? moreOptions?.data |
| 85 | + : filter.options |
| 86 | + |
| 87 | + const showShowMore = !showMore || options === filter.options |
| 88 | + const canShowToggleButtons = Boolean(filter.hasMore) && !optionSearch |
| 89 | + |
25 | 90 | return ( |
26 | 91 | <> |
27 | | - {!moreOptions?.data && ( |
28 | | - <FormGroup aria-labelledby={id}> |
29 | | - {filter.options.map(renderOption)} |
30 | | - </FormGroup> |
| 92 | + {Boolean(filter.hasMore) && ( |
| 93 | + <> |
| 94 | + <SearchOption |
| 95 | + id="outlined-basic" |
| 96 | + label={searchOptionLabel} |
| 97 | + variant="outlined" |
| 98 | + name="searchOption" |
| 99 | + value={optionSearch} |
| 100 | + onChange={(e: ChangeEvent<HTMLInputElement>): void => |
| 101 | + handleOptionSearchChange(e.target.value) |
| 102 | + } |
| 103 | + size="small" |
| 104 | + /> |
| 105 | + {Boolean(optionSearch) && |
| 106 | + ((moreOptions?.status === LoadStatus.SUCCEEDED && |
| 107 | + moreOptions?.data?.length < 1) || |
| 108 | + Boolean(moreOptions?.error)) && ( |
| 109 | + <NoOption> |
| 110 | + {moreOptions?.error |
| 111 | + ? 'An error occured' |
| 112 | + : `No value matching the search "${optionSearch}"`} |
| 113 | + </NoOption> |
| 114 | + )} |
| 115 | + </> |
31 | 116 | )} |
32 | | - <Collapse in={open}> |
33 | | - <FormGroup aria-labelledby={id}> |
34 | | - {moreOptions?.data?.map(renderOption)} |
35 | | - </FormGroup> |
36 | | - </Collapse> |
37 | | - {Boolean(filter.hasMore) && !open && ( |
| 117 | + <FormGroup aria-labelledby={id}> |
| 118 | + {options?.map((option) => renderOption(option, handleOptionChange))} |
| 119 | + </FormGroup> |
| 120 | + {canShowToggleButtons && (showShowMore || Boolean(moreOptions?.error)) ? ( |
38 | 121 | <Button |
39 | 122 | disabled={Boolean(moreOptions?.error)} |
40 | | - onClick={handleToggleMore} |
| 123 | + onClick={(): void => handleToggleMore(true)} |
41 | 124 | > |
42 | 125 | {moreOptions?.error ? 'An error occured' : 'Show more'} |
43 | 126 | </Button> |
44 | | - )} |
| 127 | + ) : null} |
| 128 | + |
| 129 | + {canShowToggleButtons && !showShowMore && !moreOptions?.error ? ( |
| 130 | + <Button onClick={(): void => handleToggleMore(false)}>Show less</Button> |
| 131 | + ) : null} |
45 | 132 | </> |
46 | 133 | ) |
47 | 134 | } |
|
0 commit comments