|
| 1 | +import React from 'react'; |
| 2 | +import RenderHTML from 'react-native-render-html'; |
| 3 | +import tableRenderers from '@native-html/heuristic-table-plugin'; |
| 4 | + |
| 5 | +const table1 = ` |
| 6 | +
|
| 7 | +The heuristic table is a pure native table component which evaluates content of each cell (<code>TNode</code>) to |
| 8 | +compute the best width possible for the corresponding column. It doesn't follow strictly the table layout CSS standard, |
| 9 | +but it should be a good enough approximation for a majority of use cases. |
| 10 | +
|
| 11 | +<h2>Standard table</h2> |
| 12 | +
|
| 13 | +<table> |
| 14 | + <tr> |
| 15 | + <th>Index</th> |
| 16 | + <th>Company</th> |
| 17 | + <th>Contact</th> |
| 18 | + <th>Country</th> |
| 19 | + </tr> |
| 20 | + <tr> |
| 21 | + <td>1</td> |
| 22 | + <td>Alfreds Futterkiste website</td> |
| 23 | + <td>Maria Anders</td> |
| 24 | + <td><a class="a" href="https://en.wikipedia.org/wiki/germany">Germany</a></td> |
| 25 | + </tr> |
| 26 | + <tr> |
| 27 | + <td>2</td> |
| 28 | + <td>Centro comercial Moctezuma</td> |
| 29 | + <td>Francisco Chang</td> |
| 30 | + <td><a href="https://en.wikipedia.org/wiki/mexico">Mexico</a></td> |
| 31 | + </tr> |
| 32 | + <tr> |
| 33 | + <td>3</td> |
| 34 | + <td>Ernst Handel</td> |
| 35 | + <td>Roland Mendel</td> |
| 36 | + <td><a href="https://en.wikipedia.org/wiki/austria">Austria</a></td> |
| 37 | + </tr> |
| 38 | + <tr> |
| 39 | + <td>4</td> |
| 40 | + <td>Island Trading</td> |
| 41 | + <td>Helen Bennett</td> |
| 42 | + <td><a href="https://en.wikipedia.org/wiki/united_kingdom">UK</td> |
| 43 | + </tr> |
| 44 | + <tr> |
| 45 | + <td>5</td> |
| 46 | + <td>Laughing Bacchus Winecellars</td> |
| 47 | + <td>Yoshi Tannamuri</td> |
| 48 | + <td><a href="https://en.wikipedia.org/wiki/canada">Canada</a></td> |
| 49 | + </tr> |
| 50 | + <tr> |
| 51 | + <td>6</td> |
| 52 | + <td>Magazzini Alimentari Riuniti</td> |
| 53 | + <td>Giovanni Rovelli</td> |
| 54 | + <td><a href="https://en.wikipedia.org/wiki/italy">Italy</a></td> |
| 55 | + </tr> |
| 56 | + <tr> |
| 57 | + <td>7</td> |
| 58 | + <td>Alfreds Futterkiste website</td> |
| 59 | + <td>Maria Anders</td> |
| 60 | + <td><a href="https://en.wikipedia.org/wiki/germany">Germany</a></td> |
| 61 | + </tr> |
| 62 | + <tr> |
| 63 | + <td>8</td> |
| 64 | + <td>Centro comercial Moctezuma</td> |
| 65 | + <td>Francisco Chang</td> |
| 66 | + <td><a href="https://en.wikipedia.org/wiki/mexico">Mexico</a></td> |
| 67 | + </tr> |
| 68 | + <tr> |
| 69 | + <td>9</td> |
| 70 | + <td>Ernst Handel</td> |
| 71 | + <td>Roland Mendel</td> |
| 72 | + <td><a href="https://en.wikipedia.org/wiki/austria">Austria</a></td> |
| 73 | + </tr> |
| 74 | + <tr> |
| 75 | + <td>10</td> |
| 76 | + <td>Island Trading</td> |
| 77 | + <td>Helen Bennett</td> |
| 78 | + <td><a href="https://en.wikipedia.org/wiki/united_kingdom">UK</td> |
| 79 | + </tr> |
| 80 | + <tr> |
| 81 | + <td>11</td> |
| 82 | + <td>Laughing Bacchus Winecellars</td> |
| 83 | + <td>Yoshi Tannamuri</td> |
| 84 | + <td><a href="https://en.wikipedia.org/wiki/canada">Canada</a></td> |
| 85 | + </tr> |
| 86 | + <tr> |
| 87 | + <td>12</td> |
| 88 | + <td>Magazzini Alimentari Riuniti</td> |
| 89 | + <td>Giovanni Rovelli</td> |
| 90 | + <td><a href="https://en.wikipedia.org/wiki/italy">Italy</a></td> |
| 91 | + </tr> |
| 92 | + <tr> |
| 93 | + <td>13</td> |
| 94 | + <td>Alfreds Futterkiste website</td> |
| 95 | + <td>Maria Anders</td> |
| 96 | + <td><a href="https://en.wikipedia.org/wiki/germany">Germany</a></td> |
| 97 | + </tr> |
| 98 | + <tr> |
| 99 | + <td>14</td> |
| 100 | + <td>Centro comercial Moctezuma</td> |
| 101 | + <td>Francisco Chang</td> |
| 102 | + <td><a href="https://en.wikipedia.org/wiki/mexico">Mexico</a></td> |
| 103 | + </tr> |
| 104 | +</table> |
| 105 | +
|
| 106 | +<h2>Table with colspan cell</h2> |
| 107 | +
|
| 108 | +<table> |
| 109 | +<tr> |
| 110 | + <th>Month</th> |
| 111 | + <th>Savings</th> |
| 112 | + <th>Savings for holiday!</th> |
| 113 | +</tr> |
| 114 | +<tr> |
| 115 | + <td colspan="2">January</td> |
| 116 | + <td>$100</td> |
| 117 | +</tr> |
| 118 | +</table> |
| 119 | +
|
| 120 | +<h2>Table with rowspan cells (left)</h2> |
| 121 | +
|
| 122 | +<table> |
| 123 | + <tr> |
| 124 | + <th>Month</th> |
| 125 | + <th>Fortnight</th> |
| 126 | + <th>Savings</th> |
| 127 | + </tr> |
| 128 | + <tr> |
| 129 | + <td rowspan="2">January</td> |
| 130 | + <td>first</td> |
| 131 | + <td >$50</td> |
| 132 | + </tr> |
| 133 | + <tr> |
| 134 | + <td >second</td> |
| 135 | + <td>$80</td> |
| 136 | + </tr> |
| 137 | +</table> |
| 138 | +
|
| 139 | +<h2>Table with rowspan cell (right)</h2> |
| 140 | +
|
| 141 | +<table> |
| 142 | +<tr> |
| 143 | + <th>Month</th> |
| 144 | + <th>Savings</th> |
| 145 | + <th>Savings for holiday!</th> |
| 146 | +</tr> |
| 147 | +<tr> |
| 148 | + <td>January</td> |
| 149 | + <td>$100</td> |
| 150 | + <td rowspan="2">$50</td> |
| 151 | +</tr> |
| 152 | +<tr> |
| 153 | + <td>February</td> |
| 154 | + <td>$80</td> |
| 155 | +</tr> |
| 156 | +</table> |
| 157 | +
|
| 158 | +<h2>Table with rowspan cell (left and right)</h2> |
| 159 | +
|
| 160 | +<table> |
| 161 | +<tr> |
| 162 | + <th>Month</th> |
| 163 | + <th>Savings</th> |
| 164 | + <th>Savings for holiday!</th> |
| 165 | +</tr> |
| 166 | +<tr> |
| 167 | + <td rowspan="2">January</td> |
| 168 | + <td>$100</td> |
| 169 | + <td rowspan="2">$50</td> |
| 170 | +</tr> |
| 171 | +<tr> |
| 172 | + <td>$80</td> |
| 173 | +</tr> |
| 174 | +</table> |
| 175 | +
|
| 176 | +<p> |
| 177 | +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
| 178 | +</p> |
| 179 | +`; |
| 180 | + |
| 181 | +const htmlConfig = { |
| 182 | + renderers: tableRenderers, |
| 183 | + renderersProps: { |
| 184 | + table: { |
| 185 | + getStyleForCell(cell) { |
| 186 | + return cell.tnode.tagName === 'td' |
| 187 | + ? { |
| 188 | + backgroundColor: |
| 189 | + cell.y % 2 > 0 |
| 190 | + ? 'rgba(65, 91, 118, .05)' |
| 191 | + : 'rgba(65, 91, 118, .30)' |
| 192 | + } |
| 193 | + : null; |
| 194 | + } |
| 195 | + } |
| 196 | + }, |
| 197 | + tagsStyles: { |
| 198 | + a: { |
| 199 | + color: '#419edc', |
| 200 | + textDecorationColor: '#419edc' |
| 201 | + }, |
| 202 | + table: { |
| 203 | + borderColor: '#dfdfdf', |
| 204 | + borderWidth: 0.5 |
| 205 | + }, |
| 206 | + th: { |
| 207 | + textAlign: 'center', |
| 208 | + backgroundColor: '#243445', |
| 209 | + color: '#fefefe', |
| 210 | + padding: 5, |
| 211 | + borderColor: '#2f455b', |
| 212 | + borderWidth: 0.5 |
| 213 | + }, |
| 214 | + td: { |
| 215 | + textAlign: 'center', |
| 216 | + padding: 5, |
| 217 | + borderColor: '#dfdfdf', |
| 218 | + borderWidth: 0.5 |
| 219 | + } |
| 220 | + }, |
| 221 | + defaultWebViewProps: {} |
| 222 | +}; |
| 223 | + |
| 224 | +export default function HeuristicTable({ |
| 225 | + instance, |
| 226 | + onLinkPress, |
| 227 | + availableWidth |
| 228 | +}) { |
| 229 | + return ( |
| 230 | + <RenderHTML |
| 231 | + key={`custom-${instance}`} |
| 232 | + source={{ html: table1 }} |
| 233 | + onLinkPress={onLinkPress} |
| 234 | + contentWidth={availableWidth} |
| 235 | + {...htmlConfig} |
| 236 | + debug={false} |
| 237 | + triggerTREInvalidationPropNames={['tagsStyles']} |
| 238 | + /> |
| 239 | + ); |
| 240 | +} |
0 commit comments