|
1 | | -# table-basic |
| 1 | +# react-widget-table-basic |
| 2 | + |
| 3 | +Table基础组件 |
| 4 | + |
| 5 | + |
| 6 | +## 安装 |
| 7 | + |
| 8 | +``` |
| 9 | +npm install --save react-widget-table-basic |
| 10 | +``` |
| 11 | + |
| 12 | +## 使用 |
| 13 | + |
| 14 | +[](https://codesandbox.io/s/react-widget-table-basic-jujcd?fontsize=14&hidenavigation=1&theme=dark) |
| 15 | + |
| 16 | +```js |
| 17 | +import TableBasic from 'react-widget-table-basic'; |
| 18 | +import 'react-widget-table-basic/style'; |
| 19 | + |
| 20 | +const dataSource = [ |
| 21 | + { |
| 22 | + key: '1', |
| 23 | + name: '胡彦斌', |
| 24 | + age: 32, |
| 25 | + address: '西湖区湖底公园1号', |
| 26 | + }, |
| 27 | + { |
| 28 | + key: '2', |
| 29 | + name: '胡彦祖', |
| 30 | + age: 42, |
| 31 | + address: '西湖区湖底公园1号', |
| 32 | + }, |
| 33 | +]; |
| 34 | + |
| 35 | +const columns = [ |
| 36 | + { |
| 37 | + title: '姓名', |
| 38 | + dataIndex: 'name', |
| 39 | + key: 'name', |
| 40 | + }, |
| 41 | + { |
| 42 | + title: '年龄', |
| 43 | + dataIndex: 'age', |
| 44 | + key: 'age', |
| 45 | + }, |
| 46 | + { |
| 47 | + title: '住址', |
| 48 | + dataIndex: 'address', |
| 49 | + key: 'address', |
| 50 | + }, |
| 51 | +]; |
| 52 | + |
| 53 | +<TableBasic data={dataSource} columns={columns} />; |
| 54 | + |
| 55 | +``` |
| 56 | + |
| 57 | +### Interfaces |
| 58 | + |
| 59 | +```ts |
| 60 | +export interface TableBasicProps<T = DataType> { |
| 61 | + prefixCls?: string; |
| 62 | + className?: string; |
| 63 | + style?: React.CSSProperties; |
| 64 | + columns: Column<T>[]; |
| 65 | + data: T[]; |
| 66 | + tableLayout?: "auto" | "fixed"; |
| 67 | + getHeaderRowProps?: (props: RenderProps, columns: TCell<Column<T>>[], index: number) => RenderProps; |
| 68 | + getRowProps?: (props: RenderProps, data: T, index: number) => RenderProps; |
| 69 | + getRowKey: (data: T, index: number) => React.Key; |
| 70 | + getRowClassName?: (data: T, index: number) => string; |
| 71 | + showHeader?: boolean; |
| 72 | + showBody?: boolean; |
| 73 | + emptyText?: React.ReactNode; |
| 74 | + emptyRender(props: RenderProps): React.ReactNode; |
| 75 | + tableRender(props: RenderProps): React.ReactNode; |
| 76 | + headerRender: (props: RenderProps) => React.ReactElement | null; |
| 77 | + headerRowRender: (props: RenderProps) => React.ReactElement | null; |
| 78 | + headerCellRender: (props: RenderProps) => React.ReactElement | null; |
| 79 | + bodyRender: (props: RenderProps) => React.ReactElement | null; |
| 80 | + bodyRowRender: (props: RenderProps) => React.ReactElement | null; |
| 81 | + bodyCellRender: (props: RenderProps) => React.ReactElement | null; |
| 82 | +} |
| 83 | +export interface TableBasicState<T = DataType> { |
| 84 | + columns: Column<T>[]; |
| 85 | + flattenColumns: Column<T>[]; |
| 86 | + columnStore: TreeStore; |
| 87 | + leafColumns: Column<T>[]; |
| 88 | + computedColumn: TCell<Column<T>>[][]; |
| 89 | +} |
| 90 | + |
| 91 | +export declare type DataType = Record<any, any>; |
| 92 | +export interface Column<T = DataType> { |
| 93 | + title?: React.ReactNode; |
| 94 | + dataIndex?: string | number; |
| 95 | + width?: string | number; |
| 96 | + minWidth?: string | number; |
| 97 | + maxWidth?: string | number; |
| 98 | + className?: string; |
| 99 | + key?: string | number; |
| 100 | + align?: "left" | "center" | "right"; |
| 101 | + ellipsis?: boolean; |
| 102 | + children?: Column<T>[]; |
| 103 | + render?: (text: any, data: T, index: number) => React.ReactNode; |
| 104 | + headerRender?: (text: any, column: Column<T>, index: number) => React.ReactNode; |
| 105 | + getCellProps?: (props: RenderProps, data: T, index: number) => RenderProps; |
| 106 | + getHeaderCellProps?: (props: RenderProps, column: Column<T>, index: number) => RenderProps; |
| 107 | +} |
| 108 | +export interface RenderProps extends React.AllHTMLAttributes<any> { |
| 109 | + key?: React.Key; |
| 110 | + ref?: React.Ref<any>; |
| 111 | + children: React.ReactNode; |
| 112 | + [x: string]: any; |
| 113 | +} |
| 114 | + |
| 115 | +``` |
| 116 | + |
| 117 | +### defaultProps |
| 118 | + |
| 119 | +```js |
| 120 | +{ |
| 121 | + prefixCls: "rw-table", |
| 122 | + tableLayout: "auto", |
| 123 | + columns: [], |
| 124 | + data: [], |
| 125 | + showHeader: true, |
| 126 | + showBody: true, |
| 127 | + |
| 128 | + getRowKey(data, index) { |
| 129 | + return data["key"] ?? index; |
| 130 | + }, |
| 131 | + |
| 132 | + emptyText: "No Data.", |
| 133 | + |
| 134 | + emptyRender(props) { |
| 135 | + return <div {...props} />; |
| 136 | + }, |
| 137 | + tableRender(props) { |
| 138 | + return <table {...props} />; |
| 139 | + }, |
| 140 | + headerRender(props) { |
| 141 | + return <thead {...props} />; |
| 142 | + }, |
| 143 | + headerRowRender(props) { |
| 144 | + return <tr {...props} />; |
| 145 | + }, |
| 146 | + headerCellRender(props) { |
| 147 | + return <th {...props} />; |
| 148 | + }, |
| 149 | + bodyRender(props) { |
| 150 | + return <tbody {...props} />; |
| 151 | + }, |
| 152 | + bodyRowRender(props) { |
| 153 | + return <tr {...props} />; |
| 154 | + }, |
| 155 | + bodyCellRender(props) { |
| 156 | + return <td {...props} />; |
| 157 | + }, |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +### 基础样式 |
| 162 | + |
| 163 | +```css |
| 164 | +.rw-table { |
| 165 | + width: 100%; |
| 166 | + table-layout: auto; |
| 167 | + text-align: left; |
| 168 | + border-style: solid; |
| 169 | + border-width: 1px 0 0 1px; |
| 170 | + border-color: #e8e8e8; |
| 171 | +} |
| 172 | + |
| 173 | +.rw-table-head-cell { |
| 174 | + padding: 4px 10px; |
| 175 | + background-color: #fafafa; |
| 176 | + border-color: #e8e8e8; |
| 177 | + border-style: solid; |
| 178 | + border-width: 0 1px 1px 0; |
| 179 | + line-height: 28px; |
| 180 | + color: rgba(0, 0, 0, 0.85); |
| 181 | + overflow-wrap: break-word; |
| 182 | +} |
| 183 | + |
| 184 | +.rw-table-body-cell { |
| 185 | + padding: 4px 10px; |
| 186 | + border-color: #e8e8e8; |
| 187 | + border-style: solid; |
| 188 | + border-width: 0 1px 1px 0; |
| 189 | + line-height: 28px; |
| 190 | + background-color: #fff; |
| 191 | + overflow-wrap: break-word; |
| 192 | +} |
| 193 | + |
| 194 | +.rw-table-cell-ellipsis { |
| 195 | + overflow: hidden; |
| 196 | + white-space: nowrap; |
| 197 | + word-wrap: normal; |
| 198 | + text-overflow: ellipsis; |
| 199 | +} |
| 200 | + |
| 201 | +.rw-table-empty-text { |
| 202 | + background-color: #fff; |
| 203 | + text-align: center; |
| 204 | +} |
| 205 | + |
| 206 | +``` |
0 commit comments