Skip to content

Commit

Permalink
Merge pull request #109 from sesolaga/feature/add-popover-direction
Browse files Browse the repository at this point in the history
Feature: popover direction
  • Loading branch information
onesine committed Apr 12, 2023
2 parents 0624c2c + 7e46812 commit 74c2d11
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/components/Datepicker.tsx
Expand Up @@ -39,7 +39,8 @@ const Datepicker: React.FC<DatepickerType> = ({
inputId,
inputName,
startWeekOn = "sun",
classNames = undefined
classNames = undefined,
popoverDirection = undefined
}) => {
// Ref
const containerRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -266,7 +267,8 @@ const Datepicker: React.FC<DatepickerType> = ({
startWeekOn,
classNames,
onChange,
input: inputRef
input: inputRef,
popoverDirection
};
}, [
asSingle,
Expand Down Expand Up @@ -297,7 +299,8 @@ const Datepicker: React.FC<DatepickerType> = ({
inputName,
startWeekOn,
classNames,
inputRef
inputRef,
popoverDirection
]);

const defaultContainerClassName = "relative w-full text-gray-700";
Expand Down
30 changes: 23 additions & 7 deletions src/components/Input.tsx
Expand Up @@ -35,7 +35,8 @@ const Input: React.FC<Props> = (e: Props) => {
displayFormat,
inputId,
inputName,
classNames
classNames,
popoverDirection
} = useContext(DatepickerContext);

// UseRefs
Expand Down Expand Up @@ -162,13 +163,20 @@ const Input: React.FC<Props> = (e: Props) => {
const arrow = arrowContainer?.current;

function showCalendarContainer() {
if (arrow && div && div.classList.contains("hidden")) {
div.classList.remove("hidden");
div.classList.add("block");
function setContainerPosition(
direction: string | undefined,
div: HTMLDivElement,
arrow: HTMLDivElement
) {
if (direction === "down") {
return;
}

// window.innerWidth === 767
if (
window.innerWidth > 767 &&
window.screen.height - 100 < div.getBoundingClientRect().bottom
direction === "up" ||
(window.innerWidth > 767 &&
window.screen.height - 100 < div.getBoundingClientRect().bottom)
) {
div.classList.add("bottom-full");
div.classList.add("mb-2.5");
Expand All @@ -179,6 +187,14 @@ const Input: React.FC<Props> = (e: Props) => {
arrow.classList.remove("border-l");
arrow.classList.remove("border-t");
}
}

if (arrow && div && div.classList.contains("hidden")) {
div.classList.remove("hidden");
div.classList.add("block");

setContainerPosition(popoverDirection, div, arrow);

setTimeout(() => {
div.classList.remove("translate-y-4");
div.classList.remove("opacity-0");
Expand All @@ -197,7 +213,7 @@ const Input: React.FC<Props> = (e: Props) => {
input.removeEventListener("focus", showCalendarContainer);
}
};
}, [calendarContainer, arrowContainer]);
}, [calendarContainer, arrowContainer, popoverDirection]);

const renderToggleIcon = useCallback(
(isEmpty: boolean) => {
Expand Down
4 changes: 3 additions & 1 deletion src/contexts/DatepickerContext.ts
Expand Up @@ -46,6 +46,7 @@ interface DatepickerStore {
inputId?: string;
inputName?: string;
classNames?: ClassNamesTypeProp;
popoverDirection?: string;
}

const DatepickerContext = createContext<DatepickerStore>({
Expand Down Expand Up @@ -91,7 +92,8 @@ const DatepickerContext = createContext<DatepickerStore>({
inputName: undefined,
startWeekOn: START_WEEK,
toggleIcon: undefined,
classNames: undefined
classNames: undefined,
popoverDirection: undefined
});

export default DatepickerContext;
1 change: 1 addition & 0 deletions src/types/index.ts
Expand Up @@ -71,4 +71,5 @@ export interface DatepickerType {
maxDate?: DateType | null;
disabledDates?: DateRangeType[] | null;
startWeekOn?: string | null;
popoverDirection?: string | undefined;
}

0 comments on commit 74c2d11

Please sign in to comment.