|
| 1 | +/* eslint no-param-reassign: 0 */ |
| 2 | +import React from 'react'; |
| 3 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 4 | +import Code from 'components/common/code-block'; |
| 5 | +import { productsGenerator } from 'utils/common'; |
| 6 | + |
| 7 | +const products = productsGenerator(); |
| 8 | + |
| 9 | +const sourceCode = `\ |
| 10 | +import BootstrapTable from 'react-bootstrap-table-next'; |
| 11 | +
|
| 12 | +class DummyColumnWithRowExpand extends React.Component { |
| 13 | + constructor(props) { |
| 14 | + super(props); |
| 15 | +
|
| 16 | + this.state = { |
| 17 | + hoverIdx: null |
| 18 | + }; |
| 19 | + } |
| 20 | +
|
| 21 | + expandRow = { |
| 22 | + renderer: () => ( |
| 23 | + <div style={ { width: '100%', height: '20px' } }>Content</div> |
| 24 | + ), |
| 25 | + showExpandColumn: true, |
| 26 | + expandByColumnOnly: true |
| 27 | + }; |
| 28 | +
|
| 29 | + actionFormater = (cell, row, rowIndex, { hoverIdx }) => { |
| 30 | + if ((hoverIdx !== null || hoverIdx !== undefined) && hoverIdx === rowIndex) { |
| 31 | + return ( |
| 32 | + <div |
| 33 | + style={ { width: '20px', height: '20px', backgroundColor: 'orange' } } |
| 34 | + /> |
| 35 | + ); |
| 36 | + } |
| 37 | + return ( |
| 38 | + <div |
| 39 | + style={ { width: '20px', height: '20px' } } |
| 40 | + /> |
| 41 | + ); |
| 42 | + } |
| 43 | +
|
| 44 | + rowEvents = { |
| 45 | + onMouseEnter: (e, row, rowIndex) => { |
| 46 | + this.setState({ hoverIdx: rowIndex }); |
| 47 | + }, |
| 48 | + onMouseLeave: () => { |
| 49 | + this.setState({ hoverIdx: null }); |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + rowStyle = (row, rowIndex) => { |
| 54 | + row.index = rowIndex; |
| 55 | + const style = {}; |
| 56 | + if (rowIndex % 2 === 0) { |
| 57 | + style.backgroundColor = 'transparent'; |
| 58 | + } else { |
| 59 | + style.backgroundColor = 'rgba(54, 163, 173, .10)'; |
| 60 | + } |
| 61 | + style.borderTop = 'none'; |
| 62 | +
|
| 63 | + return style; |
| 64 | + } |
| 65 | +
|
| 66 | + render() { |
| 67 | + const columns = [{ |
| 68 | + dataField: 'id', |
| 69 | + text: 'Product ID' |
| 70 | + }, { |
| 71 | + dataField: 'name', |
| 72 | + text: 'Product Name' |
| 73 | + }, { |
| 74 | + dataField: 'price', |
| 75 | + text: 'Product Price' |
| 76 | + }, { |
| 77 | + text: '', |
| 78 | + isDummyField: true, |
| 79 | + formatter: this.actionFormater, |
| 80 | + formatExtraData: { hoverIdx: this.state.hoverIdx }, |
| 81 | + headerStyle: { width: '50px' }, |
| 82 | + style: { height: '30px' } |
| 83 | + }]; |
| 84 | + return ( |
| 85 | + <div> |
| 86 | + <BootstrapTable |
| 87 | + keyField="id" |
| 88 | + data={ products } |
| 89 | + columns={ columns } |
| 90 | + noDataIndication="There is no data" |
| 91 | + classes="table" |
| 92 | + rowStyle={ this.rowStyle } |
| 93 | + rowEvents={ this.rowEvents } |
| 94 | + expandRow={ this.expandRow } |
| 95 | + /> |
| 96 | + </div> |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | +`; |
| 101 | + |
| 102 | +export default class DummyColumnWithRowExpand extends React.Component { |
| 103 | + constructor(props) { |
| 104 | + super(props); |
| 105 | + |
| 106 | + this.state = { |
| 107 | + hoverIdx: null |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + expandRow = { |
| 112 | + renderer: () => ( |
| 113 | + <div style={ { width: '100%', height: '20px' } }>Content</div> |
| 114 | + ), |
| 115 | + showExpandColumn: true, |
| 116 | + expandByColumnOnly: true |
| 117 | + }; |
| 118 | + |
| 119 | + actionFormater = (cell, row, rowIndex, { hoverIdx }) => { |
| 120 | + if ((hoverIdx !== null || hoverIdx !== undefined) && hoverIdx === rowIndex) { |
| 121 | + return ( |
| 122 | + <div |
| 123 | + style={ { width: '20px', height: '20px', backgroundColor: 'orange' } } |
| 124 | + /> |
| 125 | + ); |
| 126 | + } |
| 127 | + return ( |
| 128 | + <div |
| 129 | + style={ { width: '20px', height: '20px' } } |
| 130 | + /> |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + rowEvents = { |
| 135 | + onMouseEnter: (e, row, rowIndex) => { |
| 136 | + this.setState({ hoverIdx: rowIndex }); |
| 137 | + }, |
| 138 | + onMouseLeave: () => { |
| 139 | + this.setState({ hoverIdx: null }); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + rowStyle = (row, rowIndex) => { |
| 144 | + row.index = rowIndex; |
| 145 | + const style = {}; |
| 146 | + if (rowIndex % 2 === 0) { |
| 147 | + style.backgroundColor = 'transparent'; |
| 148 | + } else { |
| 149 | + style.backgroundColor = 'rgba(54, 163, 173, .10)'; |
| 150 | + } |
| 151 | + style.borderTop = 'none'; |
| 152 | + |
| 153 | + return style; |
| 154 | + } |
| 155 | + |
| 156 | + render() { |
| 157 | + const columns = [{ |
| 158 | + dataField: 'id', |
| 159 | + text: 'Product ID' |
| 160 | + }, { |
| 161 | + dataField: 'name', |
| 162 | + text: 'Product Name' |
| 163 | + }, { |
| 164 | + dataField: 'price', |
| 165 | + text: 'Product Price' |
| 166 | + }, { |
| 167 | + isDummyField: true, |
| 168 | + text: '', |
| 169 | + formatter: this.actionFormater, |
| 170 | + formatExtraData: { hoverIdx: this.state.hoverIdx }, |
| 171 | + headerStyle: { width: '50px' }, |
| 172 | + style: { height: '30px' } |
| 173 | + }]; |
| 174 | + return ( |
| 175 | + <div> |
| 176 | + <BootstrapTable |
| 177 | + keyField="id" |
| 178 | + data={ products } |
| 179 | + columns={ columns } |
| 180 | + rowStyle={ this.rowStyle } |
| 181 | + rowEvents={ this.rowEvents } |
| 182 | + expandRow={ this.expandRow } |
| 183 | + /> |
| 184 | + <Code>{ sourceCode }</Code> |
| 185 | + </div> |
| 186 | + ); |
| 187 | + } |
| 188 | +} |
0 commit comments