Skip to content

Commit

Permalink
fix: the prop value was added and the date formatting was done on mon…
Browse files Browse the repository at this point in the history
…th input cmp (#196)

fix: the prop value was added and the date formatting was done on month input cmp
  • Loading branch information
arielsrodriguez committed Apr 8, 2024
1 parent e72f0b6 commit bf1ee67
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/components/Form/Input/MonthInput.tsx
Expand Up @@ -2,13 +2,15 @@
* Copyright (c) DD360 and its affiliates.
*/

import { useState, ChangeEvent, useCallback, useEffect } from 'react'
import { useState, ChangeEvent, useCallback, useEffect, useMemo } from 'react'
import { CalendarIcon } from '@heroicons/react/outline'
import { composeClasses } from 'lib/classes'

import BaseInput, { InputProps } from './BaseInput'
import DatePicker from '../../DatePicker/DatePicker'

export type TypeValue = string | number | null

const monthNames = {
es: [
'Enero',
Expand Down Expand Up @@ -50,9 +52,7 @@ function MonthInput({
}: InputProps & { pickerType?: 'month' | 'month-year' }) {
const [showDatePicker, setShowDatePicker] = useState(false)
const handleToggleDatePicker = () => setShowDatePicker(!showDatePicker)
const [localValue, setLocalValue] = useState<number | string | null>(
value as any
)
const [localValue, setLocalValue] = useState<TypeValue>(value as TypeValue)
const [displayValue, setDisplayValue] = useState<string | undefined>()

const handleDateChange = useCallback(
Expand All @@ -75,6 +75,14 @@ function MonthInput({
[localValue, onChange]
)

const parsedDate: Date | undefined = useMemo(() => {
if (typeof value === 'string' && value.length) {
const parts = value.split(' ')
return parts.length >= 2 ? new Date(+parts[0], +parts[1]) : undefined
}
return undefined
}, [value])

useEffect(() => {
if (value === '') {
setDisplayValue(undefined)
Expand Down Expand Up @@ -117,6 +125,7 @@ function MonthInput({
</button>
{showDatePicker && (
<DatePicker
value={parsedDate}
language={language}
onlyOf={pickerType}
onChange={handleDateChange}
Expand Down

0 comments on commit bf1ee67

Please sign in to comment.