Skip to content

Commit 74c2d11

Browse files
authored
Merge pull request #109 from sesolaga/feature/add-popover-direction
Feature: popover direction
2 parents 0624c2c + 7e46812 commit 74c2d11

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

src/components/Datepicker.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const Datepicker: React.FC<DatepickerType> = ({
3939
inputId,
4040
inputName,
4141
startWeekOn = "sun",
42-
classNames = undefined
42+
classNames = undefined,
43+
popoverDirection = undefined
4344
}) => {
4445
// Ref
4546
const containerRef = useRef<HTMLDivElement | null>(null);
@@ -266,7 +267,8 @@ const Datepicker: React.FC<DatepickerType> = ({
266267
startWeekOn,
267268
classNames,
268269
onChange,
269-
input: inputRef
270+
input: inputRef,
271+
popoverDirection
270272
};
271273
}, [
272274
asSingle,
@@ -297,7 +299,8 @@ const Datepicker: React.FC<DatepickerType> = ({
297299
inputName,
298300
startWeekOn,
299301
classNames,
300-
inputRef
302+
inputRef,
303+
popoverDirection
301304
]);
302305

303306
const defaultContainerClassName = "relative w-full text-gray-700";

src/components/Input.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const Input: React.FC<Props> = (e: Props) => {
3535
displayFormat,
3636
inputId,
3737
inputName,
38-
classNames
38+
classNames,
39+
popoverDirection
3940
} = useContext(DatepickerContext);
4041

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

164165
function showCalendarContainer() {
165-
if (arrow && div && div.classList.contains("hidden")) {
166-
div.classList.remove("hidden");
167-
div.classList.add("block");
166+
function setContainerPosition(
167+
direction: string | undefined,
168+
div: HTMLDivElement,
169+
arrow: HTMLDivElement
170+
) {
171+
if (direction === "down") {
172+
return;
173+
}
174+
168175
// window.innerWidth === 767
169176
if (
170-
window.innerWidth > 767 &&
171-
window.screen.height - 100 < div.getBoundingClientRect().bottom
177+
direction === "up" ||
178+
(window.innerWidth > 767 &&
179+
window.screen.height - 100 < div.getBoundingClientRect().bottom)
172180
) {
173181
div.classList.add("bottom-full");
174182
div.classList.add("mb-2.5");
@@ -179,6 +187,14 @@ const Input: React.FC<Props> = (e: Props) => {
179187
arrow.classList.remove("border-l");
180188
arrow.classList.remove("border-t");
181189
}
190+
}
191+
192+
if (arrow && div && div.classList.contains("hidden")) {
193+
div.classList.remove("hidden");
194+
div.classList.add("block");
195+
196+
setContainerPosition(popoverDirection, div, arrow);
197+
182198
setTimeout(() => {
183199
div.classList.remove("translate-y-4");
184200
div.classList.remove("opacity-0");
@@ -197,7 +213,7 @@ const Input: React.FC<Props> = (e: Props) => {
197213
input.removeEventListener("focus", showCalendarContainer);
198214
}
199215
};
200-
}, [calendarContainer, arrowContainer]);
216+
}, [calendarContainer, arrowContainer, popoverDirection]);
201217

202218
const renderToggleIcon = useCallback(
203219
(isEmpty: boolean) => {

src/contexts/DatepickerContext.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ interface DatepickerStore {
4646
inputId?: string;
4747
inputName?: string;
4848
classNames?: ClassNamesTypeProp;
49+
popoverDirection?: string;
4950
}
5051

5152
const DatepickerContext = createContext<DatepickerStore>({
@@ -91,7 +92,8 @@ const DatepickerContext = createContext<DatepickerStore>({
9192
inputName: undefined,
9293
startWeekOn: START_WEEK,
9394
toggleIcon: undefined,
94-
classNames: undefined
95+
classNames: undefined,
96+
popoverDirection: undefined
9597
});
9698

9799
export default DatepickerContext;

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@ export interface DatepickerType {
7171
maxDate?: DateType | null;
7272
disabledDates?: DateRangeType[] | null;
7373
startWeekOn?: string | null;
74+
popoverDirection?: string | undefined;
7475
}

0 commit comments

Comments
 (0)