@gm-pc/keyboard 1.25.0-beta.0 → 1.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/cell/auto_complete.tsx +79 -79
- package/src/cell/cell.tsx +27 -27
- package/src/cell/date_picker.tsx +73 -73
- package/src/cell/input.tsx +75 -75
- package/src/cell/input_number.tsx +92 -92
- package/src/cell/level_select.tsx +70 -70
- package/src/cell/more_select.tsx +83 -83
- package/src/cell/select.tsx +70 -70
- package/src/cell/table_select.tsx +70 -70
- package/src/core/cell.tsx +96 -96
- package/src/core/wrap.tsx +253 -253
- package/src/hoc/keyboard_table_x.tsx +121 -121
- package/src/index.ts +22 -22
- package/src/stories.tsx +308 -308
- package/src/types.ts +87 -87
- package/src/utils/constant.ts +21 -21
- package/src/utils/context.ts +4 -4
- package/src/utils/do_focus.ts +14 -14
- package/src/utils/get_column_key.ts +11 -11
- package/src/utils/get_key.ts +8 -8
- package/src/utils/index.ts +8 -8
- package/src/utils/is_input_un_boundary.ts +29 -29
- package/src/utils/scroll_into_view_fixed_width.ts +71 -71
- package/src/utils/use_context_data.ts +12 -12
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import React, { ComponentType, FC, useMemo } from 'react'
|
|
2
|
-
import { TableXCellProps, TableXProps } from '@gm-pc/table-x'
|
|
3
|
-
import { devWarnForHook } from '@gm-common/tool'
|
|
4
|
-
|
|
5
|
-
import { KeyboardTableXColumn, KeyboardTableXProps } from '../types'
|
|
6
|
-
import { getColumnKey, CellKeyContext } from '../utils'
|
|
7
|
-
import Wrap from '../core/wrap'
|
|
8
|
-
/**
|
|
9
|
-
* 请使用Table并配置isKeyboard
|
|
10
|
-
* @deprecated
|
|
11
|
-
*/
|
|
12
|
-
function keyboardTableXHOC<Props extends TableXProps = TableXProps>(
|
|
13
|
-
Table: ComponentType<Props>
|
|
14
|
-
) {
|
|
15
|
-
/**
|
|
16
|
-
* 要求 props 是 id 和 onAddRow。
|
|
17
|
-
* and column 需要标志 isKeyboard,同时需要 accessor or id
|
|
18
|
-
* and 如果是 fixed,则需要提供 width,focus 的时候如果在 fixed 遮挡则需要滚动到可视区域,这时候就要用到 width 了
|
|
19
|
-
* */
|
|
20
|
-
const KeyboardTableX: FC<Props & KeyboardTableXProps> = ({
|
|
21
|
-
id,
|
|
22
|
-
onAddRow,
|
|
23
|
-
onBeforeDispatch,
|
|
24
|
-
allowAddRowOnDownKey = true,
|
|
25
|
-
...tableProps
|
|
26
|
-
}) => {
|
|
27
|
-
const { data, columns } = tableProps
|
|
28
|
-
|
|
29
|
-
// 检测下 columns
|
|
30
|
-
// 需要提供能够 accessor or id
|
|
31
|
-
// 用 isKeyboard 也必要会用到了 Cell
|
|
32
|
-
devWarnForHook(() => {
|
|
33
|
-
columns.forEach((column: KeyboardTableXColumn) => {
|
|
34
|
-
if (column.isKeyboard && column.show !== false) {
|
|
35
|
-
if (getColumnKey(column) === null) {
|
|
36
|
-
console.error('column need accessor or id', column)
|
|
37
|
-
} else if (!column.Cell) {
|
|
38
|
-
console.error('column need Cell', column)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (column.fixed && !column.width) {
|
|
43
|
-
console.error('column fixed need width', column)
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
// Cell 会产生新组建,所以需要 useMemo
|
|
49
|
-
const { columnKeys, newColumns } = useMemo(() => {
|
|
50
|
-
const columnKeys: string[] = []
|
|
51
|
-
const newColumns = columns.map((column: KeyboardTableXColumn) => {
|
|
52
|
-
// 非全键盘有效列
|
|
53
|
-
if (!(column.isKeyboard && column.show !== false)) {
|
|
54
|
-
return column
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const columnKey: string = getColumnKey(column) as string
|
|
58
|
-
columnKeys.push(columnKey)
|
|
59
|
-
|
|
60
|
-
const oldCell = column.Cell
|
|
61
|
-
|
|
62
|
-
// Cell 是个方法
|
|
63
|
-
// 用 <Cell {...cellProps}/> 会导致重新渲染组件,不知道为什么
|
|
64
|
-
|
|
65
|
-
return {
|
|
66
|
-
...column,
|
|
67
|
-
Cell: (cellProps: TableXCellProps) => (
|
|
68
|
-
<CellKeyContext.Provider value={`${cellProps.row.index}_${columnKey}`}>
|
|
69
|
-
{oldCell && oldCell(cellProps)}
|
|
70
|
-
</CellKeyContext.Provider>
|
|
71
|
-
),
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
return { columnKeys, newColumns }
|
|
75
|
-
}, [columns])
|
|
76
|
-
|
|
77
|
-
// fix hoc 带来的问题
|
|
78
|
-
let leftFixedWidth = 0
|
|
79
|
-
let rightFixedWidth = 0
|
|
80
|
-
// useMemo(() => {
|
|
81
|
-
// columns.forEach((column: KeyboardTableXColumn) => {
|
|
82
|
-
// if (column.show !== false) {
|
|
83
|
-
// if (column.fixed === 'left' && column.width) {
|
|
84
|
-
// leftFixedWidth += column.width as number
|
|
85
|
-
// } else if (column.fixed === 'right' && column.width) {
|
|
86
|
-
// rightFixedWidth += column.width as number
|
|
87
|
-
// }
|
|
88
|
-
// }
|
|
89
|
-
// })
|
|
90
|
-
// }, [columns])
|
|
91
|
-
|
|
92
|
-
// 这里缓存的话,会导致keyboardleft时滚动条无法跟随焦点走
|
|
93
|
-
columns.forEach((column: KeyboardTableXColumn) => {
|
|
94
|
-
if (column.show !== false) {
|
|
95
|
-
if (column.fixed === 'left' && column.width) {
|
|
96
|
-
leftFixedWidth += column.width as number
|
|
97
|
-
} else if (column.fixed === 'right' && column.width) {
|
|
98
|
-
rightFixedWidth += column.width as number
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
return (
|
|
104
|
-
<Wrap
|
|
105
|
-
id={id}
|
|
106
|
-
columnKeys={columnKeys}
|
|
107
|
-
fixedWidths={{ leftFixedWidth, rightFixedWidth }}
|
|
108
|
-
dataLength={data.length}
|
|
109
|
-
onAddRow={onAddRow}
|
|
110
|
-
allowAddRowOnDownKey={allowAddRowOnDownKey}
|
|
111
|
-
onBeforeDispatch={onBeforeDispatch}
|
|
112
|
-
>
|
|
113
|
-
<Table {...(tableProps as Props)} id={id} columns={newColumns} />
|
|
114
|
-
</Wrap>
|
|
115
|
-
)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return KeyboardTableX
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export default keyboardTableXHOC
|
|
1
|
+
import React, { ComponentType, FC, useMemo } from 'react'
|
|
2
|
+
import { TableXCellProps, TableXProps } from '@gm-pc/table-x'
|
|
3
|
+
import { devWarnForHook } from '@gm-common/tool'
|
|
4
|
+
|
|
5
|
+
import { KeyboardTableXColumn, KeyboardTableXProps } from '../types'
|
|
6
|
+
import { getColumnKey, CellKeyContext } from '../utils'
|
|
7
|
+
import Wrap from '../core/wrap'
|
|
8
|
+
/**
|
|
9
|
+
* 请使用Table并配置isKeyboard
|
|
10
|
+
* @deprecated
|
|
11
|
+
*/
|
|
12
|
+
function keyboardTableXHOC<Props extends TableXProps = TableXProps>(
|
|
13
|
+
Table: ComponentType<Props>
|
|
14
|
+
) {
|
|
15
|
+
/**
|
|
16
|
+
* 要求 props 是 id 和 onAddRow。
|
|
17
|
+
* and column 需要标志 isKeyboard,同时需要 accessor or id
|
|
18
|
+
* and 如果是 fixed,则需要提供 width,focus 的时候如果在 fixed 遮挡则需要滚动到可视区域,这时候就要用到 width 了
|
|
19
|
+
* */
|
|
20
|
+
const KeyboardTableX: FC<Props & KeyboardTableXProps> = ({
|
|
21
|
+
id,
|
|
22
|
+
onAddRow,
|
|
23
|
+
onBeforeDispatch,
|
|
24
|
+
allowAddRowOnDownKey = true,
|
|
25
|
+
...tableProps
|
|
26
|
+
}) => {
|
|
27
|
+
const { data, columns } = tableProps
|
|
28
|
+
|
|
29
|
+
// 检测下 columns
|
|
30
|
+
// 需要提供能够 accessor or id
|
|
31
|
+
// 用 isKeyboard 也必要会用到了 Cell
|
|
32
|
+
devWarnForHook(() => {
|
|
33
|
+
columns.forEach((column: KeyboardTableXColumn) => {
|
|
34
|
+
if (column.isKeyboard && column.show !== false) {
|
|
35
|
+
if (getColumnKey(column) === null) {
|
|
36
|
+
console.error('column need accessor or id', column)
|
|
37
|
+
} else if (!column.Cell) {
|
|
38
|
+
console.error('column need Cell', column)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (column.fixed && !column.width) {
|
|
43
|
+
console.error('column fixed need width', column)
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
// Cell 会产生新组建,所以需要 useMemo
|
|
49
|
+
const { columnKeys, newColumns } = useMemo(() => {
|
|
50
|
+
const columnKeys: string[] = []
|
|
51
|
+
const newColumns = columns.map((column: KeyboardTableXColumn) => {
|
|
52
|
+
// 非全键盘有效列
|
|
53
|
+
if (!(column.isKeyboard && column.show !== false)) {
|
|
54
|
+
return column
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const columnKey: string = getColumnKey(column) as string
|
|
58
|
+
columnKeys.push(columnKey)
|
|
59
|
+
|
|
60
|
+
const oldCell = column.Cell
|
|
61
|
+
|
|
62
|
+
// Cell 是个方法
|
|
63
|
+
// 用 <Cell {...cellProps}/> 会导致重新渲染组件,不知道为什么
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
...column,
|
|
67
|
+
Cell: (cellProps: TableXCellProps) => (
|
|
68
|
+
<CellKeyContext.Provider value={`${cellProps.row.index}_${columnKey}`}>
|
|
69
|
+
{oldCell && oldCell(cellProps)}
|
|
70
|
+
</CellKeyContext.Provider>
|
|
71
|
+
),
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
return { columnKeys, newColumns }
|
|
75
|
+
}, [columns])
|
|
76
|
+
|
|
77
|
+
// fix hoc 带来的问题
|
|
78
|
+
let leftFixedWidth = 0
|
|
79
|
+
let rightFixedWidth = 0
|
|
80
|
+
// useMemo(() => {
|
|
81
|
+
// columns.forEach((column: KeyboardTableXColumn) => {
|
|
82
|
+
// if (column.show !== false) {
|
|
83
|
+
// if (column.fixed === 'left' && column.width) {
|
|
84
|
+
// leftFixedWidth += column.width as number
|
|
85
|
+
// } else if (column.fixed === 'right' && column.width) {
|
|
86
|
+
// rightFixedWidth += column.width as number
|
|
87
|
+
// }
|
|
88
|
+
// }
|
|
89
|
+
// })
|
|
90
|
+
// }, [columns])
|
|
91
|
+
|
|
92
|
+
// 这里缓存的话,会导致keyboardleft时滚动条无法跟随焦点走
|
|
93
|
+
columns.forEach((column: KeyboardTableXColumn) => {
|
|
94
|
+
if (column.show !== false) {
|
|
95
|
+
if (column.fixed === 'left' && column.width) {
|
|
96
|
+
leftFixedWidth += column.width as number
|
|
97
|
+
} else if (column.fixed === 'right' && column.width) {
|
|
98
|
+
rightFixedWidth += column.width as number
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<Wrap
|
|
105
|
+
id={id}
|
|
106
|
+
columnKeys={columnKeys}
|
|
107
|
+
fixedWidths={{ leftFixedWidth, rightFixedWidth }}
|
|
108
|
+
dataLength={data.length}
|
|
109
|
+
onAddRow={onAddRow}
|
|
110
|
+
allowAddRowOnDownKey={allowAddRowOnDownKey}
|
|
111
|
+
onBeforeDispatch={onBeforeDispatch}
|
|
112
|
+
>
|
|
113
|
+
<Table {...(tableProps as Props)} id={id} columns={newColumns} />
|
|
114
|
+
</Wrap>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return KeyboardTableX
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default keyboardTableXHOC
|
package/src/index.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { isInputUnBoundary, scrollIntoViewFixedWidth, doFocus } from './utils'
|
|
2
|
-
|
|
3
|
-
export const KeyboardUtil = { isInputUnBoundary, scrollIntoViewFixedWidth, doFocus }
|
|
4
|
-
|
|
5
|
-
export { default as keyboardTableXHOC } from './hoc/keyboard_table_x'
|
|
6
|
-
|
|
7
|
-
export { default as KC } from './cell/cell'
|
|
8
|
-
export { default as KCAutoComplete } from './cell/auto_complete'
|
|
9
|
-
export { default as KCInput } from './cell/input'
|
|
10
|
-
export { default as KCMoreSelect } from './cell/more_select'
|
|
11
|
-
export { default as KCInputNumber } from './cell/input_number'
|
|
12
|
-
export { default as KCLevelSelect } from './cell/level_select'
|
|
13
|
-
export { default as KCTableSelect } from './cell/table_select'
|
|
14
|
-
export { default as KCDatePicker } from './cell/date_picker'
|
|
15
|
-
export { default as KCSelect } from './cell/select'
|
|
16
|
-
|
|
17
|
-
export type {
|
|
18
|
-
KeyboardTableXProps,
|
|
19
|
-
KeyboardTableXColumn,
|
|
20
|
-
KeyboardCustomEvent,
|
|
21
|
-
KeyboardDirection,
|
|
22
|
-
} from './types'
|
|
1
|
+
import { isInputUnBoundary, scrollIntoViewFixedWidth, doFocus } from './utils'
|
|
2
|
+
|
|
3
|
+
export const KeyboardUtil = { isInputUnBoundary, scrollIntoViewFixedWidth, doFocus }
|
|
4
|
+
|
|
5
|
+
export { default as keyboardTableXHOC } from './hoc/keyboard_table_x'
|
|
6
|
+
|
|
7
|
+
export { default as KC } from './cell/cell'
|
|
8
|
+
export { default as KCAutoComplete } from './cell/auto_complete'
|
|
9
|
+
export { default as KCInput } from './cell/input'
|
|
10
|
+
export { default as KCMoreSelect } from './cell/more_select'
|
|
11
|
+
export { default as KCInputNumber } from './cell/input_number'
|
|
12
|
+
export { default as KCLevelSelect } from './cell/level_select'
|
|
13
|
+
export { default as KCTableSelect } from './cell/table_select'
|
|
14
|
+
export { default as KCDatePicker } from './cell/date_picker'
|
|
15
|
+
export { default as KCSelect } from './cell/select'
|
|
16
|
+
|
|
17
|
+
export type {
|
|
18
|
+
KeyboardTableXProps,
|
|
19
|
+
KeyboardTableXColumn,
|
|
20
|
+
KeyboardCustomEvent,
|
|
21
|
+
KeyboardDirection,
|
|
22
|
+
} from './types'
|