-
Notifications
You must be signed in to change notification settings - Fork 5
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 #1614 from merico-dev/current-view-filters-value
add useVisibleFilters
- Loading branch information
Showing
5 changed files
with
114 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import dayjs from 'dayjs'; | ||
import { useMemo } from 'react'; | ||
import { useDashboardContext } from '../../contexts'; | ||
import { | ||
DashboardFilterType, | ||
DateRangeValue, | ||
EViewComponentType, | ||
FilterDateRangeConfigInstance, | ||
FilterMetaInstance, | ||
FilterSelectConfigInstance, | ||
} from '../../model'; | ||
import { formatDateRangeValue } from './filter-date-range/render'; | ||
|
||
const NOT_AVAILABLE = 'N/A'; | ||
|
||
/** | ||
* Returns the formatted filter values in the current view. | ||
*/ | ||
export function useVisibleFilters(): IFormattedFilter[] { | ||
const model = useDashboardContext(); | ||
const visibleViews = model.content.views.visibleViews; | ||
return useMemo(() => { | ||
const viewIds = visibleViews.map((it) => { | ||
if (it.type === EViewComponentType.Tabs) { | ||
return it.tabView?.view_id; | ||
} | ||
return it.id; | ||
}); | ||
const filters = viewIds | ||
.filter(Boolean) | ||
.flatMap((it) => model.content.filters.visibleInView(it!)) | ||
.filter(Boolean); | ||
return filters.map((it) => formatFilter(it)); | ||
}, [visibleViews]); | ||
} | ||
|
||
export interface IFormattedFilter { | ||
label: string; | ||
value: string | string[]; | ||
} | ||
|
||
function formatFilter(filter: FilterMetaInstance): IFormattedFilter { | ||
switch (filter.type) { | ||
case DashboardFilterType.Select: | ||
case DashboardFilterType.MultiSelect: | ||
case DashboardFilterType.TreeSelect: | ||
case DashboardFilterType.TreeSingleSelect: | ||
return { label: filter.label, value: valueOfSelect(filter.config, filter.value) }; | ||
case DashboardFilterType.TextInput: | ||
return { label: filter.label, value: valueOfTextInput(filter.value) }; | ||
case DashboardFilterType.DateRange: | ||
return { label: filter.label, value: valueOfDateRange(filter.config, filter.value) }; | ||
case DashboardFilterType.Checkbox: | ||
return { label: filter.label, value: valueOfCheckbox(filter.value) }; | ||
default: | ||
return { label: filter.label, value: filter.value }; | ||
} | ||
} | ||
|
||
function valueOfSelect(config: FilterSelectConfigInstance, value: string | string[]): string { | ||
const set = new Set(); | ||
if (Array.isArray(value)) { | ||
value.forEach((it) => set.add(it)); | ||
} else { | ||
set.add(value); | ||
} | ||
if (set.size === 0) { | ||
return NOT_AVAILABLE; | ||
} | ||
return config.options | ||
.filter((it) => set.has(it.value)) | ||
.map((it) => it.label) | ||
.join(', '); | ||
} | ||
|
||
function valueOfTextInput(value: string): string { | ||
return value; | ||
} | ||
|
||
function valueOfDateRange(config: FilterDateRangeConfigInstance, value: DateRangeValue): string { | ||
const formattedValue = formatDateRangeValue(value); | ||
const dateValues = formattedValue.value.filter((it) => !!it).map((it) => dayjs(it).format(config.inputFormat)); | ||
if (dateValues.length === 0) { | ||
return NOT_AVAILABLE; | ||
} | ||
return dateValues.join(' - '); | ||
} | ||
|
||
function valueOfCheckbox(value: boolean): string { | ||
return value ? '✔️' : '❌'; | ||
} |
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