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