@@ -4,6 +4,7 @@ import React, { useCallback, useContext, useEffect, useRef } from "react";
4
4
import { BORDER_COLOR , DATE_FORMAT , RING_COLOR } from "../constants" ;
5
5
import DatepickerContext from "../contexts/DatepickerContext" ;
6
6
import { dateIsValid } from "../helpers" ;
7
+ import { PopoverDirectionType } from "../types" ;
7
8
8
9
import ToggleButton from "./ToggleButton" ;
9
10
@@ -43,12 +44,6 @@ const Input: React.FC<Props> = (e: Props) => {
43
44
const buttonRef = useRef < HTMLButtonElement > ( null ) ;
44
45
const inputRef = useRef < HTMLInputElement > ( null ) ;
45
46
46
- useEffect ( ( ) => {
47
- if ( inputRef && e . setContextRef && typeof e . setContextRef === "function" ) {
48
- e . setContextRef ( inputRef ) ;
49
- }
50
- } , [ e , inputRef ] ) ;
51
-
52
47
// Functions
53
48
const getClassName = useCallback ( ( ) => {
54
49
const input = inputRef . current ;
@@ -108,7 +103,45 @@ const Input: React.FC<Props> = (e: Props) => {
108
103
[ changeDatepickerValue , changeDayHover , changeInputText , hideDatepicker ]
109
104
) ;
110
105
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
+
111
138
// UseEffects && UseLayoutEffect
139
+ useEffect ( ( ) => {
140
+ if ( inputRef && e . setContextRef && typeof e . setContextRef === "function" ) {
141
+ e . setContextRef ( inputRef ) ;
142
+ }
143
+ } , [ e , inputRef ] ) ;
144
+
112
145
useEffect ( ( ) => {
113
146
const button = buttonRef ?. current ;
114
147
@@ -163,20 +196,18 @@ const Input: React.FC<Props> = (e: Props) => {
163
196
const arrow = arrowContainer ?. current ;
164
197
165
198
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" ) ;
174
202
175
203
// window.innerWidth === 767
204
+ const popoverOnUp = popoverDirection == PopoverDirectionType . up ;
205
+ const popoverOnDown = popoverDirection === PopoverDirectionType . down ;
176
206
if (
177
- direction === "up" ||
207
+ popoverOnUp ||
178
208
( window . innerWidth > 767 &&
179
- window . screen . height - 100 < div . getBoundingClientRect ( ) . bottom )
209
+ window . screen . height - 100 < div . getBoundingClientRect ( ) . bottom &&
210
+ ! popoverOnDown )
180
211
) {
181
212
div . classList . add ( "bottom-full" ) ;
182
213
div . classList . add ( "mb-2.5" ) ;
@@ -187,13 +218,6 @@ const Input: React.FC<Props> = (e: Props) => {
187
218
arrow . classList . remove ( "border-l" ) ;
188
219
arrow . classList . remove ( "border-t" ) ;
189
220
}
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
221
198
222
setTimeout ( ( ) => {
199
223
div . classList . remove ( "translate-y-4" ) ;
@@ -215,38 +239,6 @@ const Input: React.FC<Props> = (e: Props) => {
215
239
} ;
216
240
} , [ calendarContainer , arrowContainer , popoverDirection ] ) ;
217
241
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
-
250
242
return (
251
243
< >
252
244
< input
@@ -274,7 +266,7 @@ const Input: React.FC<Props> = (e: Props) => {
274
266
disabled = { disabled }
275
267
className = { getToggleClassName ( ) }
276
268
>
277
- { renderToggleIcon ( inputText == null || ( inputText != null && ! inputText . length ) ) }
269
+ { renderToggleIcon ( inputText == null || ! inputText ? .length ) }
278
270
</ button >
279
271
</ >
280
272
) ;
0 commit comments