@yorkjs/hive-ui 2.1.6 → 2.1.7

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.7",
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,170 @@
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
+ key: string
11
+ /** 列标题,支持自定义节点 */
12
+ title: React.ReactNode
13
+ /** 对应数据字段;不传时仅通过 render 展示 */
14
+ dataIndex?: keyof T
15
+ /** 列宽度;数字按 PX 处理,字符串原样使用 */
16
+ width?: number | string
17
+ /**
18
+ * 自定义单元格渲染
19
+ * @param value 当前单元格对应的字段值
20
+ * @param record 当前行数据
21
+ * @param index 当前行索引
22
+ */
23
+ render?: (value: T[keyof T] | undefined, record: T, index: number) => React.ReactNode
24
+ }
25
+
26
+ export interface TableProps<T extends object> {
27
+ /** 列配置 */
28
+ columns: TableColumn<T>[]
29
+ /** 表格数据源 */
30
+ dataSource: T[]
31
+ /** 行唯一标识对应的数据字段 */
32
+ rowKey: keyof T
33
+ /** 自定义类名 */
34
+ className?: string
35
+ /** 自定义样式 */
36
+ style?: React.CSSProperties
37
+ }
38
+
39
+ /**
40
+ * 格式化列宽;数字转为带 PX 的字符串
41
+ */
42
+ function formatWidth(width?: number | string) {
43
+ if (width == null) {
44
+ return undefined
45
+ }
46
+
47
+ return typeof width === 'number' ? `${width}PX` : width
48
+ }
49
+
50
+ /**
51
+ * 根据列宽生成单元格 flex 样式;未指定宽度时均分剩余空间
52
+ */
53
+ function getCellStyle(width?: number | string): React.CSSProperties {
54
+ const formattedWidth = formatWidth(width)
55
+
56
+ if (formattedWidth) {
57
+ return { flex: `0 0 ${formattedWidth}` }
58
+ }
59
+
60
+ return { flex: 1 }
61
+ }
62
+
63
+ /**
64
+ * 获取行的唯一标识;取不到有效值时回退为行索引
65
+ */
66
+ function getRowKey<T extends object>(
67
+ record: T,
68
+ index: number,
69
+ rowKey: keyof T,
70
+ ): string | number {
71
+ const key = record[rowKey]
72
+
73
+ if (key == null) {
74
+ return index
75
+ }
76
+
77
+ if (typeof key === 'string' || typeof key === 'number') {
78
+ return key
79
+ }
80
+
81
+ return String(key)
82
+ }
83
+
84
+ /**
85
+ * 渲染文本节点;字符串/数字包一层 Text,其它节点原样返回
86
+ */
87
+ function renderText(content: React.ReactNode, className: string) {
88
+ if (typeof content === 'string' || typeof content === 'number') {
89
+ return (
90
+ <Text className={className}>
91
+ {content}
92
+ </Text>
93
+ )
94
+ }
95
+
96
+ return content
97
+ }
98
+
99
+ function Table<T extends object>(props: TableProps<T>) {
100
+ const {
101
+ dataSource,
102
+ columns,
103
+ rowKey,
104
+ className,
105
+ style,
106
+ } = props
107
+
108
+ if (!columns.length) {
109
+ return null
110
+ }
111
+
112
+ return (
113
+ <View
114
+ className={formatClassNames(styles['table'], className)}
115
+ style={style}
116
+ >
117
+ <View className={styles['table-header']}>
118
+ {
119
+ columns.map(column => (
120
+ <View
121
+ key={column.key}
122
+ className={styles['table-cell']}
123
+ style={getCellStyle(column.width)}
124
+ >
125
+ {renderText(column.title, styles['table-header-text'])}
126
+ </View>
127
+ ))
128
+ }
129
+ </View>
130
+
131
+ <View className={styles['table-body']}>
132
+ {
133
+ dataSource.map((record, rowIndex) => {
134
+ const currentRowKey = getRowKey(record, rowIndex, rowKey)
135
+
136
+ return (
137
+ <View
138
+ key={currentRowKey}
139
+ className={styles['table-row']}
140
+ >
141
+ {
142
+ columns.map(column => {
143
+ const value = column.dataIndex != null
144
+ ? record[column.dataIndex]
145
+ : undefined
146
+ const content = column.render
147
+ ? column.render(value, record, rowIndex)
148
+ : value
149
+
150
+ return (
151
+ <View
152
+ key={`${currentRowKey}-${column.key}`}
153
+ className={styles['table-cell']}
154
+ style={getCellStyle(column.width)}
155
+ >
156
+ {renderText(content as React.ReactNode, styles['table-cell-text'])}
157
+ </View>
158
+ )
159
+ })
160
+ }
161
+ </View>
162
+ )
163
+ })
164
+ }
165
+ </View>
166
+ </View>
167
+ )
168
+ }
169
+
170
+ 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'