From 8c54272c058706d81df24d603cc776ede44c3d77 Mon Sep 17 00:00:00 2001 From: MarcusNotheis Date: Wed, 29 Apr 2020 20:07:22 +0200 Subject: [PATCH 1/2] fix(AnalyticalTable): support RTL closes #460 --- .../AnalyticalTable/ColumnHeader/index.tsx | 14 ++---- .../AnalyticalTable/hooks/useRTL.ts | 50 +++++++++++++++++++ .../hooks/useTableCellStyling.ts | 8 +-- .../hooks/useTableHeaderStyling.ts | 4 +- .../src/components/AnalyticalTable/index.tsx | 5 +- .../virtualization/VirtualTableBody.tsx | 2 + 6 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 packages/main/src/components/AnalyticalTable/hooks/useRTL.ts diff --git a/packages/main/src/components/AnalyticalTable/ColumnHeader/index.tsx b/packages/main/src/components/AnalyticalTable/ColumnHeader/index.tsx index 798c90896db..0e3ff4d406b 100644 --- a/packages/main/src/components/AnalyticalTable/ColumnHeader/index.tsx +++ b/packages/main/src/components/AnalyticalTable/ColumnHeader/index.tsx @@ -28,7 +28,6 @@ export interface ColumnHeaderProps { className: string; column: ColumnType; style: CSSProperties; - isLastColumn?: boolean; onSort?: (e: CustomEvent<{ column: unknown; sortDirection: string }>) => void; onGroupBy?: (e: CustomEvent<{ column: unknown; isGrouped: boolean }>) => void; onDragStart: DragEventHandler; @@ -102,7 +101,6 @@ export const ColumnHeader: FC = (props: ColumnHeaderProps) => column, className, style, - isLastColumn, onSort, onGroupBy, onDragEnter, @@ -122,13 +120,12 @@ export const ColumnHeader: FC = (props: ColumnHeaderProps) => const filterIcon = isFiltered ? : null; const groupingIcon = column.isGrouped ? : null; - const isResizable = !isLastColumn && column.canResize; const hasPopover = column.canGroupBy || column.canSort || column.canFilter; const innerStyle: CSSProperties = useMemo(() => { const modifiedStyles: CSSProperties = { cursor: hasPopover ? 'pointer' : 'auto' }; - if (isResizable) { + if (column.canResize) { modifiedStyles.maxWidth = `calc(100% - 16px)`; } if (dragOver) { @@ -140,12 +137,9 @@ export const ColumnHeader: FC = (props: ColumnHeaderProps) => if (column.hAlign === TextAlign.End) { modifiedStyles.justifyContent = 'flex-end'; modifiedStyles.maxWidth = ''; - if (isLastColumn) { - modifiedStyles.paddingRight = `calc(${ThemingParameters.sapScrollBar_Dimension} + 1rem)`; - } } return modifiedStyles; - }, [isResizable, dragOver, hasPopover]); + }, [column.canResize, dragOver, hasPopover]); const popoverRef = useRef(null); @@ -188,8 +182,8 @@ export const ColumnHeader: FC = (props: ColumnHeaderProps) => {hasPopover && } - {column.getResizerProps && ( -
+ {column.canResize && column.getResizerProps && ( +
)}
); diff --git a/packages/main/src/components/AnalyticalTable/hooks/useRTL.ts b/packages/main/src/components/AnalyticalTable/hooks/useRTL.ts new file mode 100644 index 00000000000..f188b575808 --- /dev/null +++ b/packages/main/src/components/AnalyticalTable/hooks/useRTL.ts @@ -0,0 +1,50 @@ +import { getRTL } from '@ui5/webcomponents-base/dist/config/RTL'; +import { ThemingParameters } from '@ui5/webcomponents-react-base/lib/ThemingParameters'; + +const visibleColumns = (currentVisibleColumns) => { + if (getRTL()) { + return currentVisibleColumns.reverse(); + } + return currentVisibleColumns; +}; +const getHeaderProps = (props, { column, instance }) => { + const isRTL = getRTL(); + const lastColumnId = instance.visibleColumns[isRTL ? 0 : instance.visibleColumns.length - 1].id; + const isLastColumn = column.id === lastColumnId; + return [ + props, + { + style: { + paddingLeft: isRTL && isLastColumn ? ThemingParameters.sapScrollBar_Dimension : 0, + paddingRight: !isRTL && isLastColumn ? ThemingParameters.sapScrollBar_Dimension : 0 + } + } + ]; +}; + +const getCellProps = (props, { cell, instance }) => { + const isRTL = getRTL(); + const lastColumnId = instance.visibleColumns[isRTL ? 0 : instance.visibleColumns.length - 1].id; + const isLastColumn = cell.column.id === lastColumnId; + return [ + props, + { + style: { + left: + isRTL && !isLastColumn + ? `calc(${cell.column.totalLeft}px - ${ThemingParameters.sapScrollBar_Dimension})` + : `${cell.column.totalLeft}px`, + width: isLastColumn + ? `calc(${cell.column.totalWidth}px - ${ThemingParameters.sapScrollBar_Dimension})` + : `${cell.column.totalWidth}px` + } + } + ]; +}; + +export const useRTL = (hooks) => { + hooks.visibleColumns.push(visibleColumns); + hooks.getHeaderProps.push(getHeaderProps); + hooks.getCellProps.push(getCellProps); +}; +useRTL.plugiName = 'useRTL'; diff --git a/packages/main/src/components/AnalyticalTable/hooks/useTableCellStyling.ts b/packages/main/src/components/AnalyticalTable/hooks/useTableCellStyling.ts index 87256df0981..e168dc8679a 100644 --- a/packages/main/src/components/AnalyticalTable/hooks/useTableCellStyling.ts +++ b/packages/main/src/components/AnalyticalTable/hooks/useTableCellStyling.ts @@ -1,10 +1,9 @@ -import { ThemingParameters } from '@ui5/webcomponents-react-base/lib/ThemingParameters'; import { TextAlign } from '@ui5/webcomponents-react/lib/TextAlign'; import { VerticalAlign } from '@ui5/webcomponents-react/lib/VerticalAlign'; + import { CSSProperties } from 'react'; const getCellProps = (cellProps, { cell: { column }, instance }) => { - const lastColumnId = instance.visibleColumns[instance.visibleColumns.length - 1]?.id; const columnIndex = instance.visibleColumns.findIndex(({ id }) => id === column.id); const { classes } = instance.webComponentsReactProperties; const style: CSSProperties = {}; @@ -52,11 +51,6 @@ const getCellProps = (cellProps, { cell: { column }, instance }) => { style.padding = 0; } - if (column.id === lastColumnId) { - style.paddingRight = `calc(${ThemingParameters.sapScrollBar_Dimension} + 0.5rem)`; - style.boxSizing = 'border-box'; - style.width = `calc(${cellProps.style.width} - ${ThemingParameters.sapScrollBar_Dimension})`; - } return [ cellProps, { diff --git a/packages/main/src/components/AnalyticalTable/hooks/useTableHeaderStyling.ts b/packages/main/src/components/AnalyticalTable/hooks/useTableHeaderStyling.ts index 7189dcb764d..053ba88d565 100644 --- a/packages/main/src/components/AnalyticalTable/hooks/useTableHeaderStyling.ts +++ b/packages/main/src/components/AnalyticalTable/hooks/useTableHeaderStyling.ts @@ -1,5 +1,3 @@ -import { PluginHook } from 'react-table'; - const getHeaderProps = (columnProps, { instance, column }) => { const { classes } = instance.webComponentsReactProperties; return [ @@ -15,7 +13,7 @@ const getHeaderProps = (columnProps, { instance, column }) => { ]; }; -export const useTableHeaderStyling: PluginHook<{}> = (hooks) => { +export const useTableHeaderStyling = (hooks) => { hooks.getHeaderProps.push(getHeaderProps); }; useTableHeaderStyling.pluginName = 'useTableHeaderStyling'; diff --git a/packages/main/src/components/AnalyticalTable/index.tsx b/packages/main/src/components/AnalyticalTable/index.tsx index 9e5979be687..738a5f3030c 100644 --- a/packages/main/src/components/AnalyticalTable/index.tsx +++ b/packages/main/src/components/AnalyticalTable/index.tsx @@ -44,6 +44,7 @@ import { useDragAndDrop } from './hooks/useDragAndDrop'; import { useDynamicColumnWidths } from './hooks/useDynamicColumnWidths'; import { useRowHighlight } from './hooks/useRowHighlight'; import { useRowSelectionColumn } from './hooks/useRowSelectionColumn'; +import { useRTL } from './hooks/useRTL'; import { useSingleRowStateSelection } from './hooks/useSingleRowStateSelection'; import { useTableCellStyling } from './hooks/useTableCellStyling'; import { useTableHeaderGroupStyling } from './hooks/useTableHeaderGroupStyling'; @@ -247,7 +248,8 @@ const AnalyticalTable: FC = forwardRef((props: TableProps, ref: Ref< useDynamicColumnWidths, useTableCellStyling, useToggleRowExpand, - ...tableHooks + ...tableHooks, + useRTL ); const updateTableClientWidth = useCallback(() => { @@ -378,7 +380,6 @@ const AnalyticalTable: FC = forwardRef((props: TableProps, ref: Ref< // eslint-disable-next-line react/jsx-key { innerRef={innerDivRef} overscanCount={overscan} onScroll={onScroll} + direction={getRTL() ? 'rtl' : 'ltr'} > {VirtualTableRow} From 3907ec804945d115bfa6319705722954be4e069d Mon Sep 17 00:00:00 2001 From: MarcusNotheis Date: Wed, 29 Apr 2020 20:10:11 +0200 Subject: [PATCH 2/2] Update AnalyticalTable.test.tsx.snap --- .../AnalyticalTable.test.tsx.snap | 250 +++++++++--------- 1 file changed, 125 insertions(+), 125 deletions(-) diff --git a/packages/main/src/components/AnalyticalTable/__snapshots__/AnalyticalTable.test.tsx.snap b/packages/main/src/components/AnalyticalTable/__snapshots__/AnalyticalTable.test.tsx.snap index 60714a8ac69..2b7d9f7fb29 100644 --- a/packages/main/src/components/AnalyticalTable/__snapshots__/AnalyticalTable.test.tsx.snap +++ b/packages/main/src/components/AnalyticalTable/__snapshots__/AnalyticalTable.test.tsx.snap @@ -34,7 +34,7 @@ exports[`AnalyticalTable Alternate Row Color 1`] = ` draggable="true" id="name" role="columnheader" - style="position: absolute; top: 0px; left: 0px; width: 150px;" + style="position: absolute; top: 0px; left: 0px; width: 150px; padding-left: 0px; padding-right: 0px;" >
@@ -478,7 +478,7 @@ exports[`AnalyticalTable Alternate Row Color 1`] = ` aria-colindex="4" class="AnalyticalTable-tableCell-0" role="cell" - style="position: absolute; top: 0px; left: 450px; align-items: center; box-sizing: border-box;" + style="position: absolute; top: 0px; left: 450px; align-items: center;" tabindex="-1" />
@@ -513,7 +513,7 @@ exports[`AnalyticalTable Alternate Row Color 1`] = ` aria-colindex="4" class="AnalyticalTable-tableCell-0" role="cell" - style="position: absolute; top: 0px; left: 450px; align-items: center; box-sizing: border-box;" + style="position: absolute; top: 0px; left: 450px; align-items: center;" tabindex="-1" />
@@ -558,7 +558,7 @@ exports[`AnalyticalTable Loading - Loader 1`] = ` draggable="true" id="name" role="columnheader" - style="position: absolute; top: 0px; left: 0px; width: 150px;" + style="position: absolute; top: 0px; left: 0px; width: 150px; padding-left: 0px; padding-right: 0px;" >
@@ -1002,7 +1002,7 @@ exports[`AnalyticalTable Loading - Loader 1`] = ` aria-colindex="4" class="AnalyticalTable-tableCell-0" role="cell" - style="position: absolute; top: 0px; left: 450px; align-items: center; box-sizing: border-box;" + style="position: absolute; top: 0px; left: 450px; align-items: center;" tabindex="-1" />
@@ -1037,7 +1037,7 @@ exports[`AnalyticalTable Loading - Loader 1`] = ` aria-colindex="4" class="AnalyticalTable-tableCell-0" role="cell" - style="position: absolute; top: 0px; left: 450px; align-items: center; box-sizing: border-box;" + style="position: absolute; top: 0px; left: 450px; align-items: center;" tabindex="-1" />
@@ -1082,7 +1082,7 @@ exports[`AnalyticalTable Loading - Placeholder 1`] = ` draggable="true" id="name" role="columnheader" - style="position: absolute; top: 0px; left: 0px; width: 150px;" + style="position: absolute; top: 0px; left: 0px; width: 150px; padding-left: 0px; padding-right: 0px;" >