Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AnalyticalTable): add onSort callback to Table #123

Merged
merged 1 commit into from
Sep 17, 2019
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { CSSProperties, FC, ReactNode, RefObject, useCallback, useRef } from 'react';
import { Ui5PopoverDomRef } from '../../../interfaces/Ui5PopoverDomRef';
import { Event } from '@ui5/webcomponents-react-base';
import { CustomListItem } from '@ui5/webcomponents-react/lib/CustomListItem';
import { FlexBox } from '@ui5/webcomponents-react/lib/FlexBox';
import { FlexBoxAlignItems } from '@ui5/webcomponents-react/lib/FlexBoxAlignItems';
Expand All @@ -19,10 +20,11 @@ export interface ColumnHeaderModalProperties {
showGroup?: boolean;
column: ColumnType;
style: CSSProperties;
onSort?: (e: Event) => void;
}

export const ColumnHeaderModal: FC<ColumnHeaderModalProperties> = (props) => {
const { showGroup, showSort, showFilter, column, style, openBy } = props;
const { showGroup, showSort, showFilter, column, style, openBy, onSort } = props;

const { Filter } = column;

Expand All @@ -35,9 +37,25 @@ export const ColumnHeaderModal: FC<ColumnHeaderModalProperties> = (props) => {
switch (sortType) {
case 'asc':
column.toggleSortBy(false);
if (typeof onSort === 'function') {
onSort(
Event.of(null, e, {
column,
sortDirection: sortType
})
);
}
break;
case 'desc':
column.toggleSortBy(true);
if (typeof onSort === 'function') {
onSort(
Event.of(null, e, {
column,
sortDirection: sortType
})
);
}
break;
case 'group':
column.toggleGroupBy(!column.isGrouped);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ColumnHeaderProps {
sortable: boolean;
filterable: boolean;
sticky?: boolean;
onSort?: (e: Event) => void;
}

const styles = ({ parameters }: JSSTheme) => ({
Expand Down Expand Up @@ -56,7 +57,7 @@ const useStyles = createUseStyles<JSSTheme, keyof ReturnType<typeof styles>>(sty
export const ColumnHeader: FC<ColumnHeaderProps> = (props) => {
const classes = useStyles(props);

const { children, column, className, style, groupable, sortable, filterable, sticky } = props;
const { children, column, className, style, groupable, sortable, filterable, sticky, onSort } = props;

const openBy = useMemo(() => {
if (!column) return null;
Expand Down Expand Up @@ -103,6 +104,7 @@ export const ColumnHeader: FC<ColumnHeaderProps> = (props) => {
showSort={sortable}
column={column}
style={style}
onSort={onSort}
/>
) : (
openBy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const defaultStory = () => {
selectable={boolean('selectable', true)}
style={{ height: '600px', overflowY: 'auto', paddingRight: '12px' }}
onRowSelected={action('onRowSelected')}
onSort={action('onSort')}
/>
);
};
Expand Down
10 changes: 7 additions & 3 deletions packages/main/src/components/AnalyticalTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface TableProps extends CommonProps {
NoDataComponent?: ReactComponentLike;
noDataText?: string;
stickyHeader?: boolean;
onSort?: (e?: Event) => void;
}

const useStyles = createUseStyles<JSSTheme, keyof ReturnType<typeof styles>>(styles);
Expand Down Expand Up @@ -93,7 +94,8 @@ export const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, re
noDataText,
selectable,
onRowSelected,
stickyHeader
stickyHeader,
onSort
} = props;

const [selectedRow, setSelectedRow] = useState(null);
Expand Down Expand Up @@ -207,12 +209,13 @@ export const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, re

const onRowClicked = useCallback(
(row) => (e) => {
setSelectedRow(row.getRowProps().key);
const newKey = row.getRowProps().key;
setSelectedRow(selectedRow === newKey ? null : newKey);
if (typeof onRowSelected === 'function') {
onRowSelected(Event.of(null, e, { row }));
}
},
[]
[selectedRow]
);

const tableBodyClasses = StyleClassHelper.of(classes.tbody);
Expand All @@ -239,6 +242,7 @@ export const AnalyticalTable: FC<TableProps> = forwardRef((props: TableProps, re
sortable={sortable}
filterable={filterable}
sticky={stickyHeader}
onSort={onSort}
>
{column.render('Header')}
</ColumnHeader>
Expand Down