import React, { useCallback, useState } from 'react'; import { Button, Divider, Form, Icon, Label, List, Menu } from 'semantic-ui-react'; import { filter, find, get, head, map } from 'lodash-es'; import { FormSelect } from 'semantic-ui-react'; import { RELATABLE_ICONS, SORT_ACTIONS } from '../../relatable.types'; import { useRelatableStateContext, useRelatableToolbarContext } from '../../states'; import arrayHasItems from '../../utils/array-has-items'; import { getToolbarStateClass } from '../../utils/relatable-state-classes'; import { IWithSortingInstance, withSorting } from '../../add-ons'; import { getRelatableAction } from '../../utils/relatable-actions'; import { columnHasAction } from '../../utils/column-actions'; import { ToolbarPopup } from './toolbar-popup'; import RelatableIcon from '../relatable-icon'; export default function SortableToolbar() { const { allColumns: columns, state: { sortBy }, onCustomSortChange } = useRelatableStateContext(); const [selectedToolbarAction, setToolbar, clearToolbar] = useRelatableToolbarContext(); const isSorted = arrayHasItems(sortBy); return } selectedToolbarAction={selectedToolbarAction} onClose={clearToolbar}> setToolbar(withSorting.name)}> Sorting {isSorted && } ; } function SortingPopup({ columns, sortBy, onClose, selectedToolbarAction, onCustomSortChange }: any) { return
{arrayHasItems(sortBy) && <> {map(sortBy, ({ id, desc }) => { const column = find(columns, (column) => column.id === id); return ; })} }
; } function SortingForm({ columns, onCustomSortChange, selectedToolbarAction, onClose }: any) { const { availableGlobalActions } = useRelatableStateContext(); const relatableAction = getRelatableAction(availableGlobalActions, selectedToolbarAction.name); const columnsToUse = filter(columns, (column) => relatableAction && columnHasAction(column, relatableAction)); const firstId = get(head(columnsToUse), 'id', undefined); const [selectedSort, setSelectedSort] = useState(SORT_ACTIONS.SORT_DESC); const [selectedColumnId, setSelectedColumnId] = useState(firstId); const selectedColumn = find(columnsToUse, ({ id }) => id === selectedColumnId); const columnOptions = map(filter(columnsToUse, 'canSort'), (column) => ({ key: column.id, value: column.id, text: column.Header, })); const sortOptions = [ { key: SORT_ACTIONS.SORT_DESC, value: SORT_ACTIONS.SORT_DESC, text: 'Descending' }, { key: SORT_ACTIONS.SORT_ASC, value: SORT_ACTIONS.SORT_ASC, text: 'Ascending' }, ]; const onSubmit = useCallback(() => { onClose(); onCustomSortChange(selectedColumn, selectedSort); }, [onCustomSortChange, selectedColumn, selectedSort]); return
setSelectedColumnId(value)}/> setSelectedSort(value)}/>
; }