2022-10-03 01:23:59 +03:00
|
|
|
import React, { FC, useCallback, useEffect, useRef } from 'react';
|
2022-08-06 01:36:13 +03:00
|
|
|
import IconButtonElement from '../../elements/IconButtonElement';
|
2022-10-21 21:47:54 +03:00
|
|
|
import { ViewSettingsI } from './interface';
|
2022-08-06 01:36:13 +03:00
|
|
|
|
2022-10-14 02:07:54 +03:00
|
|
|
interface SortProps {
|
2022-08-21 03:09:22 +03:00
|
|
|
getSortMenuOptions: () => {
|
|
|
|
name: string;
|
2022-10-03 01:23:59 +03:00
|
|
|
value: string;
|
2022-10-21 21:47:54 +03:00
|
|
|
}[];
|
|
|
|
viewSettings: ViewSettingsI
|
|
|
|
setViewSettings: React.Dispatch<React.SetStateAction<ViewSettingsI>>;
|
2022-08-06 01:36:13 +03:00
|
|
|
}
|
|
|
|
|
2022-10-21 21:47:54 +03:00
|
|
|
const Sort: FC<SortProps> = ({
|
|
|
|
getSortMenuOptions,
|
|
|
|
viewSettings,
|
|
|
|
setViewSettings
|
|
|
|
}) => {
|
2022-08-06 01:36:13 +03:00
|
|
|
const element = useRef<HTMLDivElement>(null);
|
|
|
|
|
2022-10-03 01:23:59 +03:00
|
|
|
const showSortMenu = useCallback(() => {
|
|
|
|
import('../../components/sortmenu/sortmenu').then(({default: SortMenu}) => {
|
|
|
|
const sortMenu = new SortMenu();
|
|
|
|
sortMenu.show({
|
2022-10-21 21:47:54 +03:00
|
|
|
settings: viewSettings,
|
|
|
|
sortOptions: getSortMenuOptions(),
|
|
|
|
setSortValues: setViewSettings
|
2022-10-03 01:23:59 +03:00
|
|
|
});
|
|
|
|
});
|
2022-10-21 21:47:54 +03:00
|
|
|
}, [getSortMenuOptions, viewSettings, setViewSettings]);
|
2022-10-03 01:23:59 +03:00
|
|
|
|
2022-08-06 01:36:13 +03:00
|
|
|
useEffect(() => {
|
|
|
|
const btnSort = element.current?.querySelector('.btnSort');
|
|
|
|
|
2022-10-03 01:23:59 +03:00
|
|
|
btnSort?.addEventListener('click', showSortMenu);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
btnSort?.removeEventListener('click', showSortMenu);
|
|
|
|
};
|
|
|
|
}, [showSortMenu]);
|
2022-08-06 01:36:13 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={element}>
|
|
|
|
<IconButtonElement
|
|
|
|
is='paper-icon-button-light'
|
|
|
|
className='btnSort autoSize'
|
|
|
|
title='Sort'
|
|
|
|
icon='material-icons sort_by_alpha'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Sort;
|