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

add monthFormat #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare module "react-nice-dates" {
maximumDate?: Date;
modifiers?: Modifiers;
modifiersClassNames?: ModifiersClassNames;
monthFormat?: string;
weekdayFormat?: string;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function Calendar({
onMonthChange,
onDayHover,
onDayClick,
monthFormat,
weekdayFormat,
touchDragEnabled
}) {
Expand All @@ -34,6 +35,7 @@ export default function Calendar({
minimumDate={minimumDate}
maximumDate={maximumDate}
month={month}
monthFormat={monthFormat}
onMonthChange={setMonth}
/>

Expand Down Expand Up @@ -63,6 +65,7 @@ Calendar.propTypes = {
onMonthChange: func,
onDayHover: func,
onDayClick: func,
monthFormat: string,
weekdayFormat: string,
touchDragEnabled: bool
}
18 changes: 14 additions & 4 deletions src/CalendarNavigation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import React from 'react'
import { func, instanceOf, object } from 'prop-types'
import { func, instanceOf, object, string } from 'prop-types'
import classNames from 'classnames'
import { addMonths, getYear, startOfMonth, subMonths, format, isSameMonth } from 'date-fns'

export default function CalendarNavigation({ locale, month, minimumDate, maximumDate, onMonthChange }) {
export default function CalendarNavigation({
locale,
month,
minimumDate,
maximumDate,
monthFormat: receivedMonthFormat,
onMonthChange
}) {
const handlePrevious = event => {
onMonthChange(startOfMonth(subMonths(month, 1)))
event.preventDefault()
Expand All @@ -14,6 +21,8 @@ export default function CalendarNavigation({ locale, month, minimumDate, maximum
event.preventDefault()
}

const monthFormat = receivedMonthFormat || (getYear(month) === getYear(new Date()) ? 'LLLL' : 'LLLL yyyy');

return (
<div className='nice-dates-navigation'>
<a
Expand All @@ -24,8 +33,8 @@ export default function CalendarNavigation({ locale, month, minimumDate, maximum
onTouchEnd={handlePrevious}
/>

<span className='nice-dates-navigation_current'>
{format(month, getYear(month) === getYear(new Date()) ? 'LLLL' : 'LLLL yyyy', { locale })}
<span className="nice-dates-navigation_current">
{format(month, monthFormat, { locale })}
</span>

<a
Expand All @@ -44,5 +53,6 @@ CalendarNavigation.propTypes = {
month: instanceOf(Date).isRequired,
minimumDate: instanceOf(Date),
maximumDate: instanceOf(Date),
monthFormat: string,
onMonthChange: func.isRequired
}
3 changes: 3 additions & 0 deletions src/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function DatePicker({
maximumDate,
modifiers,
modifiersClassNames,
monthFormat,
weekdayFormat,
touchDragEnabled
}) {
Expand Down Expand Up @@ -76,6 +77,7 @@ export default function DatePicker({
maximumDate={maximumDate}
modifiers={modifiers}
modifiersClassNames={modifiersClassNames}
monthFormat={monthFormat}
weekdayFormat={weekdayFormat}
touchDragEnabled={touchDragEnabled}
/>
Expand All @@ -94,6 +96,7 @@ DatePicker.propTypes = {
maximumDate: instanceOf(Date),
modifiers: objectOf(func),
modifiersClassNames: objectOf(string),
monthFormat: string,
weekdayFormat: string,
touchDragEnabled: bool
}
Expand Down
3 changes: 3 additions & 0 deletions src/DatePickerCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function DatePickerCalendar({
maximumDate,
modifiers: receivedModifiers,
modifiersClassNames,
monthFormat,
weekdayFormat,
touchDragEnabled
}) {
Expand All @@ -36,6 +37,7 @@ export default function DatePickerCalendar({
maximumDate={maximumDate}
modifiers={modifiers}
modifiersClassNames={modifiersClassNames}
monthFormat={monthFormat}
weekdayFormat={weekdayFormat}
touchDragEnabled={touchDragEnabled}
/>
Expand All @@ -52,6 +54,7 @@ DatePickerCalendar.propTypes = {
maximumDate: instanceOf(Date),
modifiers: objectOf(func),
modifiersClassNames: objectOf(string),
monthFormat: string,
weekdayFormat: string,
touchDragEnabled: bool
}
61 changes: 61 additions & 0 deletions website/examples/FormatsExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { useState } from 'react'
import { enUS } from 'date-fns/locale'
import { DatePicker } from '../../src'
import Example from './Example'

const code = `
import React, { useState } from 'react'
import { enUS } from 'date-fns/locale'
import { DatePicker } from 'react-nice-dates'
import 'react-nice-dates/build/style.css'

export default function LocalesExample() {
const [date, setDate] = useState()

return (
<div>
<p>Month format:</p>

<DatePicker date={date} onDateChange={setDate} locale={enUS} monthFormat='GGGG yyyy, MMMM'>
{({ inputProps, focused }) => (
<input className={'input' + (focused ? ' -focused' : '')} {...inputProps} placeholder='GGGG yyyy, MMMM' />
)}
</DatePicker>

<p>Weekday format:</p>

<DatePicker date={date} onDateChange={setDate} locale={enUS} weekdayFormat="eeee">
{({ inputProps, focused }) => (
<input className={'input' + (focused ? ' -focused' : '')} {...inputProps} placeholder='eeee' />
)}
</DatePicker>
</div>
)
}
`;

export default function FormatsExample() {
const [date, setDate] = useState()

return (
<Example code={code}>
<p>Month format:</p>

<DatePicker date={date} onDateChange={setDate} locale={enUS} monthFormat='GGGG yyyy, MMMM'>
{({ inputProps, focused }) => (
<input className={'input' + (focused ? ' -focused' : '')} {...inputProps} placeholder='GGGG yyyy, MMMM' />
)}
</DatePicker>

<br />

<p>Weekday format:</p>

<DatePicker date={date} onDateChange={setDate} locale={enUS} weekdayFormat="eeee">
{({ inputProps, focused }) => (
<input className={'input' + (focused ? ' -focused' : '')} {...inputProps} placeholder='eeee' />
)}
</DatePicker>
</Example>
)
}
139 changes: 139 additions & 0 deletions website/examples/MonthReactElementExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import React, { useEffect, useState } from 'react'
import { format, setYear, subDays, addYears } from 'date-fns'
import { enUS } from 'date-fns/locale'
import { DatePicker } from '../../src'
import Example from './Example'

const code = `
import React, { useEffect, useState } from 'react'
import { format, setYear, subDays, addYears } from 'date-fns'
import { enUS } from 'date-fns/locale'
import { DatePicker } from 'react-nice-dates'
import 'react-nice-dates/build/style.css'

export default function LocalesExample() {
const [date, setDate] = useState()

return (
<div>
<p>Month format as React component:</p>

<DatePicker date={date} onDateChange={setDate} locale={enUS}
minimumDate={subDays(new Date(), 7)}
maximumDate={addYears(new Date(), 2)}
monthFormat={({ currentMonth, onMonthChange, minimumDate, maximumDate }) => {
const [selectedYear, setSelectedYear] = useState(format(currentMonth, 'y'));

useEffect(() => {
setSelectedYear(format(currentMonth, 'y'))
}, [currentMonth])

const handleInputBlur = () => {
if (selectedYear.match(/^\d{4}$/)) {
onMonthChange(setYear(currentMonth, selectedYear))
} else {
setSelectedYear(format(currentMonth, 'y'))
}
}

const handleInputChange = evt => {
if (evt.target.value.match(/^\d{4}$/)) {
onMonthChange(setYear(currentMonth, evt.target.value))
}
setSelectedYear(evt.target.value)
}

return (
<>
{format(currentMonth, 'MMMM')}
{' '}
<input
type='number'
value={selectedYear}
onChange={handleInputChange}
onBlur={handleInputBlur}
size={8}
/>

<br />

<sup>
{minimumDate && \`min date: \${format(minimumDate, 'yyyy/MM/dd', {locale: enUS})}\`}
{' '}
{maximumDate && \`max date: \${format(maximumDate, 'yyyy/MM/dd', {locale: enUS})}\`}
</sup>
</>
)
}}
>
{({ inputProps, focused }) => (
<input className={'input' + (focused ? ' -focused' : '')} {...inputProps} placeholder='' />
)}
</DatePicker>
</div>
)
}
`;

export default function MonthReactElementExample() {
const [date, setDate] = useState()

return (
<Example code={code}>
<p>Month format as React component:</p>

<DatePicker date={date} onDateChange={setDate} locale={enUS}
minimumDate={subDays(new Date(), 7)}
maximumDate={addYears(new Date(), 2)}
monthFormat={({ currentMonth, onMonthChange, minimumDate, maximumDate }) => {
const [selectedYear, setSelectedYear] = useState(format(currentMonth, 'y'));

useEffect(() => {
setSelectedYear(format(currentMonth, 'y'))
}, [currentMonth])

const handleInputBlur = () => {
if (selectedYear.match(/^\d{4}$/)) {
onMonthChange(setYear(currentMonth, selectedYear))
} else {
setSelectedYear(format(currentMonth, 'y'))
}
}

const handleInputChange = evt => {
if (evt.target.value.match(/^\d{4}$/)) {
onMonthChange(setYear(currentMonth, evt.target.value))
}
setSelectedYear(evt.target.value)
}

return (
<>
{format(currentMonth, 'MMMM')}
{' '}
<input
type='number'
value={selectedYear}
onChange={handleInputChange}
onBlur={handleInputBlur}
size={8}
/>

<br />

<sup>
{minimumDate && `min date: ${format(minimumDate, 'yyyy/MM/dd', {locale: enUS})}`}
{' '}
{maximumDate && `max date: ${format(maximumDate, 'yyyy/MM/dd', {locale: enUS})}`}
</sup>
</>
)
}}
>
{({ inputProps, focused }) => (
<input className={'input' + (focused ? ' -focused' : '')} {...inputProps} placeholder='' />
)}
</DatePicker>
</Example>
)
}
19 changes: 17 additions & 2 deletions website/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import DateRangePickerCalendarExample from './examples/DateRangePickerCalendarEx
import StandaloneInputExample from './examples/StandaloneInputExample'
import DatePickerCalendarWithInputExample from './examples/DatePickerCalendarWithInputExample'
import ModifiersExample from './examples/ModifiersExample'
import FormatsExample from './examples/FormatsExample'
import LocalesExample from './examples/LocalesExample'
import CalendarExample from './examples/CalendarExample'
import CodeBlock from './CodeBlock'
Expand Down Expand Up @@ -140,11 +141,20 @@ function App() {

<DatePickerWithTimeInputExample />

<h3>Localization</h3>
<h3>Dates format</h3>

<p>
As you might have noticed, React Nice Dates relies of the awesome <a href='https://date-fns.org/'>date-fns</a>{' '}
library as a peer dependency. All components require a <code>locale</code> prop, which must be a{' '}
library as a peer dependency. Thanks to it you can configure format of visible dates.
</p>

<FormatsExample />

<h3>Localization</h3>

<p>
As mentioned above, React Nice Dates is using under the hood <a href='https://date-fns.org/'>date-fns</a>.
All components require a <code>locale</code> prop, which must be a{' '}
<a href='https://date-fns.org/v2.9.0/docs/I18n'>date-fns locale object</a> of your desired language.
</p>

Expand Down Expand Up @@ -233,6 +243,7 @@ minimumDate: instanceOf(Date), // See Calendar props
maximumDate: instanceOf(Date), // See Calendar props
modifiers: objectOf(func),
modifiersClassNames: objectOf(string),
monthFormat: string, // See Calendar props
weekdayFormat: string, // See Calendar props
touchDragEnabled: bool // See Calendar props `}
/>
Expand Down Expand Up @@ -272,6 +283,7 @@ minimumLength: number, // See DateRangePickerCalendar props
maximumLength: number, // See DateRangePickerCalendar props
modifiers: objectOf(func),
modifiersClassNames: objectOf(string),
monthFormat: string, // See Calendar props
weekdayFormat: string, // See Calendar props
touchDragEnabled: bool // See Calendar props `}
/>
Expand Down Expand Up @@ -308,6 +320,7 @@ minimumDate: instanceOf(Date), // See Calendar props
maximumDate: instanceOf(Date), // See Calendar props
modifiers: objectOf(func),
modifiersClassNames: objectOf(string),
monthFormat: string, // See Calendar props
weekdayFormat: string, // See Calendar props
touchDragEnabled: bool // See Calendar props`}
/>
Expand All @@ -333,6 +346,7 @@ minimumLength: number, // Minimum range selection length, defaults to 0
maximumLength: number, // Maximum range selection length, defaults to null
modifiers: objectOf(func),
modifiersClassNames: objectOf(string),
monthFormat: string, // See Calendar props
weekdayFormat: string, // See Calendar props
touchDragEnabled: bool // See Calendar props `}
/>
Expand All @@ -349,6 +363,7 @@ maximumDate: instanceOf(Date), // Days after maximumDate will be disabled
modifiers: objectOf(func),
modifiersClassNames: objectOf(string),
month: instanceOf(Date), // Optional: Turns current month into a controlled prop
monthFormat: string, // Optional: allows month name to be dynamically formatted (ex. "GGGG yyyy, MMMM")
onMonthChange: func, // Optional: Turns current month into a controlled prop
onDayHover: func,
onDayClick: func,
Expand Down