Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | 1x 1x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 12x 12x 12x 68x 68x 64x 68x 68x 68x 68x 4x 4x 4x 4x 4x 4x 4x 4x 68x 4x 4x 4x 4x 4x 4x 4x 4x 4x 68x 68x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 68x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 68x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 68x 2x 2x 2x 2x 2x 2x 68x 68x 68x | import {
Button,
Input,
Select,
Datepicker,
RangeInput,
RangeDatepicker,
InDevelopmentWrapper,
Tooltip,
} from '@tmk/ui-kit'
import { TableFilterItem, TableFilterQuery } from '@/widgets/table'
import { BaseFilter, MultipleBaseFilter, OrderBaseFilter } from '@/shared/@types'
import { SelectWithQuery, useTranslate } from '@/shared/lib'
import { RangeValue } from 'rc-picker/lib/interface'
import { getIdFromIRI } from '@/shared/helpers'
import { useUpdateAtom } from 'jotai/utils'
import { useAtomValue } from 'jotai'
import { ChangeEvent, useState } from 'react'
import { Dayjs } from 'dayjs'
import TrashIcon from '@/shared/assets/icons/common/TrashIcon.svg'
import { MultiSelect } from '@/shared/ui'
export interface CurrentFilterProps extends TableFilterItem {
dependentEntityType?: 'id' | 'filter'
dependentEntity?: Record<string, string>
setCurrentFilter?: (value: TableFilterItem | null) => void
withDelete?: boolean
isRange?: boolean
isApplySingleOption?: boolean
onDelete?: () => void
loadingOnMount?: boolean
withQuery?: boolean
isLoading?: boolean
}
export const CurrentFilter = ({
name,
inDevelopment,
type = 'selectSearch',
labelKey = 'name',
valueKey = 'id',
label,
useQuery,
options,
atom,
scope,
showTime,
isMultiple = false,
inputProps,
withDelete = false,
onDelete,
isRange,
dependentEntityType,
dependentEntity,
dependentLabel,
isApplySingleOption,
loadingOnMount = false,
withQuery = true,
isLoading,
onAfterChange,
...rest
}: CurrentFilterProps) => {
const updateFilter = useUpdateAtom(atom, scope)
const value = useAtomValue(atom, scope)
const { t } = useTranslate(['common'])
const [searchValue, setSearchValue] = useState<string>('')
const [query, setQuery] = useState<string>(() => (value as string) || '')
const [rangeValue, setRangeValue] = useState<(string | number | null)[]>(() => (value as string[]) || [null, null])
const [timerId, setTimerId] = useState<NodeJS.Timeout | null>(null)
const isSelectSearch = type === 'selectSearch'
const isMultipleSelect = type === 'multiSelect'
const isSelect = type === 'select'
const isDate = type === 'date'
const isRangeDate = type === 'range-date'
const isRangeInput = type === 'range'
const resetFilter = () => {
setQuery('')
setRangeValue([null, null])
applyFilterValue(null)
onDelete?.()
}
const applyFilterValue = (filterValue: OrderBaseFilter) => {
updateFilter(prev => {
if (isSelectSearch || isSelect || isMultipleSelect) {
const valueId = Array.isArray(filterValue)
? (filterValue as BaseFilter[])?.map(item => (item?.id ? getIdFromIRI(item?.id) : item))
: getIdFromIRI((filterValue as BaseFilter)?.id)
if (!valueId && isSelectSearch) {
return null
}
const value = {
id: isSelectSearch || isMultipleSelect ? valueId : filterValue,
value: isSelect
? options?.find(item => item.id === filterValue)?.label
: isMultipleSelect
? (filterValue as BaseFilter[])?.map(item => item.label).join(', ')
: (filterValue as BaseFilter)?.label,
label,
}
return Array.isArray(prev) && isMultiple
? ([...(prev as BaseFilter[]).filter(item => item?.id !== value?.id), value] as OrderBaseFilter)
: value?.id || typeof value?.id === 'boolean'
? (value as OrderBaseFilter)
: null
}
if (isRangeInput) {
const isEmpty = (filterValue as string[])?.[0] === '' && (filterValue as string[])?.[1] === ''
return isEmpty ? null : filterValue
}
if ((typeof prev === 'string' || typeof prev === 'number') && !isDate && isMultiple) {
return [prev, filterValue] as OrderBaseFilter
}
if (Array.isArray(prev) && !isRangeDate && !isDate && !isRangeInput) {
return [...prev, filterValue] as OrderBaseFilter
}
return filterValue as OrderBaseFilter
})
}
const changeDateFilterValue = (range: RangeValue<Dayjs>) => {
applyFilterValue([range?.[0]?.format() || '', range?.[1]?.format() || ''])
}
const changeSearchFilterValue = (value: BaseFilter) => {
applyFilterValue(value)
onAfterChange?.(value)
}
const changeInputFilterValue = (event: ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value)
if (timerId) {
clearTimeout(timerId)
}
const newTimerId = setTimeout(() => {
applyFilterValue(event.target.value)
}, 300)
setTimerId(newTimerId)
}
const getSearchOptions = () => {
if (searchValue) {
return options?.filter(item => item.label.toLowerCase().includes(searchValue.toLowerCase()))
}
return options
}
const onChangeRange = (value: (string | number)[]) => {
if (timerId) {
clearTimeout(timerId)
}
setRangeValue(value)
const timeout = setTimeout(() => {
applyFilterValue(value as OrderBaseFilter)
}, 300)
setTimerId(timeout)
}
if (inDevelopment) {
return (
<InDevelopmentWrapper t={t} wrapperClassName='w-full'>
<Input name={name} label={label} value={value as string} onChange={changeInputFilterValue} {...inputProps} />
</InDevelopmentWrapper>
)
}
const isActiveTooltip =
dependentEntity && Object.values(dependentEntity).filter(Boolean)?.length === 0 && !!dependentLabel
return (
<div className='flex items-center gap-x-2.5'>
{type === 'input' && (
<Input
name={name}
label={label}
labelClassName='w-[90%] truncate'
value={query}
onChange={changeInputFilterValue}
{...inputProps}
/>
)}
{type === 'date' && (
<Datepicker
name={name}
value={value as string}
onChange={applyFilterValue}
inputProps={{
label,
labelClassName: 'w-[65%] truncate',
}}
/>
)}
{type === 'range-date' && (
<RangeDatepicker
name={name}
onChange={changeDateFilterValue}
resetTime={!showTime}
className='mt-medium'
t={t}
value={(value as [string, string]) || [null, null]}
/>
)}
{type === 'select' && (
<Select
name={name}
label={label}
value={(value as BaseFilter)?.id}
onChange={applyFilterValue}
options={options}
inputProps={inputProps}
t={t}
isLoading={isLoading}
/>
)}
{((type === 'selectSearch' && withQuery) || (type === 'multiSelect' && withQuery)) && (
<Tooltip wrapperClassName='w-full' label={dependentLabel as string} isActive={!!isActiveTooltip}>
<SelectWithQuery
useQuery={useQuery as TableFilterQuery}
value={(value as BaseFilter)?.id}
name={name}
onChangeWithOptions={value => changeSearchFilterValue(value as BaseFilter)}
label={label}
type={type}
labelKey={labelKey}
valueKey={valueKey}
inputProps={inputProps}
dependentEntityType={dependentEntityType}
loadingOnMount={loadingOnMount}
isApplySingleOption={isApplySingleOption}
{...(!!dependentEntity && { dependentEntities: { ...dependentEntity } })}
{...rest}
/>
</Tooltip>
)}
{type === 'multiSelect' && !withQuery && (
<MultiSelect
name={name}
label={label}
options={getSearchOptions()}
value={(value as MultipleBaseFilter)?.id}
onChangeWithOptions={value => applyFilterValue(value as OrderBaseFilter)}
{...rest}
isLoading={isLoading}
onCustomSearch={setSearchValue}
/>
)}
{type === 'range' && (
<RangeInput
name={name}
value={(rangeValue as (string | number)[]) || ['', '']}
onChange={data => onChangeRange(data)}
{...rest}
/>
)}
{withDelete && (
<Button variant='icon' className='mt-medium' onClick={resetFilter}>
<TrashIcon className='fill-blue-dark' />
</Button>
)}
</div>
)
}
|