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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | 14x 34x 2x 2x 2x 2x 2x 2x 2x 2x 8x 2x 6x 2x 4x 93x 77x 16x 16x 16x 2x 14x 14x 14x 14x 14x 14x 16x 2x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 24x 4x 24x 2x 34x 2x 10x 8x 32x 2x 2x 8x 2x 2x 10x 6x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x | import React, { useContext } from "react"; import warning from "warning"; import classNames from "classnames"; import { TableAddon, TableProps, RowRenderContext } from "../TableProps"; import { ControlledProps } from "../../form/controlled"; import { CheckTreeRelation, CheckTree } from "../../checktree"; import { Checkbox } from "../../checkbox"; import { getRowKeyFromRecordKey } from "../util/get-row-key-from-record-key"; import { CheckContext } from "../../check"; import { injectPropsIfTargetNotExisted } from "../util/inject-props-if-target-not-existed"; import { useConfig } from "../../_util/config-context"; /** * `selectable` 插件用于支持表格可选择行的样式及操作。 */ export interface SelectableOptions<Record = any> extends ControlledProps< string[], React.SyntheticEvent, { event: React.SyntheticEvent; record: Record; selectedRecords: Record[] } > { /** * 不支持非受控模式 */ defaultValue?: never; /** * 提供 `relations` 属性,则会按照树状选择逻辑进行 */ relations?: CheckTreeRelation; /** * 提供 `all` 属性来支持全选 * 给定字符串可以用作全选项的 `key` */ all?: boolean | string; /** * 如果开启了全选支持,则可以指定哪些记录从全选的范围内排除 * * - 默认为 `disabled` 可以排除禁用的记录 * - 提供回调则自定义哪些记录应该排除,对于应该排除的记录,应该返回 `true` * * @default "disabled" */ shouldRecordExcludeFromAll?: "disabled" | ((record: any) => boolean); /** * 提供一个列的 `key`,将选择组件插入到一个目标列 * * 默认在最前新建一列插入 */ targetColumnKey?: string; /** * **\[Deprecated\]** 请使用 `indentable` 插件代替 * * @deprecated */ indent?: number; /** * 是否整行可选 */ rowSelect?: boolean; /** * 列宽度,可以指定 CSS 属性或数字 (单位:px) * @default 26 */ width?: string | number; /** * **高级用法** * 更改该插件的在每行的渲染内容,`element` 为默认渲染内容,`context` 中包含该行数据相关信息 * @default x => x */ render?: ( element: JSX.Element, context: RowRenderContext<Record> ) => React.ReactNode; /** * 指定行的选择是否可用 * * **(除展开行外,使用 Table 中的 `rowDisabled`,同样可禁用指定行的选择)** * * @default () => true */ rowSelectable?: ( rowKey: string, context: { parent: string; children: string[] } ) => boolean; } export function selectable(options: SelectableOptions): TableAddon { const { relations = {}, value, onChange, all = true, targetColumnKey, indent, rowSelect, width = 26, render = x => x, shouldRecordExcludeFromAll = "disabled", rowSelectable = () => true, } = options; Iif (typeof indent !== "undefined") { warning( false, "Property `indent` is deprecated. Please use `indentable` addon instead." ); } const fallbackColumnKey = "__selectable_addon__"; const fallbackAllKey = "__selectable_all__"; const allKey = typeof all === "string" ? all : fallbackAllKey; let rowDisabled: TableProps["rowDisabled"] = null; return { onInjectProps: props => { let { columns } = props; rowDisabled = props.rowDisabled || (() => false); columns = injectPropsIfTargetNotExisted(columns, targetColumnKey, { key: fallbackColumnKey, width, header: null, render: () => null, }); 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) { children = ( <> <Checkbox style={all ? undefined : { display: "none" }} name={allKey} > {children || <> </>} </Checkbox> {!all ? children : null} </> ); } // 记录行 else { let paddingLeft = 0; let depth = 0; Iif (indent > 0) { let node = rowKey; while (relations && relations[node]) { depth += 1; node = relations[node]; } paddingLeft = indent * depth; } const checkbox = ( <TableCheckbox name={rowKey}>{children || <> </>}</TableCheckbox> ); const element = paddingLeft ? ( <div style={{ paddingLeft }}>{checkbox}</div> ) : ( checkbox ); children = render(element, { children: preChildren, record, depth, rowKey, recordIndex, disabled: rowDisabled(record), }); } return { ...result, props, children }; }, onInjectTable: renderBody => props => { const { recordKey, records, rowDisabled = () => false } = props; const rowKey = getRowKeyFromRecordKey(recordKey); // 哪些应该从 all 里面排除 let shouldRecordExcludeFromAllFinal: (record: any) => boolean = null; Iif (typeof shouldRecordExcludeFromAll === "function") { shouldRecordExcludeFromAllFinal = shouldRecordExcludeFromAll; } else Eif (shouldRecordExcludeFromAll === "disabled") { shouldRecordExcludeFromAllFinal = rowDisabled; } shouldRecordExcludeFromAllFinal = shouldRecordExcludeFromAllFinal || (() => false); const relationsKeys = Object.keys(relations); const childrenMap = {}; relationsKeys.forEach(child => { const parent = relations[child]; if (!childrenMap[parent]) { childrenMap[parent] = []; } childrenMap[parent].push(child); }); const disabledKeys = Array.from( new Set([...relationsKeys, ...records.map(rowKey)]) ).filter( key => !rowSelectable(key, { parent: relations[key], children: childrenMap[relations[key]], }) ); // all keys const keys = Array.from( new Set([ ...relationsKeys, // 这里 records 不包括扩展行 ...records .filter(x => !shouldRecordExcludeFromAllFinal(x)) .map(rowKey) .filter(x => shouldRecordExcludeFromAll === "disabled" ? !disabledKeys.includes(x) : true ), ]) ); // keys without children const rootKeys = relations ? keys.filter(key => !relations[key]) : keys; const relationWithLeafs = { ...(relations || {}), }; for (const key of rootKeys) { relationWithLeafs[key] = allKey; } // 只有全选 Check 可用时 const hasRelations = !!Object.keys(relationWithLeafs).length; return ( <CheckTree relations={relationWithLeafs} value={value} onChange={ hasRelations ? (value, context) => onChange(value, { ...context, record: records.find( (record, index) => context.check.name === rowKey(record, index) ), selectedRecords: records.filter((record, index) => value.includes(rowKey(record, index)) ), }) : null } disabledNames={[ ...records.filter(x => rowDisabled(x)).map(rowKey), ...disabledKeys, ]} > {renderBody(props)} </CheckTree> ); }, onInjectRow: renderRow => (record, rowKey, recordIndex, columns) => { const { prepends, appends, row: preRow } = renderRow( record, rowKey, recordIndex, columns ); let row = preRow; // 支持整行选择 row = ( <SelectWrapper key={rowKey} name={rowKey} rowSelect={rowSelect && !rowDisabled(record)} > {row} </SelectWrapper> ); return { prepends, row, appends }; }, }; } function SelectWrapper({ name, rowSelect, children, ...props }: { name: string; rowSelect: boolean; children: React.ReactElement<React.HTMLAttributes<HTMLTableRowElement>>; } & React.HTMLAttributes<HTMLTableRowElement>): JSX.Element { const context = useContext(CheckContext); Eif (context) { const checkProps = context.inject({ type: "checkbox", name }); const { onChange, value } = checkProps; const rowSelectProps = { onClick: (event: React.MouseEvent<HTMLTableRowElement>) => { // 事件合并 if (typeof props.onClick === "function") { props.onClick(event); } if (typeof children.props.onClick === "function") { children.props.onClick(event); } return onChange(!value, { event, check: checkProps }); }, }; return React.cloneElement(children, { ...props, className: classNames(props.className, children.props.className, { "is-selected": !!value, }), ...(rowSelect ? rowSelectProps : {}), }); } return children; } function TableCheckbox({ children, ...props }) { const { classPrefix } = useConfig(); return ( <Checkbox className={`${classPrefix}-form-check--table-select`} {...props}> {children} </Checkbox> ); } |