diff --git a/README.md b/README.md index e45dafe..286b6cc 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ const DemoComponent = () => { ### IOptions +- anchor?: "auto"|"left"|"right" - Default: `auto` - title?: string - Default: `disabled` - autoHide?: boolean - Default: `true` - todayBtn?: boolean - Default: `true` diff --git a/src/Components/DatePicker.tsx b/src/Components/DatePicker.tsx index acadaff..756a0c0 100644 --- a/src/Components/DatePicker.tsx +++ b/src/Components/DatePicker.tsx @@ -17,7 +17,7 @@ export interface IDatePickerProps { } const DatePicker = ({ value, children, options, onChange, classNames, show, setShow, selectedDateState }: IDatePickerProps) => ( -
+
{children} diff --git a/src/Components/DatePickerPopup.tsx b/src/Components/DatePickerPopup.tsx index 7960c56..527b3ad 100644 --- a/src/Components/DatePickerPopup.tsx +++ b/src/Components/DatePickerPopup.tsx @@ -1,4 +1,4 @@ -import React, { ForwardedRef, forwardRef, useContext } from "react" +import React, {ForwardedRef, forwardRef, useContext, useEffect, useState} from "react" import { twMerge } from "tailwind-merge" import { dayOfTheWeekOf, firstDateOfMonth } from "../Utils/date" import { ButtonClear, ButtonNextMonth, ButtonPrevMonth, ButtonSelectMonth, ButtonToday } from "./Buttons" @@ -18,9 +18,36 @@ const DatePickerPopup = forwardRef((_props, ref: ForwardedRef { + setLeftAnchor(options.anchor !== "right") + if (options.anchor !== "auto"){ + return; + } + // @ts-ignore + const _ref: HTMLDivElement = ref.current; + if (!_ref){ + return; + } + + const resize = () => { + const rect = _ref.getBoundingClientRect(); + const screenWidth = window.innerWidth; + + setLeftAnchor(rect.right <= screenWidth) + } + resize(); + window.addEventListener("resize",resize); + return () => { + window.removeEventListener("resize",resize) + } + },[ref,options.anchor]) return ( -
+
{options?.title &&
{options?.title}
} diff --git a/src/Options.ts b/src/Options.ts index cc4f138..9799c02 100644 --- a/src/Options.ts +++ b/src/Options.ts @@ -18,6 +18,7 @@ interface IIcons { } export interface IOptions { + anchor?: "left"|"right"|"auto", title?: string autoHide?: boolean todayBtn?: boolean @@ -40,6 +41,7 @@ export interface IOptions { } const options: IOptions = { + anchor: "auto", autoHide: true, todayBtn: true, clearBtn: true,