Resetting inclusion/exclusion fields with filters
This commit is contained in:
parent
737300c80a
commit
bc8b4c200c
3 changed files with 25 additions and 14 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import TableField from '~components/common/TableField'
|
import TableField from '~components/common/TableField'
|
||||||
import MentionTypeahead from '../MentionTypeahead'
|
import MentionTypeahead from '../MentionTypeahead'
|
||||||
|
import Typeahead from 'react-bootstrap-typeahead/types/core/Typeahead'
|
||||||
|
|
||||||
interface Props
|
interface Props
|
||||||
extends React.DetailedHTMLProps<
|
extends React.DetailedHTMLProps<
|
||||||
|
|
@ -11,6 +12,7 @@ interface Props
|
||||||
placeholder?: string
|
placeholder?: string
|
||||||
inclusions: MentionItem[]
|
inclusions: MentionItem[]
|
||||||
exclusions: MentionItem[]
|
exclusions: MentionItem[]
|
||||||
|
typeaheadRef: React.Ref<Typeahead>
|
||||||
onUpdate: (content: MentionItem[]) => void
|
onUpdate: (content: MentionItem[]) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,6 +22,7 @@ const MentionTableField = ({
|
||||||
placeholder,
|
placeholder,
|
||||||
inclusions,
|
inclusions,
|
||||||
exclusions,
|
exclusions,
|
||||||
|
typeaheadRef,
|
||||||
...props
|
...props
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
return (
|
return (
|
||||||
|
|
@ -31,6 +34,7 @@ const MentionTableField = ({
|
||||||
label={label}
|
label={label}
|
||||||
>
|
>
|
||||||
<MentionTypeahead
|
<MentionTypeahead
|
||||||
|
ref={typeaheadRef}
|
||||||
label={label}
|
label={label}
|
||||||
description={description}
|
description={description}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import React from 'react'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { getCookie } from 'cookies-next'
|
import { getCookie } from 'cookies-next'
|
||||||
import { useTranslation } from 'next-i18next'
|
import { useTranslation } from 'next-i18next'
|
||||||
|
|
@ -14,6 +15,7 @@ import {
|
||||||
RenderMenuProps,
|
RenderMenuProps,
|
||||||
Token,
|
Token,
|
||||||
} from 'react-bootstrap-typeahead'
|
} from 'react-bootstrap-typeahead'
|
||||||
|
import Typeahead from 'react-bootstrap-typeahead/types/core/Typeahead'
|
||||||
|
|
||||||
import api from '~utils/api'
|
import api from '~utils/api'
|
||||||
import { numberToElement } from '~utils/elements'
|
import { numberToElement } from '~utils/elements'
|
||||||
|
|
@ -41,14 +43,10 @@ interface RawSearchResponse {
|
||||||
element: number
|
element: number
|
||||||
}
|
}
|
||||||
|
|
||||||
const MentionTypeahead = ({
|
const MentionTypeahead = React.forwardRef<Typeahead, Props>(function Typeahead(
|
||||||
label,
|
{ label, description, placeholder, inclusions, exclusions, ...props }: Props,
|
||||||
description,
|
forwardedRef
|
||||||
placeholder,
|
) {
|
||||||
inclusions,
|
|
||||||
exclusions,
|
|
||||||
...props
|
|
||||||
}: Props) => {
|
|
||||||
const { t } = useTranslation('common')
|
const { t } = useTranslation('common')
|
||||||
const locale = getCookie('NEXT_LOCALE')
|
const locale = getCookie('NEXT_LOCALE')
|
||||||
? (getCookie('NEXT_LOCALE') as string)
|
? (getCookie('NEXT_LOCALE') as string)
|
||||||
|
|
@ -159,6 +157,7 @@ const MentionTypeahead = ({
|
||||||
return (
|
return (
|
||||||
<AsyncTypeahead
|
<AsyncTypeahead
|
||||||
multiple
|
multiple
|
||||||
|
ref={forwardedRef}
|
||||||
className={styles.typeahead}
|
className={styles.typeahead}
|
||||||
id={label}
|
id={label}
|
||||||
align="left"
|
align="left"
|
||||||
|
|
@ -166,7 +165,6 @@ const MentionTypeahead = ({
|
||||||
labelKey={(option) => (option as MentionItem).name[locale]}
|
labelKey={(option) => (option as MentionItem).name[locale]}
|
||||||
defaultSelected={inclusions}
|
defaultSelected={inclusions}
|
||||||
filterBy={() => true}
|
filterBy={() => true}
|
||||||
minLength={3}
|
|
||||||
onSearch={handleSearch}
|
onSearch={handleSearch}
|
||||||
options={options}
|
options={options}
|
||||||
useCache={false}
|
useCache={false}
|
||||||
|
|
@ -181,6 +179,6 @@ const MentionTypeahead = ({
|
||||||
onChange={(selected) => props.onUpdate(selected as MentionItem[])}
|
onChange={(selected) => props.onUpdate(selected as MentionItem[])}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
export default MentionTypeahead
|
export default MentionTypeahead
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
import { getCookie, setCookie } from 'cookies-next'
|
import { getCookie, setCookie } from 'cookies-next'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import { Trans, useTranslation } from 'react-i18next'
|
import { Trans, useTranslation } from 'react-i18next'
|
||||||
|
|
@ -10,16 +10,16 @@ import DialogContent from '~components/common/DialogContent'
|
||||||
|
|
||||||
import Button from '~components/common/Button'
|
import Button from '~components/common/Button'
|
||||||
import InputTableField from '~components/common/InputTableField'
|
import InputTableField from '~components/common/InputTableField'
|
||||||
|
import MentionTableField from '~components/common/MentionTableField'
|
||||||
import SelectTableField from '~components/common/SelectTableField'
|
import SelectTableField from '~components/common/SelectTableField'
|
||||||
import SliderTableField from '~components/common/SliderTableField'
|
import SliderTableField from '~components/common/SliderTableField'
|
||||||
import SwitchTableField from '~components/common/SwitchTableField'
|
import SwitchTableField from '~components/common/SwitchTableField'
|
||||||
import SelectItem from '~components/common/SelectItem'
|
import SelectItem from '~components/common/SelectItem'
|
||||||
|
|
||||||
import type { DialogProps } from '@radix-ui/react-dialog'
|
import type { DialogProps } from '@radix-ui/react-dialog'
|
||||||
|
import Typeahead from 'react-bootstrap-typeahead/types/core/Typeahead'
|
||||||
|
|
||||||
import styles from './index.module.scss'
|
import styles from './index.module.scss'
|
||||||
import MentionTableField from '~components/common/MentionTableField'
|
|
||||||
import classNames from 'classnames'
|
|
||||||
|
|
||||||
interface Props extends DialogProps {
|
interface Props extends DialogProps {
|
||||||
defaultFilterSet: FilterSet
|
defaultFilterSet: FilterSet
|
||||||
|
|
@ -43,6 +43,8 @@ const FilterModal = (props: Props) => {
|
||||||
// Refs
|
// Refs
|
||||||
const headerRef = React.createRef<HTMLDivElement>()
|
const headerRef = React.createRef<HTMLDivElement>()
|
||||||
const footerRef = React.createRef<HTMLDivElement>()
|
const footerRef = React.createRef<HTMLDivElement>()
|
||||||
|
const inclusionRef = useRef<Typeahead>(null)
|
||||||
|
const exclusionRef = useRef<Typeahead>(null)
|
||||||
|
|
||||||
// States
|
// States
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
|
|
@ -88,7 +90,6 @@ const FilterModal = (props: Props) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.open !== undefined) {
|
if (props.open !== undefined) {
|
||||||
setOpen(props.open)
|
setOpen(props.open)
|
||||||
|
|
||||||
// When should we reset the filter state?
|
// When should we reset the filter state?
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -160,6 +161,12 @@ const FilterModal = (props: Props) => {
|
||||||
setUserQuality(props.defaultFilterSet.user_quality)
|
setUserQuality(props.defaultFilterSet.user_quality)
|
||||||
setNameQuality(props.defaultFilterSet.name_quality)
|
setNameQuality(props.defaultFilterSet.name_quality)
|
||||||
setOriginalOnly(props.defaultFilterSet.original)
|
setOriginalOnly(props.defaultFilterSet.original)
|
||||||
|
|
||||||
|
setInclusions([])
|
||||||
|
inclusionRef.current?.clear()
|
||||||
|
|
||||||
|
setExclusions([])
|
||||||
|
exclusionRef.current?.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
function openChange() {
|
function openChange() {
|
||||||
|
|
@ -413,6 +420,7 @@ const FilterModal = (props: Props) => {
|
||||||
exclusions={exclusions}
|
exclusions={exclusions}
|
||||||
placeholder={t('modals.filters.placeholders.inclusion')}
|
placeholder={t('modals.filters.placeholders.inclusion')}
|
||||||
label={t('modals.filters.labels.inclusion')}
|
label={t('modals.filters.labels.inclusion')}
|
||||||
|
typeaheadRef={inclusionRef}
|
||||||
onUpdate={storeInclusions}
|
onUpdate={storeInclusions}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
@ -424,6 +432,7 @@ const FilterModal = (props: Props) => {
|
||||||
exclusions={inclusions}
|
exclusions={inclusions}
|
||||||
placeholder={t('modals.filters.placeholders.exclusion')}
|
placeholder={t('modals.filters.placeholders.exclusion')}
|
||||||
label={t('modals.filters.labels.exclusion')}
|
label={t('modals.filters.labels.exclusion')}
|
||||||
|
typeaheadRef={exclusionRef}
|
||||||
onUpdate={storeExclusions}
|
onUpdate={storeExclusions}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue