Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 3x 3x 4x 1x 1x 1x 1x 1x 1x 4x 1x 1x 8x 1x 2x 72x 63x 9x 9x 9x 8x 8x 8x 8x 4x 4x 4x 9x 5x 8x 7x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 4x 4x | import React from "react"; import classNames from "classnames"; import { Icon } from "../../icon"; import { TableAddon, RowRenderContext, TableProps } from "../TableProps"; import { getRowKeyFromRecordKey } from "../util/get-row-key-from-record-key"; import { useMiddleware } from "../util/use-middleware"; import { injectPropsIfTargetNotExisted } from "../util/inject-props-if-target-not-existed"; import { useConfig } from "../../_util/config-context"; /** * `expandable` 插件用于支持表格行展开。 */ export interface ExpandableAddonOptions<Record = any> { /** * 展开的行如何渲染 */ render?(record: Record): React.ReactNode; /** * 将当前记录展开成更多的记录 */ expand?(record: Record): Record[]; /** * 展开行的键值 */ expandedKeys?: string[]; /** * 用户进行展开/收起操作的时候,知会最新的键值 */ onExpandedKeysChange?: ( value: string[], context: { event: React.SyntheticEvent; operateType: "expand" | "collapse"; operateKey: string; operateRecord: Record; } ) => void; /** * 判断记录是否允许展开,如果不提供该判断,则认为所有记录均可展开 */ shouldRecordExpandable?: (record: Record) => boolean; /** * 展开行内容渲染之前,插入多少个间隙单元格 */ gapCell?: number; /** * 可以在插入的间隙单元格中,渲染指定的内容(需要配置 gapCell > 0) */ gapCellRender?: (record: Record) => React.ReactNode; /** * 提供一个列的 `key`,将展开组件插入到一个目标列 * * 默认在最前新建一列插入 */ targetColumnKey?: string; /** * 列宽度,可以指定 CSS 属性或数字 (单位:px) * @default 26 */ width?: string | number; /** * **高级用法** * 更改该插件的在每行的渲染内容,`element` 为默认渲染内容,`context` 中包含该行数据相关信息 * @default x => x */ iconRender?: ( element: JSX.Element, context: RowRenderContext<Record> ) => React.ReactNode; } const fallbackColumnKey = "__expandable_addon__"; let rowDisabled: TableProps["rowDisabled"] = null; export function expandable<Record = any>( options: ExpandableAddonOptions<Record> ): TableAddon<Record> { const { render, expand, shouldRecordExpandable, expandedKeys, onExpandedKeysChange, targetColumnKey, gapCell, gapCellRender, width = 26, iconRender = x => x, } = options; const expandedKeySet = new Set(expandedKeys || []); let getRowKey: ReturnType<typeof getRowKeyFromRecordKey>; let addons: TableAddon[] = []; return { onInjectProps: props => { const { recordKey } = props; getRowKey = getRowKeyFromRecordKey(recordKey); /* eslint-disable prefer-destructuring */ rowDisabled = props.rowDisabled || (() => false); addons = props.addons; const columns = injectPropsIfTargetNotExisted( props.columns, targetColumnKey, { key: fallbackColumnKey, width, header: null, render: () => null, } ); /* eslint-enable prefer-destructuring */ return { ...props, columns }; }, onInjectColumn: previous => ( record, rowKey, recordIndex, column, columnIndex ) => { // 不是目标列 if (column.key !== targetColumnKey && column.key !== fallbackColumnKey) { return previous(record, rowKey, recordIndex, column, columnIndex); } const { children: preChildren, props, ...result } = previous( record, rowKey, recordIndex, column, columnIndex ); let children = preChildren; // 非表头 if (recordIndex !== -1) { const key = getRowKey(record, recordIndex); const isExpandable = !shouldRecordExpandable || shouldRecordExpandable(record); const isExpanded = expandedKeySet.has(key); if (isExpandable) { const toggle = (event: React.MouseEvent) => { if (isExpanded) { const nextSet = new Set(expandedKeySet); nextSet.delete(key); onExpandedKeysChange(Array.from(nextSet.keys()), { operateType: "collapse", operateKey: key, operateRecord: record, event, }); } else { onExpandedKeysChange(expandedKeys.concat(key), { operateType: "expand", operateKey: key, operateRecord: record, event, }); } }; const element = ( <div> <IconWrap onClick={toggle}> <Icon type={isExpanded ? "arrowdown" : "arrowright"} /> </IconWrap> {children} </div> ); children = iconRender(element, { children: preChildren, record, rowKey, recordIndex, disabled: rowDisabled(record), }); } } return { ...result, props, children }; }, onInjectRow: renderRow => (record, rowKey, recordIndex, columns) => { // 不是展开的行,原样渲染即可 if (!expandedKeySet.has(rowKey)) { return renderRow(record, rowKey, recordIndex, columns); } // 原始行的渲染内容 const { prepends, row, appends } = renderRow( record, rowKey, recordIndex, columns ); // 展开的内容 const expands: JSX.Element[] = []; // 支持两种展开方式,提供了 render 方法的话,渲染一个行给 render() 作为渲染容器 Iif (typeof render === "function") { expands.push( <tr className="tr__detailrow" key={`${rowKey}__expanded-row`}> {gapCell > 0 && ( <td colSpan={gapCell}> {gapCellRender && <div>{gapCellRender(record)}</div>} </td> )} <td colSpan={columns.length - (gapCell > 0 ? gapCell : 0)}> <div>{render(record)}</div> </td> </tr> ); } // 如果提供了 expand 方法,则意图为展开当前数据为更多的数据 // 更多的数据走原来表格的渲染逻辑 else Eif (typeof expand === "function") { // 展开的数据 const expandedRecords = expand(record) || []; // 展开数据的渲染和追加 expandedRecords.forEach((expandedRecord, expandedRecordIndex) => { const expandedRowKey = getRowKey(expandedRecord, expandedRecordIndex); // 这里如果直接使用 renderRow,则展开的数据无法经过插件的处理,所以调用了 useMiddleware const expandedInfo = useMiddleware(addons, "onInjectRow")(renderRow)( expandedRecord, expandedRowKey, recordIndex, columns ); expands.push(...expandedInfo.prepends); expands.push(expandedInfo.row); expands.push(...expandedInfo.appends); }); } return { prepends, row: React.cloneElement(row, { className: classNames(row.props.className, "tr__masterrow"), }), appends: [...appends, ...expands], }; }, }; } function IconWrap({ onClick, children }) { const { classPrefix } = useConfig(); return ( <a className={`${classPrefix}-table__icon-wrap`} onClick={onClick}> {children} </a> ); } |