Skip to content

Commit 4c5941a

Browse files
committed
Add inline option to use the calendar stand alone or with a custom input.
Also allow overriding picker classes
1 parent bf063fe commit 4c5941a

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

pages/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default function Playground() {
3131
const [newDisabledDates, setNewDisabledDates] = useState({ startDate: "", endDate: "" });
3232
const [startFrom, setStartFrom] = useState("2023-03-01");
3333
const [startWeekOn, setStartWeekOn] = useState("");
34+
const [inline, setInline] = useState(false);
3435

3536
const handleChange = (value, e) => {
3637
setValue(value);
@@ -113,6 +114,7 @@ export default function Playground() {
113114
return isEmpty ? "Select Date" : "Clear";
114115
}}
115116
popoverDirection={"down"}
117+
inline={inline}
116118
// classNames={{
117119
// input: ({ disabled, readOnly, className }) => {
118120
// if (disabled) {
@@ -215,6 +217,20 @@ export default function Playground() {
215217
</label>
216218
</div>
217219
</div>
220+
<div className="mb-2 w-1/2 sm:w-full">
221+
<div className="inline-flex items-center">
222+
<input
223+
type="checkbox"
224+
className="mr-2 rounded"
225+
id="inline"
226+
checked={inline}
227+
onChange={e => setInline(e.target.checked)}
228+
/>
229+
<label className="block" htmlFor="inline">
230+
Inline
231+
</label>
232+
</div>
233+
</div>
218234
</div>
219235
<div className="w-full sm:w-1/3 pr-2 flex flex-col">
220236
<div className="mb-2">

src/components/Datepicker.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Input from "../components/Input";
77
import Shortcuts from "../components/Shortcuts";
88
import { COLORS, DATE_FORMAT, DEFAULT_COLOR, LANGUAGE } from "../constants";
99
import DatepickerContext from "../contexts/DatepickerContext";
10-
import { formatDate, nextMonth, previousMonth } from "../helpers";
10+
import { formatDate, nextMonth, previousMonth, classNames as classNamesUtil } from "../helpers";
1111
import useOnClickOutside from "../hooks";
1212
import { Period, DatepickerType, ColorKeys } from "../types";
1313

@@ -29,6 +29,7 @@ const Datepicker: React.FC<DatepickerType> = ({
2929
disabled = false,
3030
inputClassName = null,
3131
containerClassName = null,
32+
pickerClassName = null,
3233
toggleClassName = null,
3334
toggleIcon = undefined,
3435
displayFormat = DATE_FORMAT,
@@ -41,7 +42,8 @@ const Datepicker: React.FC<DatepickerType> = ({
4142
inputName,
4243
startWeekOn = "sun",
4344
classNames = undefined,
44-
popoverDirection = undefined
45+
popoverDirection = undefined,
46+
inline = false
4547
}) => {
4648
// Ref
4749
const containerRef = useRef<HTMLDivElement | null>(null);
@@ -64,7 +66,7 @@ const Datepicker: React.FC<DatepickerType> = ({
6466
// Custom Hooks use
6567
useOnClickOutside(containerRef, () => {
6668
const container = containerRef.current;
67-
if (container) {
69+
if (container && !inline) {
6870
hideDatepicker();
6971
}
7072
});
@@ -327,18 +329,33 @@ const Datepicker: React.FC<DatepickerType> = ({
327329
: defaultContainerClassName;
328330
}, [containerClassName]);
329331

332+
const pickerClassNameOverload = useMemo(() => {
333+
const defaultPickerClassName = classNamesUtil(
334+
"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",
335+
!inline && "mt-2.5"
336+
);
337+
return typeof pickerClassName === "function"
338+
? pickerClassName(defaultPickerClassName)
339+
: typeof pickerClassName === "string" && pickerClassName !== ""
340+
? pickerClassName
341+
: defaultPickerClassName;
342+
}, [pickerClassName, inline]);
343+
330344
return (
331345
<DatepickerContext.Provider value={contextValues}>
332346
<div className={containerClassNameOverload} ref={containerRef}>
333-
<Input setContextRef={setInputRef} />
347+
{!inline && <Input setContextRef={setInputRef} />}
334348

335349
<div
336-
className="transition-all ease-out duration-300 absolute z-10 mt-[1px] text-sm lg:text-xs 2xl:text-sm translate-y-4 opacity-0 hidden"
350+
className={classNamesUtil(
351+
"transition-all ease-out duration-300 mt-[1px] text-sm lg:text-xs 2xl:text-sm",
352+
!inline && "absolute z-10 opacity-0 hidden translate-y-4"
353+
)}
337354
ref={calendarContainerRef}
338355
>
339-
<Arrow ref={arrowRef} />
356+
{!inline && <Arrow ref={arrowRef} />}
340357

341-
<div className="mt-2.5 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">
358+
<div className={pickerClassNameOverload}>
342359
<div className="flex flex-col lg:flex-row py-2">
343360
{showShortcuts && <Shortcuts />}
344361

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface DatepickerType {
6969
disabled?: boolean;
7070
classNames?: ClassNamesTypeProp | undefined;
7171
containerClassName?: ((className: string) => string) | string | null;
72+
pickerClassName?: ((className: string) => string) | string | null;
7273
inputClassName?: ((className: string) => string) | string | null;
7374
toggleClassName?: ((className: string) => string) | string | null;
7475
toggleIcon?: (open: boolean) => React.ReactNode;
@@ -82,6 +83,7 @@ export interface DatepickerType {
8283
disabledDates?: DateRangeType[] | null;
8384
startWeekOn?: string | null;
8485
popoverDirection?: PopoverDirectionType;
86+
inline?: boolean;
8587
}
8688

8789
export type ColorKeys = (typeof COLORS)[number]; // "blue" | "orange"

0 commit comments

Comments
 (0)