Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 32 additions & 35 deletions frontend/web/components/tables/TableFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React, { FC, ReactNode, useState } from 'react'
import InlineModal from 'components/InlineModal'
import React, { FC, ReactNode } from 'react'
import { IonIcon } from '@ionic/react'
import { caretDown } from 'ionicons/icons'
import classNames from 'classnames'
import Popover from 'components/base/Popover'
import BareButton from 'components/base/forms/BareButton'

type TableFilterType = {
title: string | ReactNode
// Names the filter when the title isn't plain text (it has a count)
label?: string
hideTitle?: boolean
dropdownTitle?: ReactNode | string
className?: string
children: ReactNode
children: ReactNode | ((close: () => void) => ReactNode)
'data-test'?: string
}

Expand All @@ -19,26 +22,25 @@ const TableFilter: FC<TableFilterType> = ({
'data-test': dataTest,
dropdownTitle,
hideTitle,
label,
title,
}) => {
const [open, setOpen] = useState(false)
const toggle = function () {
if (!open) {
setOpen(true)
}
}
return (
<>
<Row
}) => (
<Popover
aria-label={label ?? (typeof title === 'string' ? title : 'Filter')}
placement='bottom-end'
// Width for the anchored panel; the phone sheet is full width
className='inline-modal--sm'
renderTrigger={(props) => (
<BareButton
{...props}
data-test={
dataTest ||
(typeof title === 'string'
? `table-filter-${title.toLowerCase()}`
: undefined)
}
onClick={toggle}
className={classNames(
'cursor-pointer py-2 py-lg-0 user-select-none',
'd-flex align-items-center py-2 py-lg-0 user-select-none',
className,
)}
>
Expand All @@ -51,25 +53,20 @@ const TableFilter: FC<TableFilterType> = ({
}}
icon={caretDown}
/>
</Row>
{open && (
<InlineModal
hideClose
title={hideTitle ? null : dropdownTitle || title}
isOpen={open}
onClose={() => {
setTimeout(() => {
setOpen(false)
}, 10)
}}
containerClassName='px-0'
className='inline-modal table-filter inline-modal--sm right'
>
{children}
</InlineModal>
)}
</>
)
}
</BareButton>
)}
>
{(close) => (
<>
{!hideTitle && (
<div className='inline-modal__title px-3 py-2'>
{dropdownTitle || title}
</div>
)}
{typeof children === 'function' ? children(close) : children}
</>
)}
</Popover>
)

export default TableFilter
151 changes: 62 additions & 89 deletions frontend/web/components/tables/TableFilterOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import React, { FC, ReactNode, useMemo, useState } from 'react'
import InlineModal from 'components/InlineModal'
import { IonIcon } from '@ionic/react'
import { caretDown } from 'ionicons/icons'
import classNames from 'classnames'
import TableFilter from './TableFilter'
import TableFilterItem from './TableFilterItem'
import Input from 'components/base/forms/Input'
import Utils from 'common/utils/utils'

