Skip to content

Commit

Permalink
Fix datepicker year (#154)
Browse files Browse the repository at this point in the history
* add next year

* fix datepicker year

* fix storybook
  • Loading branch information
tysky committed Dec 18, 2023
1 parent 1279539 commit 9c68a81
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cadolabs/sphere-ui",
"version": "5.1.0",
"version": "5.1.1",
"main": "dist/index.js",
"license": "MIT",
"repository": "https://github.com/Cado-Labs/sphere-ui",
Expand Down
11 changes: 2 additions & 9 deletions src/components/DatePicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useMemo, useState } from "react"
import { Calendar } from "primereact/calendar"
import { locale as primeLocale } from "primereact/api"

import { getPartsOfTime, filterTooltipOptions, pickDataAttributes } from "../../utils"
import { getYearRange, filterTooltipOptions, pickDataAttributes } from "../../utils"

import { withRange } from "./withRange"
import { MONTHS, START_DATE } from "./constants"
Expand Down Expand Up @@ -65,7 +65,7 @@ export const DatePicker = React.forwardRef(({
onChange,
dateFormat,
placeholder,
yearRange: getYearRange(),
yearRange: yearRange || getYearRange({ startCalendarDate, startRangeOfYears }),
locale: primeLocale().locale,
disabled,
monthNavigator,
Expand Down Expand Up @@ -114,13 +114,6 @@ export const DatePicker = React.forwardRef(({
setViewDate(e.value)
}

const getYearRange = () => {
const { year } = getPartsOfTime()
const startYear = startCalendarDate ? startCalendarDate.getFullYear() : startRangeOfYears

return yearRange || `${startYear}:${year}`
}

const renderDateRangePicker = () => {
const RangePicker = useMemo(() => withRange(Calendar), [])

Expand Down
11 changes: 2 additions & 9 deletions src/components/DateTimePicker/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react"
import { Calendar } from "primereact/calendar"

import { getPartsOfTime, filterTooltipOptions, pickDataAttributes } from "../../utils"
import { getYearRange, filterTooltipOptions, pickDataAttributes } from "../../utils"

import { START_DATE } from "../DatePicker/constants"

Expand Down Expand Up @@ -55,13 +55,6 @@ export const DateTimePicker = React.forwardRef(({
onVisibleChange,
...props
}, ref) => {
const getYearRange = () => {
const { year } = getPartsOfTime()
const startYear = startCalendarDate ? startCalendarDate.getFullYear() : startRangeOfYears

return yearRange || `${startYear}:${year}`
}

const renderFooter = () => {
if (!showUTC || !value) return null

Expand Down Expand Up @@ -135,7 +128,7 @@ export const DateTimePicker = React.forwardRef(({
showTime
monthNavigator={monthNavigator}
yearNavigator={yearNavigator}
yearRange={getYearRange()}
yearRange={yearRange || getYearRange({ startCalendarDate, startRangeOfYears })}
footerTemplate={renderFooter}
tooltip={tooltip}
tooltipOptions={filteredTooltipOptions}
Expand Down
8 changes: 8 additions & 0 deletions src/utils/getDate.js → src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const getPartsOfTime = () => {
return { month: currentMonth, day: currentDay, year: currentYear }
}

const getYearRange = ({ startCalendarDate, startRangeOfYears }) => {
const startYear = startCalendarDate ? startCalendarDate.getFullYear() : startRangeOfYears
const endYear = getPartsOfTime().year + 1

return `${startYear}:${endYear}`
}

export {
getPartsOfTime,
getYearRange,
}
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { getPartsOfTime } from "./getDate"
export { getPartsOfTime, getYearRange } from "./date"
export { filterTooltipOptions } from "./filterTooltipOptions"
export { filterOptions } from "./filterOptions"
export { pickDataAttributes } from "./pickDataAttributes"
Expand Down
3 changes: 1 addition & 2 deletions storybook/stories/form/DatePicker/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ function DatePickerExample () {
const minDate = new Date()
const month = new Date().getMonth()
const nextMonth = (month === 11) ? 0 : month + 1
const maxDate = new Date()
maxDate.setMonth(nextMonth)
maxDate.setMonth(maxDate.getMonth() + 1)
const minDate2 = new Date()
minDate2.setYear(minDate2.getFullYear() - 1)
Expand Down
3 changes: 1 addition & 2 deletions storybook/stories/form/DateTimePicker/dateTimePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ function DateTimePickerExample () {
const minDate = new Date()
const month = new Date().getMonth()
const nextMonth = (month === 11) ? 0 : month + 1
const maxDate = new Date()
maxDate.setMonth(nextMonth)
maxDate.setMonth(maxDate.getMonth() + 1)
const onChange = event => {
const target = event.target
Expand Down

0 comments on commit 9c68a81

Please sign in to comment.