Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom content in CalendarDay with #renderDay #307

Merged
merged 1 commit into from
Feb 8, 2017
Merged

Custom content in CalendarDay with #renderDay #307

merged 1 commit into from
Feb 8, 2017

Conversation

evandavis
Copy link
Contributor

@evandavis evandavis commented Feb 7, 2017

Replaces #283

/**
 * @param day - the Moment object representing the day
 * @returns {string|element} - a single text or JSX node
 */
 // example
 // print a happy cat on Fridays
 <CalendarDay
   renderDay={day => (day.day() % 6 === 5 ? '😻' : day.format('D'))}
 />

screenshot 2017-02-07 13 41 54


if (renderDay) {
const result = renderDay(day);
return typeof result === 'string' ? result : Children.only(result);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@majapw @ljharb Children.only doesn't accept strings, so I had to work around it a bit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, after more exploration, we kind of realized that we should probably just return the result directly because Children.only breaks on null, string, number, undefined... etc. X_x I thought this function did something slightly different then it does.

Basically we should just have {renderDay ? renderDay(day) : day.format('D')}directly in the` instead.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.09%) to 86.594% when pulling 1aa3fe3 on relayrides:issue-127_renderDay into 8a0c56d on airbnb:master.

Copy link
Collaborator

@majapw majapw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the one comment, unfortunately, backtracking on our previous recommendation and then this is ready to go.

If you can squash your commits into just one, that would also be appreciated. Thank you!

@@ -64,7 +76,7 @@ export default class CalendarDay extends React.Component {
onMouseLeave={e => this.onDayMouseLeave(day, e)}
onClick={e => this.onDayClick(day, e)}
>
{day.format('D')}
{this.renderDay(day)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an alternative would be to get rid of the instance method entirely, and just define the defaultProp value for renderDay to be day => day.format('D'), and do {renderDay(day)} inline here?

Copy link
Contributor Author

@evandavis evandavis Feb 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb the linter requires that we have default props at every level, so we would have to use renderDay: undefined instead of null in the wrapping components, and it seems like that pattern is discouraged. I think the instance method (or the ternary directly in render) is easier to reason about given that requirement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, altho defaulting to undefined elsewhere would be fine too.

@evandavis
Copy link
Contributor Author

@majapw updated, rebased, and squashed

@coveralls
Copy link

Coverage Status

Coverage remained the same at 86.501% when pulling 8627454 on relayrides:issue-127_renderDay into 4c2c604 on airbnb:master.

@zachfeldman
Copy link

@evandavis I'm trying this out and returning the day, then some content in a span. Works pretty decently but the span gets rendered as plaintext (<, etc). Not sure if this is intentional but once again just throwing in my two cents!

@evandavis
Copy link
Contributor Author

@zachfeldman can you post your renderDay function? It sounds like you are returning a string instead of a React node.

@zachfeldman
Copy link

@evandavis you're right :). Fixed it to a React node and now we're good! Thanks!

@evandavis
Copy link
Contributor Author

@zachfeldman 👍 return an array if you need adjacent nodes: [day.format('D'), <span />]

@majapw majapw merged commit c48e7ea into react-dates:master Feb 8, 2017
@evandavis evandavis deleted the issue-127_renderDay branch February 8, 2017 20:23
@@ -64,7 +67,7 @@ export default class CalendarDay extends React.Component {
onMouseLeave={e => this.onDayMouseLeave(day, e)}
onClick={e => this.onDayClick(day, e)}
>
{day.format('D')}
{renderDay ? renderDay(day) : day.format('D')}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to have the default renderDay function as (day) => day.format('D') and just call it always, no need for the if statement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bamieh we already discussed that here: #307 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants