自分でゼロから実装するのではなくライブラリという形ですが、「decision tree react」で検索すると、react-decision-tree-flow
というのがありました。
sandboxの例を図のフローに合うように改変した例です。
https://codesandbox.io/embed/decisiontree-forked-2dign8?fontsize=14&hidenavigation=1&theme=dark
index.js
js
1import React from "react";
2import ReactDOM from "react-dom";
3import { Wizard, Step, Controls } from "react-decision-tree-flow";
4
5import "./styles.css";
6const tree = {
7 Q1: ["Q2", "Q6"],
8 Q2: ["Q3", "Q4", "Q5", "Q1"],
9 Q3: ["Q2"],
10 Q4: ["Q2"],
11 Q5: ["Q2"],
12 Q6: ["Q1"]
13};
14function App() {
15 return (
16 <div className="App">
17 <h2>Start</h2>
18
19 <Wizard tree={tree} first="Q1">
20 <Step name="Q1">
21 <div>
22 Q1
23 <br />
24 <div className="btnSection">
25 <Controls>
26 {({ Q2 }) => (
27 <div onClick={Q2} className="btn">
28 A
29 </div>
30 )}
31 </Controls>
32 <Controls>
33 {({ Q6 }) => (
34 <div onClick={Q6} className="btn">
35 B
36 </div>
37 )}
38 </Controls>
39 </div>
40 </div>
41 </Step>
42
43 <Step name="Q2">
44 <div>
45 Q2.
46 <br />
47 <div className="btnSection">
48 <Controls>
49 {({ Q3 }) => (
50 <div onClick={Q3} className="btn">
51 C
52 </div>
53 )}
54 </Controls>
55 <Controls>
56 {({ Q4 }) => (
57 <div onClick={Q4} className="btn">
58 D
59 </div>
60 )}
61 </Controls>
62 <Controls>
63 {({ Q5 }) => (
64 <div onClick={Q5} className="btn">
65 E
66 </div>
67 )}
68 </Controls>
69 </div>
70 <br />
71 <Controls>
72 {({ Q1 }) => <button onClick={Q1}>Go back to Q1</button>}
73 </Controls>
74 </div>
75 </Step>
76 <Step name="Q3">
77 <div>
78 Q3
79 <br />
80 </div>
81 <br />
82 <Controls>
83 {({ Q2 }) => <button onClick={Q2}>Go back to Q2</button>}
84 </Controls>
85 </Step>
86 <Step name="Q4">
87 <div>
88 Q4
89 <br />
90 </div>
91 <br />
92 <Controls>
93 {({ Q2 }) => <button onClick={Q2}>Go back to Q2</button>}
94 </Controls>
95 </Step>
96 <Step name="Q5">
97 <div>
98 Q5
99 <br />
100 </div>
101 <br />
102 <Controls>
103 {({ Q2 }) => <button onClick={Q2}>Go back to Q2</button>}
104 </Controls>
105 </Step>
106 <Step name="Q6">
107 <div>
108 Q6
109 <br />
110 <br />
111 <Controls>
112 {({ Q1 }) => <button onClick={Q1}>Go back to Q1</button>}
113 </Controls>
114 </div>
115 </Step>
116 </Wizard>
117 </div>
118 );
119}
120
121const rootElement = document.getElementById("root");
122ReactDOM.render(<App />, rootElement);
style.css
css
1.btnSection {
2 display: flex;
3 padding: 0.5rem 0;
4}
5
6.btnSection > div {
7 margin-right: 1rem;
8}
9
10.btn {
11 display: flex;
12 justify-content: center;
13 align-items: center;
14 padding: 0.5rem 1rem;
15 border-radius: 5px;
16 border: 2px solid green;
17 text-transform: uppercase;
18}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/11 14:43
2022/04/11 15:32
退会済みユーザー
2022/04/11 22:38
2022/04/12 14:51