Skip to content

Commit

Permalink
feat(components): showMore component customization
Browse files Browse the repository at this point in the history
I'm trying to render a custom component for the `show more` button. I
know there are a few recommended ways to do this through the `onShowMore`
callback and the `messages.showMore` prop as suggested in jquense#1147.

But that doesn't cover all use cases. For example, I want to access the
events that are being hidden by the `show more` button. I also want to
render a custom popup right next to the `show more` button.

Resolves jquense#2391
  • Loading branch information
davidmh committed Sep 21, 2024
1 parent 856dddf commit 32dbb68
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ class Calendar extends React.Component {
* timeGutterHeader: MyTimeGutterWrapper,
* timeGutterWrapper: MyTimeGutterWrapper,
* resourceHeader: MyResourceHeader,
* showMore: MyShowMoreEvent,
* toolbar: MyToolbar,
* agenda: {
* event: MyAgendaEvent, // with the agenda view use a different component to render events
Expand Down Expand Up @@ -783,6 +784,7 @@ class Calendar extends React.Component {
timeGutterHeader: PropTypes.elementType,
timeGutterWrapper: PropTypes.elementType,
resourceHeader: PropTypes.elementType,
showMore: PropTypes.elementType,

toolbar: PropTypes.elementType,

Expand Down
22 changes: 21 additions & 1 deletion src/EventEndingRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,30 @@ class EventEndingRow extends React.Component {
}

renderShowMore(segments, slot) {
let { localizer, slotMetrics } = this.props
let { localizer, slotMetrics, components } = this.props
const events = slotMetrics.getEventsForSlot(slot)
const remainingEvents = eventsInSlot(segments, slot)
const count = remainingEvents.length

if (components?.showMore) {
const ShowMore = components.showMore
// The received slot seems to be 1-based, but the range we use to pull the date is 0-based
const slotDate = slotMetrics.getDateForSlot(slot - 1)

return count ? (
<ShowMore
localizer={localizer}
slotDate={slotDate}
slot={slot}
count={count}
events={events}
remainingEvents={remainingEvents}
/>
) : (
false
)
}

return count ? (
<button
type="button"
Expand Down
10 changes: 10 additions & 0 deletions stories/Calendar.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,13 @@ CustomDayColumnWrapper.parameters = {
},
},
}

export const CustomShowMore = Template.bind({})
CustomShowMore.storyName = 'add custom showMore'
CustomShowMore.args = {
defaultView: Views.MONTH,
events,
components: {
showMore: customComponents.showMore,
},
}
30 changes: 30 additions & 0 deletions stories/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ export const DragableCalendar = (props) => {
}

export const events = [
{
title: 'example 1',
start: moment().startOf('month').add(1, 'hours').toDate(),
end: moment().startOf('month').add(2, 'hours').toDate(),
allDay: false,
},
{
title: 'example 2',
start: moment().startOf('month').add(1, 'hours').toDate(),
end: moment().startOf('month').add(2, 'hours').toDate(),
allDay: false,
},
{
title: 'example 3',
start: moment().startOf('month').add(1, 'hours').toDate(),
end: moment().startOf('month').add(2, 'hours').toDate(),
allDay: false,
},
{
title: 'example 4',
start: moment().startOf('month').add(1, 'hours').toDate(),
end: moment().startOf('month').add(2, 'hours').toDate(),
allDay: false,
},
{
title: 'example 5',
start: moment().startOf('month').add(1, 'hours').toDate(),
end: moment().startOf('month').add(2, 'hours').toDate(),
allDay: false,
},
{
title: 'test',
start: moment().add(1, 'days').subtract(5, 'hours').toDate(),
Expand Down
21 changes: 21 additions & 0 deletions stories/resources/customComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ const customComponents = {
</div>
)
},
showMore: (showMoreProps) => {
return (
<button
id="my-custom-show-more"
style={{ border: '4px solid red', cursor: 'pointer' }}
onClick={() => {
console.log('showMoreProps', showMoreProps)
window.alert(`
Clicked ${showMoreProps.slotDate
.toISOString()
.substr(0, 10)} with ${
showMoreProps.remainingEvents.length
} remaining events.
Open the console for the full set of props.
`)
}}
>
{showMoreProps.count} more!
</button>
)
},
}

export default customComponents

0 comments on commit 32dbb68

Please sign in to comment.