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

Feature: Add columnResizeDirection to support RTL column resizing #5192

Merged
merged 1 commit into from
Dec 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/api/features/column-sizing.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ columnResizeMode?: 'onChange' | 'onEnd'

Determines when the columnSizing state is updated. `onChange` updates the state when the user is dragging the resize handle. `onEnd` updates the state when the user releases the resize handle.

### `columnResizeDirection`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt there a global setting to rtl, that we can depend on instead of allowing rtl specifically for column resizing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In another PR, I have introduced a global option rtl that could be used in other places.

But, this is the only feature that requires an explicit rtl setting. Other things can be sorted using CSS properties.

Therefore, I thought it would be more appropriate to make this a feature-specific option.


```tsx
columnResizeDirection?: 'ltr' | 'rtl'
```

Enables or disables right-to-left support for resizing the column. defaults to 'ltr'.

### `onColumnSizingChange`

```tsx
Expand Down
14 changes: 12 additions & 2 deletions packages/table-core/src/features/ColumnSizing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface ColumnSizingInfoState {

export type ColumnResizeMode = 'onChange' | 'onEnd'

export type ColumnResizeDirection = 'ltr' | 'rtl'

export interface ColumnSizingOptions {
/**
* Determines when the columnSizing state is updated. `onChange` updates the state when the user is dragging the resize handle. `onEnd` updates the state when the user releases the resize handle.
Expand All @@ -36,6 +38,12 @@ export interface ColumnSizingOptions {
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
enableColumnResizing?: boolean
/**
* Enables or disables right-to-left support for resizing the column. defaults to 'ltr'.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#rtl)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing)
*/
columnResizeDirection?: ColumnResizeDirection
/**
* If provided, this function will be called with an `updaterFn` when `state.columnSizing` changes. This overrides the default internal state management, so you will also need to supply `state.columnSizing` from your own managed state.
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#oncolumnsizingchange)
Expand All @@ -52,7 +60,7 @@ export interface ColumnSizingOptions {

export type ColumnSizingDefaultOptions = Pick<
ColumnSizingOptions,
'columnResizeMode' | 'onColumnSizingChange' | 'onColumnSizingInfoChange'
'columnResizeMode' | 'onColumnSizingChange' | 'onColumnSizingInfoChange' | 'columnResizeDirection'
>

export interface ColumnSizingInstance {
Expand Down Expand Up @@ -225,6 +233,7 @@ export const ColumnSizing: TableFeature = {
): ColumnSizingDefaultOptions => {
return {
columnResizeMode: 'onEnd',
columnResizeDirection: 'ltr',
onColumnSizingChange: makeStateUpdater('columnSizing', table),
onColumnSizingInfoChange: makeStateUpdater('columnSizingInfo', table),
}
Expand Down Expand Up @@ -346,7 +355,8 @@ export const ColumnSizing: TableFeature = {
}

table.setColumnSizingInfo(old => {
const deltaOffset = clientXPos - (old?.startOffset ?? 0)
const deltaDirection = table.options.columnResizeDirection === 'rtl' ? -1 : 1
const deltaOffset = (clientXPos - (old?.startOffset ?? 0)) * deltaDirection
const deltaPercentage = Math.max(
deltaOffset / (old?.startSize ?? 0),
-0.999999
Expand Down