-
-
Notifications
You must be signed in to change notification settings - Fork 622
docs: example sortable columns #1415
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
Open
FatahChan
wants to merge
5
commits into
react-component:master
Choose a base branch
from
FatahChan:docs/example-sortable-columns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
70e42aa
refactor: convert Demo component to functional component and optimize…
FatahChan 3a5240e
feat: add sortable column demo with DnD functionality and update depe…
FatahChan e3fe784
fix: update column sorting logic to use dataIndex instead of key for …
FatahChan acc7f7a
fix: update column resizing and sortable demo to correctly use dataIn…
FatahChan 71e3873
fix: update column sortable demo to directly use dataIndex for header…
FatahChan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| title: column-sortable | ||
| nav: | ||
| title: Demo | ||
| path: /demo | ||
| --- | ||
|
|
||
| <code src="../examples/column-sortable.tsx"></code> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,71 @@ | ||
| import React from 'react'; | ||
| import { Resizable } from 'react-resizable'; | ||
| import React, { useMemo, useState } from 'react'; | ||
| import type { ColumnType, ColumnsType } from 'rc-table'; | ||
| import Table from 'rc-table'; | ||
| import { Resizable } from 'react-resizable'; | ||
| import '../../assets/index.less'; | ||
| import 'react-resizable/css/styles.css'; | ||
| import type { ColumnType } from '@/interface'; | ||
|
|
||
| const ResizableTitle = props => { | ||
| const { onResize, width, ...restProps } = props; | ||
|
|
||
| if (!width) { | ||
| return <th {...restProps} />; | ||
| } | ||
|
|
||
| return ( | ||
| <Resizable width={width} height={0} onResize={onResize}> | ||
| <th {...restProps} /> | ||
| </Resizable> | ||
| ); | ||
| }; | ||
|
|
||
| interface RecordType { | ||
| a: string; | ||
| b?: string; | ||
| c?: string; | ||
| d?: number; | ||
| key: string; | ||
| } | ||
| const data = [ | ||
| { a: '123', key: '1' }, | ||
| { a: 'cdd', b: 'edd', key: '2' }, | ||
| { a: '1333', c: 'eee', d: 2, key: '3' }, | ||
| ] as const; | ||
|
|
||
| interface DemoState { | ||
| columns: ColumnType<RecordType>[]; | ||
| } | ||
| type RecordType = (typeof data)[number]; | ||
|
|
||
| class Demo extends React.Component<{}, DemoState> { | ||
| state: DemoState = { | ||
| columns: [ | ||
| { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, | ||
| { title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, | ||
| { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, | ||
| { | ||
| title: 'Operations', | ||
| dataIndex: '', | ||
| key: 'd', | ||
| render() { | ||
| return <a href="#">Operations</a>; | ||
| }, | ||
| const Demo = () => { | ||
| const [columns, setColumns] = useState<ColumnsType<RecordType>>([ | ||
| { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, | ||
| { title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, | ||
| { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, | ||
| { | ||
| title: 'Operations', | ||
| dataIndex: 'd', | ||
| key: 'd', | ||
| render() { | ||
| return <a href="#">Operations</a>; | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| components = { | ||
| header: { | ||
| cell: ResizableTitle, | ||
| }, | ||
| }; | ||
|
|
||
| data = [ | ||
| { a: '123', key: '1' }, | ||
| { a: 'cdd', b: 'edd', key: '2' }, | ||
| { a: '1333', c: 'eee', d: 2, key: '3' }, | ||
| ]; | ||
|
|
||
| handleResize = | ||
| index => | ||
| (e, { size }) => { | ||
| this.setState(({ columns }) => { | ||
| const nextColumns = [...columns]; | ||
| nextColumns[index] = { | ||
| ...nextColumns[index], | ||
| width: size.width, | ||
| }; | ||
| return { columns: nextColumns }; | ||
| }); | ||
| ]); | ||
| const handleResize = useMemo(() => { | ||
| return (index: number) => { | ||
| return (_: React.MouseEvent<HTMLTableCellElement>, { size }: { size: { width: number } }) => { | ||
| setColumns(prevColumns => | ||
| prevColumns.map((col, i) => (i === index ? { ...col, width: size.width } : col)), | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
| render() { | ||
| const columns = this.state.columns.map((col, index) => ({ | ||
| }, []); | ||
| const columnsWithResizable = useMemo(() => { | ||
| return columns.map((col, index) => ({ | ||
| ...col, | ||
| onHeaderCell: (column: ColumnType<RecordType>) => | ||
| ({ | ||
| width: column.width, | ||
| onResize: this.handleResize(index), | ||
| }) as any, | ||
| onHeaderCell: (column: ColumnType<RecordType>) => ({ | ||
| width: column.width, | ||
| onResize: handleResize(index), | ||
| }), | ||
| })); | ||
| }, [columns, handleResize]); | ||
|
|
||
| return ( | ||
| <div> | ||
| <h2>Integrate with react-resizable</h2> | ||
| <Table components={this.components} columns={columns} data={this.data} /> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
| const tableProps = useMemo(() => { | ||
| return { | ||
| components: { header: { cell: ResizableTitle } }, | ||
| columns: columnsWithResizable, | ||
| data, | ||
| }; | ||
| }, [columnsWithResizable]); | ||
| return <Table {...tableProps} />; | ||
| }; | ||
|
|
||
| export default Demo; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import type { DragEndEvent } from '@dnd-kit/core'; | ||
| import { DndContext, closestCenter } from '@dnd-kit/core'; | ||
| import { restrictToHorizontalAxis } from '@dnd-kit/modifiers'; | ||
| import { SortableContext, arrayMove, rectSwappingStrategy, useSortable } from '@dnd-kit/sortable'; | ||
| import { CSS } from '@dnd-kit/utilities'; | ||
| import Table from 'rc-table'; | ||
| import React, { useCallback, useMemo, useState } from 'react'; | ||
| import 'react-resizable/css/styles.css'; | ||
| import '../../assets/index.less'; | ||
| const SortableHeaderCell = props => { | ||
| const { style: styleProps, ...restProps } = props; | ||
|
|
||
| const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ | ||
| id: props.id as string, | ||
| }); | ||
|
|
||
| const style = { | ||
| ...styleProps, | ||
| transform: CSS.Transform.toString(transform), | ||
| transition, | ||
| cursor: 'move', | ||
| }; | ||
|
|
||
| return <th ref={setNodeRef} style={style} {...attributes} {...listeners} {...restProps} />; | ||
| }; | ||
|
|
||
| const data = [ | ||
| { a: '123', key: '1' }, | ||
| { a: 'cdd', b: 'edd', key: '2' }, | ||
| { a: '1333', c: 'eee', d: 2, key: '3' }, | ||
| ] as const; | ||
|
|
||
| const Demo = () => { | ||
| const [columns, setColumns] = useState([ | ||
| { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, | ||
| { title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, | ||
| { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, | ||
| { | ||
| title: 'Operations', | ||
| dataIndex: 'd', | ||
| key: 'd', | ||
| render() { | ||
| return <a href="#">Operations</a>; | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| const columnsWithSortable = useMemo(() => { | ||
| return columns.map(col => ({ | ||
| ...col, | ||
| onHeaderCell: () => ({ | ||
| id: col.dataIndex, | ||
| }), | ||
| })); | ||
| }, [columns]); | ||
|
|
||
| const tableProps = useMemo(() => { | ||
| return { | ||
| components: { header: { cell: SortableHeaderCell } }, | ||
| columns: columnsWithSortable, | ||
| data, | ||
| }; | ||
| }, [columnsWithSortable]); | ||
| const handleDragEnd = useCallback((e: DragEndEvent) => { | ||
| const { active, over } = e; | ||
| if (over) { | ||
| setColumns(prevColumns => { | ||
| const activeIndex = prevColumns.findIndex(col => col.dataIndex === active.id); | ||
| const overIndex = prevColumns.findIndex(col => col.dataIndex === over.id); | ||
| if (activeIndex === -1 || overIndex === -1) { | ||
| return prevColumns; | ||
| } | ||
| return arrayMove([...prevColumns], activeIndex, overIndex); | ||
| }); | ||
| } | ||
| }, []); | ||
| return ( | ||
| <DndContext | ||
| collisionDetection={closestCenter} | ||
| onDragEnd={handleDragEnd} | ||
| modifiers={[restrictToHorizontalAxis]} | ||
| > | ||
| <SortableContext items={columns.map(col => col.dataIndex)} strategy={rectSwappingStrategy}> | ||
| <Table {...tableProps} />; | ||
| </SortableContext> | ||
| </DndContext> | ||
| ); | ||
| }; | ||
|
|
||
| export default Demo; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.