-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExplanationTable.jsx
69 lines (68 loc) · 2.28 KB
/
ExplanationTable.jsx
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
import React from "react";
import ReactDOM from "react-dom";
export default class ExplanationTable extends React.Component {
render() {
var name = this.props.name;
var mark = null;
for (var item of this.props.marks) {
if (item[0] === name) {
var matcher = this.props.matcher ? this.props.matcher : (s) => s;
mark = matcher(item[1]);
}
}
var rows = [];
for (var item of this.props.items) {
var title = item[0];
if (!Array.isArray(title)) {
title = [title, title];
}
var cls = '';
if (title[0] === mark) {
cls += 'marked';
}
var reading = item[1];
var descs = item[2];
if (!Array.isArray(descs)) {
descs = [descs];
} else {
// 下で shift() することで、元のデータを破壊してしまっていた。
// ここでコピーすることで、修正。
descs = [...descs];
}
var id = name + '_' + title[0];
var titleHtml = (
<span className={cls} id={id}>{title[1]}</span>
);
if (reading) {
titleHtml = (
<div>
<div className="explanation-table__title-ruby">{reading}</div>
{titleHtml}
</div>
);
}
var count = descs.length;
var desc = descs.shift();
rows.push(
<tr key={id+count}>
<th className="explanation-table__title-cell" rowSpan={count}>{titleHtml}</th>
<td className="explanation-table__text-cell">{desc}</td>
</tr>
);
for (var desc of descs) {
rows.push(
<tr key={id+descs.length}>
<td className="explanation-table__text-cell">{desc}</td>
</tr>
);
}
}
return (
<table className="explanation-table">
<tbody>
{rows}
</tbody>
</table>
);
}
}