type TableFilterType = {
type TableFilterOptionsType = {
title: ReactNode
label?: string
dropdownTitle?: ReactNode | string
className?: string
size?: 'sm' | 'lg'
isLoading?: boolean
options: { label: string; value: string | number; subtitle?: string }[]
onChange: (value: string | string[]) => void | Promise<void>
Expand All @@ -20,26 +18,19 @@ type TableFilterType = {
showSearch?: boolean
}

const TableFilter: FC<TableFilterType> = ({
const TableFilterOptions: FC<TableFilterOptionsType> = ({
className,
isLoading,
label,
multiple,
onChange,
options,
showSearch,
size = 'sm',
title,
value,
}) => {
const [filter, setFilter] = useState('')

const [open, setOpen] = useState(false)
const toggle = function () {
if (!open) {
setOpen(true)
}
}

const filteredOptions = useMemo(() => {
const lowerSearch = filter?.toLowerCase()
return filter
Expand All @@ -51,86 +42,68 @@ const TableFilter: FC<TableFilterType> = ({

return (
<div className={isLoading ? 'disabled' : ''}>
<Row
onClick={toggle}
className={classNames('cursor-pointer user-select-none', className)}
<TableFilter
title={title}
label={label}
hideTitle
className={classNames('user-select-none', className)}
>
<span>{title} </span>
<IonIcon
style={{
fontSize: 11,
marginLeft: 2,
marginTop: 4,
}}
icon={caretDown}
/>
</Row>
{open && (
<InlineModal
hideClose
title={null}
isOpen={open}
onClose={() => {
setTimeout(() => {
setOpen(false)
}, 10)
}}
containerClassName='px-0'
className={`inline-modal table-filter inline-modal--${size} right`}
>
{!!showSearch && (
<div className='px-2 mt-2 mb-2'>
<Input
autoFocus
onChange={(e) => {
setFilter(Utils.safeParseEventValue(e))
}}
className='full-width'
value={filter}
type='text'
size='xSmall'
placeholder='Search'
search
/>
{!filteredOptions?.length && (
<div className='text-center py-2'>No results</div>
)}
</div>
)}
<div className='table-filter-list'>
{filteredOptions.map((v) => (
<TableFilterItem
key={v.value}
title={v.label}
subtitle={v.subtitle}
onClick={() => {
if (!multiple) {
setOpen(false)
}
if (multiple) {
if ((value || []).includes(v.value)) {
onChange(
((value as string[]) || []).filter(
(item) => item !== v.value,
),
)
{(close) => (
<>
{!!showSearch && (
<div className='px-3 my-2'>
<Input
autoFocus
onChange={(e) => {
setFilter(Utils.safeParseEventValue(e))
}}
className='full-width'
value={filter}
type='text'
size='xSmall'
placeholder='Search'
search
/>
{!filteredOptions?.length && (
<div className='text-center py-2'>No results</div>
)}
</div>
)}
<div className='table-filter-list'>
{filteredOptions.map((v) => (
<TableFilterItem
key={v.value}
title={v.label}
subtitle={v.subtitle}
onClick={() => {
if (!multiple) {
close()
}
if (multiple) {
if ((value || []).includes(v.value)) {
onChange(
((value as string[]) || []).filter(
(item) => item !== v.value,
),
)
} else {
onChange(((value as string[]) || []).concat([v.value]))
}
} else {
onChange(((value as string[]) || []).concat([v.value]))
onChange(v.value)
}
} else {
onChange(v.value)
}}
isActive={
multiple ? value?.includes(v.value) : value === v.value
}
}}
isActive={
multiple ? value?.includes(v.value) : value === v.value
}
/>
))}
</div>
</InlineModal>
)}
/>
))}
</div>
</>
)}
</TableFilter>
</div>
)
}

export default TableFilter
export default TableFilterOptions
1 change: 1 addition & 0 deletions frontend/web/components/tables/TableGroupsFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const TableGroupsFilter: FC<TableFilterType> = ({

return (
<TableFilterOptions
label='Groups'
className={className}
title={
<Row>
Expand Down
2 changes: 1 addition & 1 deletion frontend/web/components/tables/TableOwnerFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const TableOwnerFilter: FC<TableFilterType> = ({

return (
<TableFilterOptions
size='lg'
label='Users'
className={className}
isLoading={isLoading}
title={
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/components/tables/TableTagFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const TableTagFilter: FC<TableFilterType> = ({
<div className={isLoading ? 'disabled' : ''}>
<TableFilter
data-test='table-filter-tags'
label='Tags'
className={className}
dropdownTitle={
<>
Expand Down Expand Up @@ -101,7 +102,7 @@ const TableTagFilter: FC<TableFilterType> = ({
}
>
<div className='inline-modal__list d-flex flex-column mx-0 py-0'>
<div className='px-2 my-2'>
<div className='px-3 my-2'>
<Input
autoFocus
onChange={(e) => {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/components/tables/TableValueFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const TableTagFilter: FC<TableFilterType> = ({
return (
<div className={isLoading ? 'disabled' : ''}>
<TableFilter
label='Value'
className={className}
hideTitle
title={
Expand All @@ -61,7 +62,7 @@ const TableTagFilter: FC<TableFilterType> = ({
}
>
<div className='inline-modal__list d-flex flex-column mx-0 py-0'>
<div className='px-2 mt-2'>
<div className='px-3 my-2'>
<InputGroup
title='Enabled State'
className='mt-2'
Expand Down
10 changes: 0 additions & 10 deletions frontend/web/styles/project/_modals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,6 @@ $side-width: 800px;
&.table-filter {
top:12px;
}
&--lg {
width: 355px;
.inline-modal__title {
padding: 8px 12px;
* {
font-size: $font-sm !important;
line-height: $line-height-sm !important;
}
}
}
&--sm {
min-width: 200px;
max-width: 355px;
Expand Down
Loading