@@ -4,6 +4,7 @@ import React, { useCallback, useContext, useEffect, useRef } from "react";
44import { BORDER_COLOR , DATE_FORMAT , RING_COLOR } from "../constants" ;
55import DatepickerContext from "../contexts/DatepickerContext" ;
66import { dateIsValid } from "../helpers" ;
7+ import { PopoverDirectionType } from "../types" ;
78
89import ToggleButton from "./ToggleButton" ;
910
@@ -43,12 +44,6 @@ const Input: React.FC<Props> = (e: Props) => {
4344 const buttonRef = useRef < HTMLButtonElement > ( null ) ;
4445 const inputRef = useRef < HTMLInputElement > ( null ) ;
4546
46- useEffect ( ( ) => {
47- if ( inputRef && e . setContextRef && typeof e . setContextRef === "function" ) {
48- e . setContextRef ( inputRef ) ;
49- }
50- } , [ e , inputRef ] ) ;
51-
5247 // Functions
5348 const getClassName = useCallback ( ( ) => {
5449 const input = inputRef . current ;
@@ -108,7 +103,45 @@ const Input: React.FC<Props> = (e: Props) => {
108103 [ changeDatepickerValue , changeDayHover , changeInputText , hideDatepicker ]
109104 ) ;
110105
106+ const renderToggleIcon = useCallback (
107+ ( isEmpty : boolean ) => {
108+ return typeof toggleIcon === "undefined" ? (
109+ < ToggleButton isEmpty = { isEmpty } />
110+ ) : (
111+ toggleIcon ( isEmpty )
112+ ) ;
113+ } ,
114+ [ toggleIcon ]
115+ ) ;
116+
117+ const getToggleClassName = useCallback ( ( ) => {
118+ const button = buttonRef . current ;
119+
120+ if (
121+ button &&
122+ typeof classNames !== "undefined" &&
123+ typeof classNames ?. toggleButton === "function"
124+ ) {
125+ return classNames . toggleButton ( button ) ;
126+ }
127+
128+ const defaultToggleClassName =
129+ "absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed" ;
130+
131+ return typeof toggleClassName === "function"
132+ ? toggleClassName ( defaultToggleClassName )
133+ : typeof toggleClassName === "string" && toggleClassName !== ""
134+ ? toggleClassName
135+ : defaultToggleClassName ;
136+ } , [ toggleClassName , buttonRef , classNames ] ) ;
137+
111138 // UseEffects && UseLayoutEffect
139+ useEffect ( ( ) => {
140+ if ( inputRef && e . setContextRef && typeof e . setContextRef === "function" ) {
141+ e . setContextRef ( inputRef ) ;
142+ }
143+ } , [ e , inputRef ] ) ;
144+
112145 useEffect ( ( ) => {
113146 const button = buttonRef ?. current ;
114147
@@ -163,20 +196,18 @@ const Input: React.FC<Props> = (e: Props) => {
163196 const arrow = arrowContainer ?. current ;
164197
165198 function showCalendarContainer ( ) {
166- function setContainerPosition (
167- direction : string | undefined ,
168- div : HTMLDivElement ,
169- arrow : HTMLDivElement
170- ) {
171- if ( direction === "down" ) {
172- return ;
173- }
199+ if ( arrow && div && div . classList . contains ( "hidden" ) ) {
200+ div . classList . remove ( "hidden" ) ;
201+ div . classList . add ( "block" ) ;
174202
175203 // window.innerWidth === 767
204+ const popoverOnUp = popoverDirection == PopoverDirectionType . up ;
205+ const popoverOnDown = popoverDirection === PopoverDirectionType . down ;
176206 if (
177- direction === "up" ||
207+ popoverOnUp ||
178208 ( window . innerWidth > 767 &&
179- window . screen . height - 100 < div . getBoundingClientRect ( ) . bottom )
209+ window . screen . height - 100 < div . getBoundingClientRect ( ) . bottom &&
210+ ! popoverOnDown )
180211 ) {
181212 div . classList . add ( "bottom-full" ) ;
182213 div . classList . add ( "mb-2.5" ) ;
@@ -187,13 +218,6 @@ const Input: React.FC<Props> = (e: Props) => {
187218 arrow . classList . remove ( "border-l" ) ;
188219 arrow . classList . remove ( "border-t" ) ;
189220 }
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 ) ;
197221
198222 setTimeout ( ( ) => {
199223 div . classList . remove ( "translate-y-4" ) ;
@@ -215,38 +239,6 @@ const Input: React.FC<Props> = (e: Props) => {
215239 } ;
216240 } , [ calendarContainer , arrowContainer , popoverDirection ] ) ;
217241
218- const renderToggleIcon = useCallback (
219- ( isEmpty : boolean ) => {
220- return typeof toggleIcon === "undefined" ? (
221- < ToggleButton isEmpty = { isEmpty } />
222- ) : (
223- toggleIcon ( isEmpty )
224- ) ;
225- } ,
226- [ toggleIcon ]
227- ) ;
228-
229- const getToggleClassName = useCallback ( ( ) => {
230- const button = buttonRef . current ;
231-
232- if (
233- button &&
234- typeof classNames !== "undefined" &&
235- typeof classNames ?. toggleButton === "function"
236- ) {
237- return classNames . toggleButton ( button ) ;
238- }
239-
240- const defaultToggleClassName =
241- "absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed" ;
242-
243- return typeof toggleClassName === "function"
244- ? toggleClassName ( defaultToggleClassName )
245- : typeof toggleClassName === "string" && toggleClassName !== ""
246- ? toggleClassName
247- : defaultToggleClassName ;
248- } , [ toggleClassName , buttonRef , classNames ] ) ;
249-
250242 return (
251243 < >
252244 < input
@@ -274,7 +266,7 @@ const Input: React.FC<Props> = (e: Props) => {
274266 disabled = { disabled }
275267 className = { getToggleClassName ( ) }
276268 >
277- { renderToggleIcon ( inputText == null || ( inputText != null && ! inputText . length ) ) }
269+ { renderToggleIcon ( inputText == null || ! inputText ? .length ) }
278270 </ button >
279271 </ >
280272 ) ;
0 commit comments