/
drag-drop.jsx
82 lines (73 loc) · 2.15 KB
/
drag-drop.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
70
71
72
73
74
75
76
77
78
79
80
81
82
/* eslint-disable react/destructuring-assignment */
import * as React from 'react';
import Paper from '@material-ui/core/Paper';
import { ViewState, EditingState } from '@devexpress/dx-react-scheduler';
import {
Scheduler,
WeekView,
Appointments,
DragDropProvider,
} from '@devexpress/dx-react-scheduler-material-ui';
import { appointments } from '../../../demo-data/appointments';
const dragDisableIds = new Set([3, 8, 10, 12]);
const allowDrag = ({ id }) => !dragDisableIds.has(id);
const appointmentComponent = (props) => {
if (allowDrag(props.data)) {
return <Appointments.Appointment {...props} />;
} return <Appointments.Appointment {...props} style={{ ...props.style, cursor: 'not-allowed' }} />;
};
export default class Demo extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
data: appointments,
currentDate: new Date('2018-06-27'),
};
this.onCommitChanges = this.commitChanges.bind(this);
}
commitChanges({ added, changed, deleted }) {
this.setState((state) => {
let { data } = state;
if (added) {
const startingAddedId = data.length > 0 ? data[data.length - 1].id + 1 : 0;
data = [...data, { id: startingAddedId, ...added }];
}
if (changed) {
data = data.map(appointment => (
changed[appointment.id] ? { ...appointment, ...changed[appointment.id] } : appointment));
}
if (deleted) {
data = data.filter(appointment => appointment.id !== deleted);
}
return { data };
});
}
render() {
const { data, currentDate } = this.state;
return (
<Paper>
<Scheduler
data={data}
height={660}
>
<ViewState
defaultCurrentDate={currentDate}
/>
<EditingState
onCommitChanges={this.onCommitChanges}
/>
<WeekView
startDayHour={9}
endDayHour={19}
/>
<Appointments
appointmentComponent={appointmentComponent}
/>
<DragDropProvider
allowDrag={allowDrag}
/>
</Scheduler>
</Paper>
);
}
}