From a7d9c62fd5b9ce1c74cc40a1f1f30903c1de541b Mon Sep 17 00:00:00 2001 From: Astrian Zheng Date: Thu, 20 Feb 2025 09:47:54 +1100 Subject: [PATCH] feat: add color customization options to SingleDatePicker and implement unique ID generation for styling --- src/components/SingleDatePicker.tsx | 59 +++++++++++++++++++++++++++-- src/style.scss | 6 --- src/utils/applyColor.ts | 21 ++++++++++ src/utils/generateUniqueId.ts | 3 ++ src/utils/index.ts | 4 +- 5 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 src/utils/applyColor.ts create mode 100644 src/utils/generateUniqueId.ts diff --git a/src/components/SingleDatePicker.tsx b/src/components/SingleDatePicker.tsx index 8a11b0e..debefa5 100644 --- a/src/components/SingleDatePicker.tsx +++ b/src/components/SingleDatePicker.tsx @@ -1,7 +1,7 @@ import LeftArrowIcon from '@/assets/icons/left-arrow.svg' import RightArrowIcon from '@/assets/icons/right-arrow.svg' import { useEffect, useState } from 'react' -import { getCalendarDates, getL10Weekday } from '../utils' +import { getCalendarDates, getL10Weekday, generateUniqueId, applyColor } from '../utils' interface Props { /** @@ -39,6 +39,36 @@ interface Props { * @default undefined */ onClose?: () => void + + /** + * The main color of the panel, including the text color and the border color. + *@default '#000000' + */ + mainColor?: string + + /** + * The accent color of the panel, including the background color of the selected date. + *@default '#000000' + */ + accentColor?: string + + /** + * The reversed color of the panel, including the text color of the selected date. + *@default '#ffffff' + */ + reversedColor?: string + + /** + * The hover color of the panel, including the hover background color of the date. + *@default '#00000017' + */ + hoverColor?: string + + /** + * The border color of the panel, including the divider color between the header and the body. + *@default '#e0e0e0' + */ + borderColor?: string } /** @@ -48,13 +78,14 @@ interface Props { * * @param {Props} props */ -export default ({ value, onSelect, localization, onClose }: Props) => { +export default ({ value, onSelect, localization, onClose, mainColor = '#000000', accentColor = '#000000', reversedColor = '#ffffff', hoverColor = '#00000017', borderColor = '#e0e0e0' }: Props) => { const [currentMonth, setCurrentMonth] = useState(new Date().getMonth()) const [currentYear, setCurrentYear] = useState(new Date().getFullYear()) const [selectedDate, setSelectedDate] = useState(new Date()) const [dates, setDates] = useState([]) const [l10nDays, setL10nDays] = useState([]) const [selectMonth, setSelectMonth] = useState(false) + const uniqueId = generateUniqueId() useEffect(() => { setDates(getCalendarDates(currentMonth, currentYear)) @@ -73,6 +104,26 @@ export default ({ value, onSelect, localization, onClose }: Props) => { setL10nDays(getL10Weekday(i18n)) }, [localization]) + useEffect(() => { + applyColor(uniqueId, { + mainColor: mainColor, + accentColor: accentColor, + reversedColor: reversedColor, + hoverColor: hoverColor, + borderColor: borderColor + }) + } , [mainColor, accentColor, reversedColor, hoverColor, borderColor]) + + useEffect(() => { + applyColor(uniqueId, { + mainColor: mainColor, + accentColor: accentColor, + reversedColor: reversedColor, + hoverColor: hoverColor, + borderColor: borderColor + }) + }, []) + function selectDate(date: Date) { setSelectedDate(date) onSelect?.({ @@ -99,7 +150,7 @@ export default ({ value, onSelect, localization, onClose }: Props) => { } if (selectMonth) return ( -
+