/
ListForm.js
133 lines (115 loc) · 3.47 KB
/
ListForm.js
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
import React from 'react';
import PropTypes from 'prop-types';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Button from '@material-ui/core/Button';
import EditIcon from '@material-ui/icons/Edit.js';
import DeleteIcon from '@material-ui/icons/Delete.js';
export class ListForm extends React.Component {
constructor(props) {
super();
const records = Array(100).fill(0).map((_, index) => {
const n = index + 1;
return {id: n, label: `X${n}`};
});
this.state = {
records: records
};
}
onClickCreate() {
const records = this.state.records;
const lastRecord = records[records.length - 1];
const newId = lastRecord.id + 1;
const record = {id: newId, label: `X${newId}`};
records.push(record);
this.setState({
records: records
});
}
onClickUpdate(id) {
console.log('[onClickUpdate] id: ' + id);
}
onClickDelete(id) {
const records = this.state.records;
const index = records.findIndex((r) => r.id === id);
if (index < 0) return;
records.splice(index, 1);
this.setState({
records: records
});
}
render() {
return (
<div>
<h2>リスト表示</h2>
<div>
<ul className="flex-container" >
<li>
<div>
{this.state.records.length}件
</div>
</li>
<li className="right-justified-item">
<Button
variant="outlined"
color="primary"
size="small"
onClick={() => this.onClickCreate()}
style={{marginRight: '30px'}}
>
Create
</Button>
</li>
</ul>
</div>
<FixedHeaderTable
rows={this.state.records}
updateAction={(id) => this.onClickUpdate(id)}
deleteAction={(id) => this.onClickDelete(id)}
/>
</div>
);
}
}
const FixedHeaderTable = (props) => {
const rows = props.rows || [];
const updateAction = props.updateAction;
const deleteAction = props.deleteAction;
return (
<div style={{height: '400px', overflow: 'scroll', marginBottom: '30px'}}>
<Table>
<TableHead>
<TableRow>
<TableCell className="sticky-head">id</TableCell>
<TableCell className="sticky-head">label</TableCell>
<TableCell className="sticky-head">action</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow key={row.id}>
<TableCell>{row.id}</TableCell>
<TableCell>{row.label}</TableCell>
<TableCell>
<button onClick={() => updateAction(row.id)} className="cell-button" style={{marginRight: '30px'}}>
<EditIcon color="primary" fontSize="small" />
</button>
<button onClick={() => deleteAction(row.id)} className="cell-button">
<DeleteIcon color="secondary" fontSize="small" />
</button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
);
};
FixedHeaderTable.propTypes = {
rows: PropTypes.array,
updateAction: PropTypes.func,
deleteAction: PropTypes.func
};