@yorkjs/hive-ui 2.1.6 → 2.1.8

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorkjs/hive-ui",
3
- "version": "2.1.6",
3
+ "version": "2.1.8",
4
4
  "description": "Hive UI - Taro React component library",
5
5
  "main": "src/index.ts",
6
6
  "module": "src/index.ts",
@@ -0,0 +1,57 @@
1
+ @import '../../styles/mixin.styl'
2
+
3
+ .table
4
+ overflow hidden
5
+ border-radius 4PX
6
+ position relative
7
+ halfBorder(top left right bottom, var(--lineBorder), 8PX)
8
+ &::before
9
+ z-index 2
10
+
11
+ .table-header
12
+ display flex
13
+ align-items stretch
14
+ background var(--containerInner)
15
+ position relative
16
+ z-index 1
17
+ halfBorder(bottom, var(--lineBorder))
18
+ &::before
19
+ z-index 1
20
+
21
+ .table-body
22
+ position relative
23
+
24
+ .table-row
25
+ display flex
26
+ align-items stretch
27
+ background var(--containerCard)
28
+ position relative
29
+ z-index 1
30
+ &:not(:last-child)
31
+ halfBorder(bottom, var(--lineBorder))
32
+ &:not(:last-child)::before
33
+ z-index 1
34
+
35
+ .table-cell
36
+ display flex
37
+ padding 6PX 10PX
38
+ box-sizing border-box
39
+ min-width 0
40
+ &:not(:last-child)
41
+ halfBorder(right, var(--lineBorder))
42
+
43
+ .table-header-text
44
+ width 100%
45
+ font-size 14PX
46
+ color var(--textTitle)
47
+ line-height 18PX
48
+ white-space nowrap
49
+ overflow hidden
50
+ text-overflow ellipsis
51
+
52
+ .table-cell-text
53
+ width 100%
54
+ font-size 13PX
55
+ color var(--textTitle)
56
+ line-height 18PX
57
+ lineClamp(1)
@@ -0,0 +1,166 @@
1
+ import React from 'react'
2
+ import { View, Text } from '@tarojs/components'
3
+
4
+ import { formatClassNames } from '../../util/function'
5
+
6
+ import styles from './index.module.styl'
7
+
8
+ export interface TableColumn<T extends object> {
9
+ /** 对应数据字段;同时作为列标识 */
10
+ dataIndex: keyof T
11
+ /** 列标题,支持自定义节点 */
12
+ title: React.ReactNode
13
+ /** 列宽度;数字按 PX 处理,字符串原样使用 */
14
+ width?: number | string
15
+ /**
16
+ * 自定义单元格渲染
17
+ * @param value 当前单元格对应的字段值
18
+ * @param record 当前行数据
19
+ * @param index 当前行索引
20
+ */
21
+ render?: (value: T[keyof T], record: T, index: number) => React.ReactNode
22
+ }
23
+
24
+ export interface TableProps<T extends object> {
25
+ /** 列配置 */
26
+ columns: TableColumn<T>[]
27
+ /** 表格数据源 */
28
+ dataSource: T[]
29
+ /** 行唯一标识对应的数据字段 */
30
+ rowKey: keyof T
31
+ /** 自定义类名 */
32
+ className?: string
33
+ /** 自定义样式 */
34
+ style?: React.CSSProperties
35
+ }
36
+
37
+ /**
38
+ * 格式化列宽;数字转为带 PX 的字符串
39
+ */
40
+ function formatWidth(width?: number | string) {
41
+ if (width == null) {
42
+ return undefined
43
+ }
44
+
45
+ return typeof width === 'number' ? `${width}PX` : width
46
+ }
47
+
48
+ /**
49
+ * 根据列宽生成单元格 flex 样式;未指定宽度时均分剩余空间
50
+ */
51
+ function getCellStyle(width?: number | string): React.CSSProperties {
52
+ const formattedWidth = formatWidth(width)
53
+
54
+ if (formattedWidth) {
55
+ return { flex: `0 0 ${formattedWidth}` }
56
+ }
57
+
58
+ return { flex: 1 }
59
+ }
60
+
61
+ /**
62
+ * 获取行的唯一标识;取不到有效值时回退为行索引
63
+ */
64
+ function getRowKey<T extends object>(
65
+ record: T,
66
+ index: number,
67
+ rowKey: keyof T,
68
+ ): string | number {
69
+ const key = record[rowKey]
70
+
71
+ if (key == null) {
72
+ return index
73
+ }
74
+
75
+ if (typeof key === 'string' || typeof key === 'number') {
76
+ return key
77
+ }
78
+
79
+ return String(key)
80
+ }
81
+
82
+ /**
83
+ * 渲染文本节点;字符串/数字包一层 Text,其它节点原样返回
84
+ */
85
+ function renderText(content: React.ReactNode, className: string) {
86
+ if (typeof content === 'string' || typeof content === 'number') {
87
+ return (
88
+ <Text className={className}>
89
+ {content}
90
+ </Text>
91
+ )
92
+ }
93
+
94
+ return content
95
+ }
96
+
97
+ function Table<T extends object>(props: TableProps<T>) {
98
+ const {
99
+ dataSource,
100
+ columns,
101
+ rowKey,
102
+ className,
103
+ style,
104
+ } = props
105
+
106
+ if (!columns.length) {
107
+ return null
108
+ }
109
+
110
+ return (
111
+ <View
112
+ className={formatClassNames(styles['table'], className)}
113
+ style={style}
114
+ >
115
+ <View className={styles['table-header']}>
116
+ {
117
+ columns.map(column => (
118
+ <View
119
+ key={String(column.dataIndex)}
120
+ className={styles['table-cell']}
121
+ style={getCellStyle(column.width)}
122
+ >
123
+ {renderText(column.title, styles['table-header-text'])}
124
+ </View>
125
+ ))
126
+ }
127
+ </View>
128
+
129
+ <View className={styles['table-body']}>
130
+ {
131
+ dataSource.map((record, rowIndex) => {
132
+ const currentRowKey = getRowKey(record, rowIndex, rowKey)
133
+
134
+ return (
135
+ <View
136
+ key={currentRowKey}
137
+ className={styles['table-row']}
138
+ >
139
+ {
140
+ columns.map(column => {
141
+ const value = record[column.dataIndex]
142
+ const content = column.render
143
+ ? column.render(value, record, rowIndex)
144
+ : value
145
+
146
+ return (
147
+ <View
148
+ key={`${currentRowKey}-${String(column.dataIndex)}`}
149
+ className={styles['table-cell']}
150
+ style={getCellStyle(column.width)}
151
+ >
152
+ {renderText(content as React.ReactNode, styles['table-cell-text'])}
153
+ </View>
154
+ )
155
+ })
156
+ }
157
+ </View>
158
+ )
159
+ })
160
+ }
161
+ </View>
162
+ </View>
163
+ )
164
+ }
165
+
166
+ export default React.memo(Table) as typeof Table
package/src/index.ts CHANGED
@@ -17,4 +17,10 @@ export { default as SimpleModal } from './components/SimpleModal'
17
17
 
18
18
  export { default as FlowItem } from './item/FlowItem'
19
19
  export { default as ListItem } from './item/ListItem'
20
- export { default as ProductItem, setProductItemConfig } from './item/ProductItem'
20
+ export { default as ProductItem, setProductItemConfig } from './item/ProductItem'
21
+
22
+ export {
23
+ default as Table,
24
+ type TableProps,
25
+ type TableColumn,
26
+ } from './components/Table'