Skip to content

Commit

Permalink
merged explicitly yours
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeyyyzhao committed Feb 22, 2022
2 parents 810e857 + aaa8c0a commit d0b2c16
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 18 deletions.
2 changes: 1 addition & 1 deletion examples/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const EXAMPLES = {
dndOutsideSource: 'Addon: Drag and drop (from outside calendar)',
}

const DEFAULT_EXAMPLE = 'basic'
const DEFAULT_EXAMPLE = 'dnd'

class Example extends React.Component {
constructor(...args) {
Expand Down
8 changes: 6 additions & 2 deletions examples/demos/dnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class Dnd extends React.Component {
// })
}

onDragStart(e) {
console.log('onDragStart', e)
}

render() {
return (
<DragAndDropCalendar
Expand All @@ -108,8 +112,8 @@ class Dnd extends React.Component {
onEventResize={this.resizeEvent}
showMultiDayTimes={true}
onSelectSlot={this.newEvent}
onDragStart={console.log}
defaultView={Views.MONTH}
onDragStart={this.onDragStart}
defaultView={Views.WEEK}
defaultDate={new Date(2015, 3, 12)}
popup={true}
dragFromOutsideItem={
Expand Down
21 changes: 20 additions & 1 deletion examples/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const now = new Date()

export default [
const DEFAULT_EXAMPLE = [
{
id: 0,
title: 'All Day Event very long title',
Expand Down Expand Up @@ -178,3 +178,22 @@ export default [
end: new Date(2015, 3, 14, 20, 0, 0),
},
]

function getRandomInt(inputMin, inputMax) {
const min = Math.ceil(inputMin)
const max = Math.floor(inputMax)
return Math.floor(Math.random() * (max - min + 1)) + min
}

let exampleEvents = DEFAULT_EXAMPLE
for (let i = 0; i < 800; i++) {
const date = getRandomInt(12, 18)
const hour = getRandomInt(1, 22)
exampleEvents = exampleEvents.concat({
id: exampleEvents.length,
title: `${i} random event`,
start: new Date(2015, 3, date, hour, getRandomInt(1, 59), 0),
end: new Date(2015, 3, date, hour + 1, getRandomInt(1, 59), 0),
})
}
export default exampleEvents
17 changes: 16 additions & 1 deletion src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
views as componentViews,
} from './utils/propTypes'

import { notify } from './utils/helpers'
import { notify, hasStateOrPropsChanged } from './utils/helpers'
import { navigate, views } from './utils/constants'
import { mergeWithDefaults } from './localizer'
import message from './utils/messages'
Expand Down Expand Up @@ -92,6 +92,7 @@ class Calendar extends React.Component {
* @controllable onNavigate
*/
date: PropTypes.instanceOf(Date),
context: PropTypes.object,

/**
* The current view of the calendar.
Expand Down Expand Up @@ -874,6 +875,10 @@ class Calendar extends React.Component {
}
}
UNSAFE_componentWillReceiveProps(nextProps) {
const { context } = this.props
if (context === nextProps.context) {
return
}
this.setState({ context: this.getContext(nextProps) })
}

Expand Down Expand Up @@ -971,6 +976,16 @@ class Calendar extends React.Component {
return getDrilldownView(date, view, Object.keys(this.getViews()))
}

shouldComponentUpdate(nextProps, nextState) {
return hasStateOrPropsChanged(
this.state,
nextState,
this.props,
nextProps,
['selected']
)
}

render() {
let {
view,
Expand Down
12 changes: 12 additions & 0 deletions src/Day.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ import React from 'react'

import { navigate } from './utils/constants'
import TimeGrid from './TimeGrid'
import { hasStateOrPropsChanged } from './utils/helpers'

class Day extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return hasStateOrPropsChanged(
this.state,
nextState,
this.props,
nextProps,
[],
false
)
}

render() {
/**
* This allows us to default min, max, and scrollToTime
Expand Down
13 changes: 12 additions & 1 deletion src/DayColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Selection, { getBoundsForNode, isEvent } from './Selection'
import * as TimeSlotUtils from './utils/TimeSlots'
import { isSelected } from './utils/selection'

import { notify } from './utils/helpers'
import { notify, hasStateOrPropsChanged } from './utils/helpers'
import * as DayEventLayout from './utils/DayEventLayout'
import TimeSlotGroup from './TimeSlotGroup'
import TimeGridEvent from './TimeGridEvent'
Expand Down Expand Up @@ -104,6 +104,17 @@ class DayColumn extends React.Component {
}
}

shouldComponentUpdate(nextProps, nextState) {
return hasStateOrPropsChanged(
this.state,
nextState,
this.props,
nextProps,
[],
false
)
}

render() {
const {
date,
Expand Down
14 changes: 12 additions & 2 deletions src/Month.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import clsx from 'clsx'
import chunk from 'lodash/chunk'

import { navigate, views } from './utils/constants'
import { notify } from './utils/helpers'
import { notify, hasStateOrPropsChanged } from './utils/helpers'
import getPosition from 'dom-helpers/position'
import * as animationFrame from 'dom-helpers/animationFrame'

Expand All @@ -15,7 +15,6 @@ import Overlay from 'react-overlays/Overlay'
import DateContentRow from './DateContentRow'
import Header from './Header'
import DateHeader from './DateHeader'

import { inRange, sortEvents } from './utils/eventLevels'

let eventsForWeek = (evts, start, end, accessors, localizer) =>
Expand All @@ -34,6 +33,17 @@ class MonthView extends React.Component {
}
}

shouldComponentUpdate(nextProps, nextState) {
return hasStateOrPropsChanged(
this.state,
nextState,
this.props,
nextProps,
[],
false
)
}

UNSAFE_componentWillReceiveProps({ date }) {
const { date: propsDate, localizer } = this.props
this.setState({
Expand Down
11 changes: 11 additions & 0 deletions src/Week.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ import PropTypes from 'prop-types'
import React from 'react'
import { navigate } from './utils/constants'
import TimeGrid from './TimeGrid'
import { hasStateOrPropsChanged } from './utils/helpers'

class Week extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return hasStateOrPropsChanged(
this.state,
nextState,
this.props,
nextProps,
[],
false
)
}
render() {
/**
* This allows us to default min, max, and scrollToTime
Expand Down
12 changes: 12 additions & 0 deletions src/WorkWeek.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react'

import Week from './Week'
import TimeGrid from './TimeGrid'
import { hasStateOrPropsChanged } from './utils/helpers'

function workWeekRange(date, options) {
return Week.range(date, options).filter(
Expand All @@ -11,6 +12,17 @@ function workWeekRange(date, options) {
}

class WorkWeek extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return hasStateOrPropsChanged(
this.state,
nextState,
this.props,
nextProps,
[],
false
)
}

render() {
/**
* This allows us to default min, max, and scrollToTime
Expand Down
30 changes: 20 additions & 10 deletions src/addons/dragAndDrop/withDragAndDrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import EventContainerWrapper from './EventContainerWrapper'
import WeekWrapper from './WeekWrapper'
import { mergeComponents } from './common'
import { DnDContext } from './DnDContext'
import { hasStateOrPropsChanged } from '../../utils/helpers'

export default function withDragAndDrop(Calendar) {
class DragAndDropCalendar extends React.Component {
Expand Down Expand Up @@ -39,12 +40,18 @@ export default function withDragAndDrop(Calendar) {
constructor(...args) {
super(...args)

const { components } = this.props
const { components, elementProps, onDragOver } = this.props

this.components = mergeComponents(components, {
eventWrapper: EventWrapper,
eventContainerWrapper: EventContainerWrapper,
weekWrapper: WeekWrapper,
elementPropsWithDropFromOutside: this.props.onDropFromOutside
? {
...elementProps,
onDragOver: onDragOver || this.defaultOnDragOver,
}
: elementProps,
})

this.state = { interacting: false }
Expand Down Expand Up @@ -98,21 +105,24 @@ export default function withDragAndDrop(Calendar) {
if (action === 'resize' && onEventResize) onEventResize(interactionInfo)
}

shouldComponentUpdate(nextProps, nextState) {
return hasStateOrPropsChanged(
this.state,
nextState,
this.props,
nextProps,
[]
)
}

render() {
const { selectable, elementProps, ...props } = this.props
const { selectable, ...props } = this.props
const { interacting } = this.state

delete props.onEventDrop
delete props.onEventResize
props.selectable = selectable ? 'ignoreEvents' : false

const elementPropsWithDropFromOutside = this.props.onDropFromOutside
? {
...elementProps,
onDragOver: this.props.onDragOver || this.defaultOnDragOver,
}
: elementProps

props.className = clsx(
props.className,
'rbc-addons-dnd',
Expand All @@ -124,7 +134,7 @@ export default function withDragAndDrop(Calendar) {
<DnDContext.Provider value={context}>
<Calendar
{...props}
elementProps={elementPropsWithDropFromOutside}
elementProps={this.state.elementPropsWithDropFromOutside}
components={this.components}
/>
</DnDContext.Provider>
Expand Down
12 changes: 12 additions & 0 deletions src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,15 @@ export function yesterday() {
export function tomorrow() {
return dates.add(dates.startOf(new Date(), 'day'), 1, 'day')
}

export function isSameDay(a, b) {
return dates.eq(a, b, 'day')
}

export function isSameMinute(a, b) {
return dates.eq(a, b, 'minutes')
}

export function isValidJSDate(dateObject) {
return typeof dateObject === 'object' && dateObject > 0
}
Loading

0 comments on commit d0b2c16

Please sign in to comment.