回答編集履歴
3
テキスト修正
test
CHANGED
@@ -86,57 +86,65 @@
|
|
86
86
|
|
87
87
|
|
88
88
|
|
89
|
-
return
|
89
|
+
return (
|
90
90
|
|
91
|
-
<
|
91
|
+
<div>
|
92
92
|
|
93
|
-
|
93
|
+
{Object.entries(selectedTime).map(([key, { start, end }]) => (
|
94
94
|
|
95
|
-
<
|
95
|
+
<MuiPickersUtilsProvider utils={DateFnsUtils} key={`picker-${key}`}>
|
96
96
|
|
97
|
-
|
97
|
+
<Grid container justify="space-around">
|
98
98
|
|
99
|
-
|
99
|
+
<KeyboardTimePicker
|
100
100
|
|
101
|
-
|
101
|
+
margin="normal"
|
102
102
|
|
103
|
-
|
103
|
+
id="start"
|
104
104
|
|
105
|
-
|
105
|
+
label="Start Time"
|
106
106
|
|
107
|
-
|
107
|
+
value={start}
|
108
108
|
|
109
|
-
|
109
|
+
onChange={date => handleTimeChange(key, "start", date)}
|
110
110
|
|
111
|
-
|
111
|
+
KeyboardButtonProps={{
|
112
112
|
|
113
|
-
|
113
|
+
"aria-label": "change time"
|
114
114
|
|
115
|
-
|
115
|
+
}}
|
116
116
|
|
117
|
-
|
117
|
+
/>
|
118
118
|
|
119
|
-
i
|
119
|
+
<KeyboardTimePicker
|
120
120
|
|
121
|
-
|
121
|
+
margin="normal"
|
122
122
|
|
123
|
-
|
123
|
+
id="end"
|
124
124
|
|
125
|
-
|
125
|
+
label="End Time"
|
126
126
|
|
127
|
-
|
127
|
+
value={end}
|
128
128
|
|
129
|
-
|
129
|
+
onChange={date => handleTimeChange(key, "end", date)}
|
130
130
|
|
131
|
-
|
131
|
+
KeyboardButtonProps={{
|
132
132
|
|
133
|
-
|
133
|
+
"aria-label": "change time"
|
134
134
|
|
135
|
-
|
135
|
+
}}
|
136
136
|
|
137
|
-
|
137
|
+
/>
|
138
138
|
|
139
|
+
</Grid>
|
140
|
+
|
141
|
+
</MuiPickersUtilsProvider>
|
142
|
+
|
139
|
-
))
|
143
|
+
))}
|
144
|
+
|
145
|
+
</div>
|
146
|
+
|
147
|
+
);
|
140
148
|
|
141
149
|
};
|
142
150
|
|
2
テキスト修正
test
CHANGED
@@ -31,3 +31,113 @@
|
|
31
31
|
|
32
32
|
|
33
33
|
`handleEndTimeChange` のほうも同様です。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
### 追記
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
リファクタリング案を挙げておきます。`map` の中で、各 `date`の変更ハンドラーを作るのではなく、ひとつの変更ハンドラですべてをまかなうこともできるかなと思います。以下のような感じです。(※動作確認はしていません)
|
46
|
+
|
47
|
+
```jsx
|
48
|
+
|
49
|
+
const SomeComponent = props => {
|
50
|
+
|
51
|
+
const [selectedTime, setSelectedTime] = React.useState({
|
52
|
+
|
53
|
+
"1": {
|
54
|
+
|
55
|
+
start: new Date(),
|
56
|
+
|
57
|
+
end: new Date()
|
58
|
+
|
59
|
+
},
|
60
|
+
|
61
|
+
"2": {
|
62
|
+
|
63
|
+
start: new Date(),
|
64
|
+
|
65
|
+
end: new Date()
|
66
|
+
|
67
|
+
},
|
68
|
+
|
69
|
+
"3": {
|
70
|
+
|
71
|
+
start: new Date(),
|
72
|
+
|
73
|
+
end: new Date()
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
});
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
const handleTimeChange = (key, name, date) => {
|
82
|
+
|
83
|
+
setSelectedTime({ ...selectedTime, [key]: { ...selectedTime[key], [name]: date } });
|
84
|
+
|
85
|
+
};
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
return Object.entries(selectedTime).map(([key, { start, end }]) => (
|
90
|
+
|
91
|
+
<MuiPickersUtilsProvider utils={DateFnsUtils} key={`picker-${key}`}>
|
92
|
+
|
93
|
+
<Grid container justify="space-around">
|
94
|
+
|
95
|
+
<KeyboardTimePicker
|
96
|
+
|
97
|
+
margin="normal"
|
98
|
+
|
99
|
+
id="start"
|
100
|
+
|
101
|
+
label="Start Time"
|
102
|
+
|
103
|
+
value={start}
|
104
|
+
|
105
|
+
onChange={date => handleTimeChange(key, "start", date)}
|
106
|
+
|
107
|
+
KeyboardButtonProps={{
|
108
|
+
|
109
|
+
"aria-label": "change time"
|
110
|
+
|
111
|
+
}}
|
112
|
+
|
113
|
+
/>
|
114
|
+
|
115
|
+
<KeyboardTimePicker
|
116
|
+
|
117
|
+
margin="normal"
|
118
|
+
|
119
|
+
id="end"
|
120
|
+
|
121
|
+
label="End Time"
|
122
|
+
|
123
|
+
value={end}
|
124
|
+
|
125
|
+
onChange={date => handleTimeChange(key, "end", date)}
|
126
|
+
|
127
|
+
KeyboardButtonProps={{
|
128
|
+
|
129
|
+
"aria-label": "change time"
|
130
|
+
|
131
|
+
}}
|
132
|
+
|
133
|
+
/>
|
134
|
+
|
135
|
+
</Grid>
|
136
|
+
|
137
|
+
</MuiPickersUtilsProvider>
|
138
|
+
|
139
|
+
));
|
140
|
+
|
141
|
+
};
|
142
|
+
|
143
|
+
```
|
1
テキスト修正
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
```javascript
|
26
26
|
|
27
|
-
setSelectedTime({ ...obj, [key]: date });
|
27
|
+
setSelectedTime({ ...obj, [key]: { ...obj[key], start: date } });
|
28
28
|
|
29
29
|
```
|
30
30
|
|