From 4c5941a7fb5912e6d9f8c3a7083786afb4fecc06 Mon Sep 17 00:00:00 2001 From: Thomas van der Kleij Date: Wed, 31 Jul 2024 21:22:57 +0200 Subject: [PATCH 1/4] Add inline option to use the calendar stand alone or with a custom input. Also allow overriding picker classes --- pages/index.js | 16 ++++++++++++++++ src/components/Datepicker.tsx | 31 ++++++++++++++++++++++++------- src/types/index.ts | 2 ++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/pages/index.js b/pages/index.js index 5ce6034e..d889dda7 100644 --- a/pages/index.js +++ b/pages/index.js @@ -31,6 +31,7 @@ export default function Playground() { const [newDisabledDates, setNewDisabledDates] = useState({ startDate: "", endDate: "" }); const [startFrom, setStartFrom] = useState("2023-03-01"); const [startWeekOn, setStartWeekOn] = useState(""); + const [inline, setInline] = useState(false); const handleChange = (value, e) => { setValue(value); @@ -113,6 +114,7 @@ export default function Playground() { return isEmpty ? "Select Date" : "Clear"; }} popoverDirection={"down"} + inline={inline} // classNames={{ // input: ({ disabled, readOnly, className }) => { // if (disabled) { @@ -215,6 +217,20 @@ export default function Playground() { +
+
+ setInline(e.target.checked)} + /> + +
+
diff --git a/src/components/Datepicker.tsx b/src/components/Datepicker.tsx index 54e90b78..5655a3b9 100644 --- a/src/components/Datepicker.tsx +++ b/src/components/Datepicker.tsx @@ -7,7 +7,7 @@ import Input from "../components/Input"; import Shortcuts from "../components/Shortcuts"; import { COLORS, DATE_FORMAT, DEFAULT_COLOR, LANGUAGE } from "../constants"; import DatepickerContext from "../contexts/DatepickerContext"; -import { formatDate, nextMonth, previousMonth } from "../helpers"; +import { formatDate, nextMonth, previousMonth, classNames as classNamesUtil } from "../helpers"; import useOnClickOutside from "../hooks"; import { Period, DatepickerType, ColorKeys } from "../types"; @@ -29,6 +29,7 @@ const Datepicker: React.FC = ({ disabled = false, inputClassName = null, containerClassName = null, + pickerClassName = null, toggleClassName = null, toggleIcon = undefined, displayFormat = DATE_FORMAT, @@ -41,7 +42,8 @@ const Datepicker: React.FC = ({ inputName, startWeekOn = "sun", classNames = undefined, - popoverDirection = undefined + popoverDirection = undefined, + inline = false }) => { // Ref const containerRef = useRef(null); @@ -64,7 +66,7 @@ const Datepicker: React.FC = ({ // Custom Hooks use useOnClickOutside(containerRef, () => { const container = containerRef.current; - if (container) { + if (container && !inline) { hideDatepicker(); } }); @@ -327,18 +329,33 @@ const Datepicker: React.FC = ({ : defaultContainerClassName; }, [containerClassName]); + const pickerClassNameOverload = useMemo(() => { + const defaultPickerClassName = classNamesUtil( + "shadow-sm border border-gray-300 px-1 py-0.5 bg-white dark:bg-slate-800 dark:text-white dark:border-slate-600 rounded-lg w-fit", + !inline && "mt-2.5" + ); + return typeof pickerClassName === "function" + ? pickerClassName(defaultPickerClassName) + : typeof pickerClassName === "string" && pickerClassName !== "" + ? pickerClassName + : defaultPickerClassName; + }, [pickerClassName, inline]); + return (
- + {!inline && }