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 | 3x 3x 1x 1x 1x 8x 1x 2x 45x 36x 9x 9x 9x 8x 8x 9x 1x 8x 8x 8x 8x 8x 8x | import React from "react"; import classNames from "classnames"; import { Omit } from "../../_type"; import { TableAddon, TableProps, RowRenderContext } from "../TableProps"; import { ControlledProps } from "../../form/controlled"; import { Radio } from "../../radio"; import { injectPropsIfTargetNotExisted } from "../util/inject-props-if-target-not-existed"; /** * `radioable` 插件用于支持表格可单选行的样式及操作。 */ export interface RadioableOptions<Record = any> extends ControlledProps< string, React.SyntheticEvent, { event: React.SyntheticEvent; record: Record } > { /** * 不支持非受控模式 */ defaultValue?: never; /** * 提供一个列的 `key`,将选择组件插入到一个目标列 * * 默认在最前新建一列插入 */ targetColumnKey?: string; /** * 是否整行可选 * @default false */ rowSelect?: boolean; /** * 列宽度,可以指定 CSS 属性或数字 (单位:px) * @default 26 */ width?: string | number; /** * **高级用法** * 更改该插件的在每行的渲染内容,`element` 为默认渲染内容,`context` 中包含该行数据相关信息 * @default x => x */ render?: ( element: JSX.Element, context: RowRenderContext<Record> ) => React.ReactNode; } const fallbackColumnKey = "__radioable_addon__"; let rowDisabled: TableProps["rowDisabled"] = null; export function radioable({ value, onChange, targetColumnKey, rowSelect, width = 26, render = x => x, }: RadioableOptions): TableAddon { return { onInjectProps: props => { rowDisabled = props.rowDisabled || (() => false); const columns = injectPropsIfTargetNotExisted( props.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) { const element = ( <Radio disabled={rowDisabled(record)} value={value === rowKey} onChange={(_, { event }) => onChange(rowKey, { event, record })} > {children || <> </>} </Radio> ); children = render(element, { children: preChildren, record, rowKey, recordIndex, disabled: rowDisabled(record), }); } return { ...result, props, children }; }, 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} value={value} onChange={(value, context) => onChange(value, { ...context, record })} rowSelect={rowSelect && !rowDisabled(record)} > {row} </SelectWrapper> ); return { prepends, row, appends }; }, }; } function SelectWrapper({ value, onChange, name, rowSelect, children, ...props }: { name: string; rowSelect: boolean; value: string; onChange: (name: string, context: { event: React.SyntheticEvent }) => void; children: React.ReactElement<React.HTMLAttributes<HTMLTableRowElement>>; } & Omit<React.HTMLAttributes<HTMLTableRowElement>, "onChange">): JSX.Element { 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(name, { event }); }, }; return React.cloneElement(children, { ...props, className: classNames(props.className, children.props.className, { "is-selected": value === name, }), ...(rowSelect ? rowSelectProps : {}), }); } |