-
-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sticky header and scroll (#505)
* feat: sticky header and scroll * improve style * refactor, complete test suite * add mouseup test * test if left <= 0 * remove useless px * remove useless code * improve ref value if undefined * fix NaN * rename isShow -> show * Update FixedHeader.tsx * fix example refactor * imporve sticky prop * fix: trackpad scroll sync sticky scroll * fix test fail * imporve sticky * add test * remove useless code * imporve useSticky Co-authored-by: 07akioni <[email protected]>
- Loading branch information
1 parent
659158f
commit 4acc581
Showing
10 changed files
with
554 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,114 @@ | ||
/* eslint-disable no-console,func-names,react/no-multi-comp */ | ||
import React from 'react'; | ||
import Table from '../src'; | ||
import '../assets/index.less'; | ||
import { ColumnType } from '../src/interface'; | ||
|
||
interface RecordType { | ||
a?: string; | ||
b?: string; | ||
c?: string; | ||
} | ||
|
||
const columns: ColumnType<{ a: string; b: string; c: string }>[] = [ | ||
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, | ||
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100, align: 'right' }, | ||
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, | ||
{ | ||
title: 'Operations', | ||
dataIndex: '', | ||
key: 'd', | ||
render(_, record) { | ||
return ( | ||
<a | ||
onClick={e => { | ||
e.preventDefault(); | ||
console.log('Operate on:', record); | ||
}} | ||
href="#" | ||
> | ||
Operations | ||
</a> | ||
); | ||
}, | ||
}, | ||
]; | ||
|
||
const data = [ | ||
{ a: '123', key: '1' }, | ||
{ a: 'cdd', b: 'edd', key: '2' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '3' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '4' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '5' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '6' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '7' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '8' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '9' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '10' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '11' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '12' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '13' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '14' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '15' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '16' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '17' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '18' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '19' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '20' }, | ||
]; | ||
|
||
const Demo = () => ( | ||
<div | ||
style={{ | ||
height: 10000, | ||
}} | ||
> | ||
<h2>Sticky</h2> | ||
<Table<RecordType> | ||
columns={columns} | ||
data={data} | ||
tableLayout="auto" | ||
sticky | ||
scroll={{ | ||
x: 10000, | ||
}} | ||
style={{ | ||
marginBottom: 100, | ||
}} | ||
/> | ||
|
||
<h2>Show offset Header</h2> | ||
<Table<RecordType> | ||
columns={columns} | ||
data={data} | ||
tableLayout="auto" | ||
sticky={{ | ||
offsetHeader: 100, | ||
}} | ||
scroll={{ | ||
x: 10000, | ||
}} | ||
style={{ | ||
marginBottom: 100, | ||
}} | ||
/> | ||
|
||
<h2>Show offset scroll</h2> | ||
<Table<RecordType> | ||
columns={columns} | ||
data={data} | ||
tableLayout="auto" | ||
sticky={{ | ||
offsetScroll: 100, | ||
}} | ||
scroll={{ | ||
x: 10000, | ||
}} | ||
style={{ | ||
marginBottom: 100, | ||
}} | ||
/> | ||
</div> | ||
); | ||
|
||
export default Demo; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,22 @@ | ||
import * as React from 'react'; | ||
import { TableSticky } from '../interface'; | ||
|
||
export default function useSticky( | ||
sticky: boolean | TableSticky, | ||
prefixCls: string, | ||
): { | ||
isSticky: boolean; | ||
offsetHeader: number; | ||
offsetScroll: number; | ||
stickyClassName: string; | ||
} { | ||
return React.useMemo(() => { | ||
const isSticky = !!sticky; | ||
return { | ||
isSticky, | ||
stickyClassName: isSticky ? `${prefixCls}-sticky-header` : '', | ||
offsetHeader: typeof sticky === 'object' ? sticky.offsetHeader || 0 : 0, | ||
offsetScroll: typeof sticky === 'object' ? sticky.offsetScroll || 0 : 0, | ||
}; | ||
}, [sticky, prefixCls]); | ||
} |
This file contains 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
Oops, something went wrong.