-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from HolodexNet/feature/search-ui-fix
- Loading branch information
Showing
10 changed files
with
627 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { | ||
Checkbox, | ||
CheckboxGroup, | ||
IconButton, | ||
Input, | ||
InputGroup, | ||
InputRightElement, | ||
Tag, | ||
VStack, | ||
} from "@chakra-ui/react"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { RiCloseFill } from "react-icons/ri"; | ||
|
||
interface CheckboxSearchListProps { | ||
dataField: string; | ||
placeholder?: string; | ||
showSearch?: boolean; | ||
aggregations: { | ||
[k: string]: { buckets: Array<{ key: string; doc_count: number }> }; | ||
}; | ||
setQuery: (query: any) => void; | ||
value: string[] | null; | ||
tagLabel?: string; | ||
} | ||
|
||
export const CheckboxSearchList = ({ | ||
dataField, | ||
placeholder, | ||
showSearch = false, | ||
aggregations, | ||
setQuery, | ||
value, | ||
tagLabel, | ||
}: CheckboxSearchListProps) => { | ||
const { t } = useTranslation(); | ||
const [filterValue, setFilterValue] = useState(""); | ||
const [checkboxValues, setCheckboxValues] = useState<Array<string | number>>( | ||
value!, | ||
); | ||
|
||
const getTermsQuery = useCallback( | ||
(values: typeof checkboxValues) => { | ||
if (!values?.length) return {}; | ||
|
||
return { query: { terms: { [dataField]: values } } }; | ||
}, | ||
[dataField], | ||
); | ||
|
||
useEffect(() => { | ||
setQuery({ | ||
value: checkboxValues, | ||
query: getTermsQuery(checkboxValues), | ||
}); | ||
}, [checkboxValues, getTermsQuery, setQuery]); | ||
|
||
// Support resetting from SelectedFilters | ||
useEffect(() => { | ||
if (value === null) setCheckboxValues([]); | ||
}, [value]); | ||
|
||
if (!aggregations?.[dataField]?.buckets?.length) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{tagLabel && ( | ||
<Tag colorScheme="brand" size="md" alignSelf="start"> | ||
{tagLabel} | ||
</Tag> | ||
)} | ||
{showSearch && ( | ||
<InputGroup> | ||
<Input | ||
value={filterValue} | ||
onChange={(e) => setFilterValue(e.target.value)} | ||
placeholder={placeholder!} | ||
/> | ||
<InputRightElement> | ||
{filterValue && ( | ||
<IconButton | ||
color="red.400" | ||
colorScheme="red" | ||
size="sm" | ||
variant="ghost" | ||
aria-label={t("Clear")} | ||
icon={<RiCloseFill />} | ||
type="button" | ||
title={t("Clear")} | ||
onClick={() => setFilterValue("")} | ||
></IconButton> | ||
)} | ||
</InputRightElement> | ||
</InputGroup> | ||
)} | ||
<CheckboxGroup | ||
value={checkboxValues} | ||
onChange={(e) => setCheckboxValues(e)} | ||
> | ||
<VStack maxH="200px" alignItems="stretch" overflowY="scroll" p={2}> | ||
{aggregations?.[dataField]?.buckets | ||
?.filter(({ key }) => | ||
key.toLowerCase().includes(filterValue.toLowerCase()), | ||
) | ||
.map(({ key }) => ( | ||
<Checkbox key={key} value={key}> | ||
{key} | ||
</Checkbox> | ||
))} | ||
</VStack> | ||
</CheckboxGroup> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
IconButton, | ||
Input, | ||
InputGroup, | ||
InputRightElement, | ||
Tag, | ||
} from "@chakra-ui/react"; | ||
import { useEffect, useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { RiSearch2Line } from "react-icons/ri"; | ||
import { useDebounce } from "use-debounce"; | ||
|
||
interface GeneralInputProps { | ||
debounceValue?: number; | ||
placeholder?: string; | ||
getQuery: (q: string) => object; | ||
setQuery: (query: { value?: string; query?: any; opts?: any }) => void; | ||
value: string | null; | ||
tagLabel?: string; | ||
} | ||
|
||
export const GeneralSearchInput = ({ | ||
debounceValue = 1000, | ||
placeholder, | ||
getQuery, | ||
setQuery, | ||
value, | ||
tagLabel, | ||
}: GeneralInputProps) => { | ||
const { t } = useTranslation(); | ||
const [searchText, setSearchText] = useState<string>(value!); | ||
const [debouncedSearchText, { flush }] = useDebounce( | ||
searchText, | ||
debounceValue, | ||
); | ||
|
||
useEffect(() => { | ||
setQuery({ | ||
value: debouncedSearchText, | ||
query: getQuery(debouncedSearchText), | ||
}); | ||
}, [debouncedSearchText, getQuery, setQuery]); | ||
|
||
// Support resetting from SelectedFilters | ||
useEffect(() => { | ||
if (value === null) setSearchText(""); | ||
}, [value]); | ||
|
||
return ( | ||
<> | ||
{tagLabel && ( | ||
<Tag colorScheme="brand" size="md" alignSelf="start"> | ||
{tagLabel} | ||
</Tag> | ||
)} | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
flush(); | ||
}} | ||
> | ||
<InputGroup> | ||
<Input | ||
value={searchText} | ||
onChange={(e) => setSearchText(e.target.value)} | ||
placeholder={placeholder} | ||
></Input> | ||
<InputRightElement> | ||
<IconButton | ||
color="brand.400" | ||
colorScheme="brand" | ||
size="sm" | ||
variant="ghost" | ||
aria-label={t("Search")} | ||
icon={<RiSearch2Line />} | ||
type="submit" | ||
title={t("Search")} | ||
></IconButton> | ||
</InputRightElement> | ||
</InputGroup> | ||
</form> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { | ||
Radio, | ||
RadioGroup, | ||
Input, | ||
VStack, | ||
Tag, | ||
InputRightElement, | ||
InputGroup, | ||
IconButton, | ||
} from "@chakra-ui/react"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { RiCloseFill } from "react-icons/ri"; | ||
|
||
interface RadioButtonSearchListProps { | ||
dataField: string; | ||
placeholder?: string; | ||
showSearch?: boolean; | ||
aggregations: { | ||
[k: string]: { buckets: Array<{ key: string; doc_count: number }> }; | ||
}; | ||
setQuery: (query: any) => void; | ||
value: string | null; | ||
tagLabel?: string; | ||
} | ||
|
||
export const RadioButtonSearchList = ({ | ||
dataField, | ||
placeholder, | ||
showSearch = false, | ||
aggregations, | ||
setQuery, | ||
value, | ||
tagLabel, | ||
}: RadioButtonSearchListProps) => { | ||
const { t } = useTranslation(); | ||
const [filterValue, setFilterValue] = useState(""); | ||
const [radioValue, setRadioValue] = useState<string>(value!); | ||
|
||
const getQuery = useCallback( | ||
(value: string) => { | ||
if (!value) return {}; | ||
|
||
return { query: { term: { [dataField]: value } } }; | ||
}, | ||
[dataField], | ||
); | ||
|
||
useEffect(() => { | ||
setQuery({ | ||
value: radioValue, | ||
query: getQuery(radioValue), | ||
}); | ||
}, [radioValue, getQuery, setQuery]); | ||
|
||
// Support resetting from SelectedFilters | ||
useEffect(() => { | ||
if (value === null) setRadioValue(""); | ||
}, [value]); | ||
|
||
if (!aggregations?.[dataField]?.buckets?.length) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{tagLabel && ( | ||
<Tag colorScheme="brand" size="md" alignSelf="start"> | ||
{tagLabel} | ||
</Tag> | ||
)} | ||
{showSearch && ( | ||
<InputGroup> | ||
<Input | ||
value={filterValue} | ||
onChange={(e) => setFilterValue(e.target.value)} | ||
placeholder={placeholder!} | ||
/> | ||
<InputRightElement> | ||
{filterValue && ( | ||
<IconButton | ||
color="red.400" | ||
colorScheme="red" | ||
size="sm" | ||
variant="ghost" | ||
aria-label={t("Clear")} | ||
icon={<RiCloseFill />} | ||
type="button" | ||
title={t("Clear")} | ||
onClick={() => setFilterValue("")} | ||
></IconButton> | ||
)} | ||
</InputRightElement> | ||
</InputGroup> | ||
)} | ||
<RadioGroup value={radioValue} onChange={(value) => setRadioValue(value)}> | ||
<VStack maxH="200px" alignItems="stretch" overflowY="scroll" p={2}> | ||
{aggregations?.[dataField]?.buckets | ||
?.filter(({ key }) => | ||
key.toLowerCase().includes(filterValue.toLowerCase()), | ||
) | ||
.map(({ key }) => ( | ||
<Radio key={key} value={key}> | ||
{key} | ||
</Radio> | ||
))} | ||
</VStack> | ||
</RadioGroup> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.