回答編集履歴

3

テキスト修正

2020/06/07 22:45

投稿

jun68ykt
jun68ykt

スコア9058

test CHANGED
@@ -86,57 +86,65 @@
86
86
 
87
87
 
88
88
 
89
- return Object.entries(selectedTime).map(([key, { start, end }]) => (
89
+ return (
90
90
 
91
- <MuiPickersUtilsProvider utils={DateFnsUtils} key={`picker-${key}`}>
91
+ <div>
92
92
 
93
- <Grid container justify="space-around">
93
+ {Object.entries(selectedTime).map(([key, { start, end }]) => (
94
94
 
95
- <KeyboardTimePicker
95
+ <MuiPickersUtilsProvider utils={DateFnsUtils} key={`picker-${key}`}>
96
96
 
97
- margin="normal"
97
+ <Grid container justify="space-around">
98
98
 
99
- id="start"
99
+ <KeyboardTimePicker
100
100
 
101
- label="Start Time"
101
+ margin="normal"
102
102
 
103
- value={start}
103
+ id="start"
104
104
 
105
- onChange={date => handleTimeChange(key, "start", date)}
105
+ label="Start Time"
106
106
 
107
- KeyboardButtonProps={{
107
+ value={start}
108
108
 
109
- "aria-label": "change time"
109
+ onChange={date => handleTimeChange(key, "start", date)}
110
110
 
111
- }}
111
+ KeyboardButtonProps={{
112
112
 
113
- />
113
+ "aria-label": "change time"
114
114
 
115
- <KeyboardTimePicker
115
+ }}
116
116
 
117
- margin="normal"
117
+ />
118
118
 
119
- id="end"
119
+ <KeyboardTimePicker
120
120
 
121
- label="End Time"
121
+ margin="normal"
122
122
 
123
- value={end}
123
+ id="end"
124
124
 
125
- onChange={date => handleTimeChange(key, "end", date)}
125
+ label="End Time"
126
126
 
127
- KeyboardButtonProps={{
127
+ value={end}
128
128
 
129
- "aria-label": "change time"
129
+ onChange={date => handleTimeChange(key, "end", date)}
130
130
 
131
- }}
131
+ KeyboardButtonProps={{
132
132
 
133
- />
133
+ "aria-label": "change time"
134
134
 
135
- </Grid>
135
+ }}
136
136
 
137
- </MuiPickersUtilsProvider>
137
+ />
138
138
 
139
+ </Grid>
140
+
141
+ </MuiPickersUtilsProvider>
142
+
139
- ));
143
+ ))}
144
+
145
+ </div>
146
+
147
+ );
140
148
 
141
149
  };
142
150
 

2

テキスト修正

2020/06/07 22:45

投稿

jun68ykt
jun68ykt

スコア9058

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

テキスト修正

2020/06/07 13:59

投稿

jun68ykt
jun68ykt

スコア9058

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