|
| 1 | +import { useEffect, useRef, useState } from "react"; |
| 2 | +import { ColumnProps, Table } from "react-fluid-table"; |
| 3 | +import { Button, Form, Icon, Input } from "semantic-ui-react"; |
| 4 | +import styled from "styled-components"; |
| 5 | +import { TestData, testData } from "../data"; |
| 6 | + |
| 7 | +const StyledTable = styled(Table)` |
| 8 | + margin-top: 10px; |
| 9 | +`; |
| 10 | + |
| 11 | +const columns: ColumnProps<TestData>[] = [ |
| 12 | + { |
| 13 | + key: "id", |
| 14 | + header: "ID", |
| 15 | + width: 50 |
| 16 | + }, |
| 17 | + { |
| 18 | + key: "firstName", |
| 19 | + header: "First", |
| 20 | + width: 120 |
| 21 | + }, |
| 22 | + { |
| 23 | + key: "lastName", |
| 24 | + header: "Last", |
| 25 | + width: 120 |
| 26 | + }, |
| 27 | + { |
| 28 | + key: "email", |
| 29 | + header: "Email", |
| 30 | + width: 250 |
| 31 | + } |
| 32 | +]; |
| 33 | + |
| 34 | +const Example11 = () => { |
| 35 | + // hooks |
| 36 | + const ref = useRef(0); |
| 37 | + const [size, setSize] = useState(1); |
| 38 | + const [running, setRunning] = useState(false); |
| 39 | + const [tableHeight, setTableHeight] = useState(400); |
| 40 | + const [minTableHeight, setMinTableHeight] = useState(0); |
| 41 | + const [maxTableHeight, setMaxTableHeight] = useState(0); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + const m = ref.current; |
| 45 | + return () => { |
| 46 | + window.clearInterval(m); |
| 47 | + }; |
| 48 | + }, []); |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + <Form> |
| 53 | + <h4>Change height properties</h4> |
| 54 | + <Form.Field> |
| 55 | + <Input |
| 56 | + label="table height" |
| 57 | + placeholder="specify table height (<= 0 to disable)" |
| 58 | + type="number" |
| 59 | + value={tableHeight.toString()} |
| 60 | + onChange={e => { |
| 61 | + setTableHeight(/-?\d+/.test(e.target.value) ? parseInt(e.target.value) : 0); |
| 62 | + }} |
| 63 | + /> |
| 64 | + </Form.Field> |
| 65 | + <Form.Field> |
| 66 | + <Input |
| 67 | + label="min table height" |
| 68 | + placeholder="specify min table height (<= 0 to disable)" |
| 69 | + type="number" |
| 70 | + value={minTableHeight.toString()} |
| 71 | + onChange={e => { |
| 72 | + setMinTableHeight(/-?\d+/.test(e.target.value) ? parseInt(e.target.value) : 0); |
| 73 | + }} |
| 74 | + /> |
| 75 | + </Form.Field> |
| 76 | + <Form.Field> |
| 77 | + <Input |
| 78 | + label="max table height" |
| 79 | + placeholder="specify max table height (<= 0 to disable)" |
| 80 | + type="number" |
| 81 | + value={maxTableHeight.toString()} |
| 82 | + onChange={e => { |
| 83 | + setMaxTableHeight(/-?\d+/.test(e.target.value) ? parseInt(e.target.value) : 0); |
| 84 | + }} |
| 85 | + /> |
| 86 | + </Form.Field> |
| 87 | + <div> |
| 88 | + <Button |
| 89 | + onClick={() => { |
| 90 | + window.clearInterval(ref.current); |
| 91 | + setSize(1); |
| 92 | + setRunning(true); |
| 93 | + ref.current = window.setInterval(() => { |
| 94 | + setSize(prev => prev + 1); |
| 95 | + }, 1000); |
| 96 | + }} |
| 97 | + > |
| 98 | + Start |
| 99 | + </Button> |
| 100 | + <Button |
| 101 | + onClick={() => { |
| 102 | + window.clearInterval(ref.current); |
| 103 | + setRunning(false); |
| 104 | + }} |
| 105 | + > |
| 106 | + Stop |
| 107 | + </Button> |
| 108 | + {running && <Icon loading name="spinner" />} |
| 109 | + </div> |
| 110 | + </Form> |
| 111 | + <StyledTable |
| 112 | + borders |
| 113 | + data={testData.slice(0, size)} |
| 114 | + columns={columns} |
| 115 | + tableHeight={tableHeight} |
| 116 | + minTableHeight={minTableHeight} |
| 117 | + maxTableHeight={maxTableHeight} |
| 118 | + /> |
| 119 | + </> |
| 120 | + ); |
| 121 | +}; |
| 122 | + |
| 123 | +const Source = ` |
| 124 | +const data = [/* ... */]; |
| 125 | +
|
| 126 | +const Footer = ({ children }) => ( |
| 127 | + <div style={{ backgroundColor: "white" }}> |
| 128 | + {children} |
| 129 | + </div> |
| 130 | +); |
| 131 | +
|
| 132 | +const SimpleFooter = ({ stickyFooter }) => { |
| 133 | + return ( |
| 134 | + <StyledTable |
| 135 | + borders |
| 136 | + data={data} |
| 137 | + columns={columns} |
| 138 | + tableHeight={400} |
| 139 | + stickyFooter={stickyFooter} |
| 140 | + footerStyle={{ backgroundColor: "white" }} |
| 141 | + footerComponent={() => <Footer>Hello, World</Footer>} |
| 142 | + /> |
| 143 | + ); |
| 144 | +}; |
| 145 | +
|
| 146 | +const ComplexFooter = ({ stickyFooter }) => { |
| 147 | + return ( |
| 148 | + <StyledTable |
| 149 | + borders |
| 150 | + data={data} |
| 151 | + columns={columns} |
| 152 | + tableHeight={400} |
| 153 | + stickyFooter={stickyFooter} |
| 154 | + footerStyle={{ backgroundColor: "white" }} |
| 155 | + footerComponent={({ widths }) => ( |
| 156 | + <Footer> |
| 157 | + <div style={{ display: "flex" }}> |
| 158 | + {columns.map((c, i) => { |
| 159 | + const width = \`\${widths[i]}px\`; |
| 160 | + const style: React.CSSProperties = { |
| 161 | + width, |
| 162 | + minWidth: width, |
| 163 | + padding: "8px" |
| 164 | + }; |
| 165 | + return ( |
| 166 | + <div key={c.key} style={style}> |
| 167 | + Footer Cell |
| 168 | + </div> |
| 169 | + ); |
| 170 | + })} |
| 171 | + </div> |
| 172 | + </Footer> |
| 173 | + )} |
| 174 | + /> |
| 175 | + ); |
| 176 | +}; |
| 177 | +`; |
| 178 | + |
| 179 | +export { Example11, Source }; |
0 commit comments