Skip to content
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@skillbill/reactlace",
"version": "1.0.1",
"version": "1.0.2",
"private": false,
"author": "skillbill",
"license": "MIT",
Expand Down
28 changes: 21 additions & 7 deletions src/components/RLDatePicker/RLDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,40 @@ export const RLDatePicker = forwardRef<RLDatePickerRef, RLDatePickerProps>(
[withTime]
)

const toDate = useCallback((v: Date | Date[] | string | null | undefined): Date | Date[] | null | undefined => {
if (!v) return v as null | undefined
if (typeof v === 'string') {
const d = new Date(v)
return isNaN(d.getTime()) ? null : d
}
return v
}, [])

const calendarValue = useMemo(() => toDate(value) ?? null, [value, toDate])

const formattedValue = useMemo((): string | undefined => {
if (!value) return undefined

if (Array.isArray(value)) {
const resolved = toDate(value)
if (!resolved) return undefined

if (Array.isArray(resolved)) {
if (selectionMode === 'range') {
const [startDate, endDate] = value
const [startDate, endDate] = resolved
const formattedStartDate = formatDate(startDate)
const formattedEndDate = endDate ? formatDate(endDate) : ''
return `${formattedStartDate} ~ ${formattedEndDate}`
} else if (selectionMode === 'multiple') {
return value.map((date) => formatDate(date)).join(', ')
return resolved.map((date) => formatDate(date)).join(', ')
}
}

if (value instanceof Date) {
return formatDate(value)
if (resolved instanceof Date) {
return formatDate(resolved)
}

return ''
}, [value, selectionMode, formatDate])
}, [value, selectionMode, formatDate, toDate])

const handleDateSelect = useCallback(
(date: Date | Date[] | null) => {
Expand Down Expand Up @@ -141,7 +155,7 @@ export const RLDatePicker = forwardRef<RLDatePickerRef, RLDatePickerProps>(
{errorMessage && <ErrorMessage>{errorMessage}</ErrorMessage>}
</div>
<Calendar
value={value}
value={calendarValue}
onChange={(e) => handleDateSelect(e.value as Date | Date[] | null)}
dateFormat="yy/mm/dd"
inline
Expand Down
2 changes: 1 addition & 1 deletion src/components/RLDatePicker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RLInputRuleType } from '../utils/types'

export interface RLDatePickerProps {
className?: string
value?: Date | Date[] | null
value?: Date | Date[] | string | null
onChange?: (value: Date | Date[] | null) => void
name?: string
label: string
Expand Down