@@ -7,7 +7,11 @@ import { dateIsValid } from "../helpers";
77
88import ToggleButton from "./ToggleButton" ;
99
10- const Input : React . FC = ( ) => {
10+ type Props = {
11+ setContextRef ?: ( ref : React . RefObject < HTMLInputElement > ) => void ;
12+ } ;
13+
14+ const Input : React . FC < Props > = ( e : Props ) => {
1115 // Context
1216 const {
1317 primaryColor,
@@ -30,21 +34,34 @@ const Input: React.FC = () => {
3034 readOnly,
3135 displayFormat,
3236 inputId,
33- inputName
37+ inputName,
38+ classNames
3439 } = useContext ( DatepickerContext ) ;
3540
3641 // UseRefs
3742 const buttonRef = useRef < HTMLButtonElement > ( null ) ;
3843 const inputRef = useRef < HTMLInputElement > ( null ) ;
3944
45+ useEffect ( ( ) => {
46+ if ( inputRef && e . setContextRef && typeof e . setContextRef === "function" ) {
47+ e . setContextRef ( inputRef ) ;
48+ }
49+ } , [ e , inputRef ] ) ;
50+
4051 // Functions
4152 const getClassName = useCallback ( ( ) => {
53+ const input = inputRef . current ;
54+
55+ if ( input && typeof classNames != "undefined" && typeof classNames . input === "function" ) {
56+ return classNames ?. input ( input ) ;
57+ }
58+
4259 const border = BORDER_COLOR . focus [ primaryColor as keyof typeof BORDER_COLOR . focus ] ;
4360 const ring =
4461 RING_COLOR [ "second-focus" ] [ primaryColor as keyof ( typeof RING_COLOR ) [ "second-focus" ] ] ;
4562 const classNameOverload = typeof inputClassName === "string" ? inputClassName : "" ;
4663 return `relative transition-all duration-300 py-2.5 pl-4 pr-14 w-full border-gray-300 dark:bg-slate-800 dark:text-white/80 dark:border-slate-600 rounded-lg tracking-wide font-light text-sm placeholder-gray-400 bg-white focus:ring disabled:opacity-40 disabled:cursor-not-allowed ${ border } ${ ring } ${ classNameOverload } ` ;
47- } , [ primaryColor , inputClassName ] ) ;
64+ } , [ inputRef , classNames , primaryColor , inputClassName ] ) ;
4865
4966 const handleInputChange = useCallback (
5067 ( e : React . ChangeEvent < HTMLInputElement > ) => {
@@ -66,10 +83,13 @@ const Input: React.FC = () => {
6683 dateIsValid ( new Date ( end ) ) &&
6784 dayjs ( start ) . isBefore ( end )
6885 ) {
69- changeDatepickerValue ( {
70- startDate : start ,
71- endDate : end
72- } ) ;
86+ changeDatepickerValue (
87+ {
88+ startDate : start ,
89+ endDate : end
90+ } ,
91+ e . target
92+ ) ;
7393 changeDayHover ( dayjs ( end ) . add ( - 1 , "day" ) . format ( "YYYY-MM-DD" ) ) ;
7494 hideDatepicker ( ) ;
7595 if ( input ) {
@@ -87,18 +107,23 @@ const Input: React.FC = () => {
87107
88108 function focusInput ( e : Event ) {
89109 e . stopPropagation ( ) ;
90- if ( inputRef ?. current ) {
91- inputRef . current . focus ( ) ;
110+ const input = inputRef . current ;
111+
112+ if ( input ) {
113+ input . focus ( ) ;
92114 if ( inputText && ! readOnly ) {
93115 changeInputText ( "" ) ;
94116 if ( dayHover ) {
95117 changeDayHover ( null ) ;
96118 }
97119 if ( period . start && period . end ) {
98- changeDatepickerValue ( {
99- startDate : null ,
100- endDate : null
101- } ) ;
120+ changeDatepickerValue (
121+ {
122+ startDate : null ,
123+ endDate : null
124+ } ,
125+ input
126+ ) ;
102127 }
103128 }
104129 }
@@ -121,7 +146,8 @@ const Input: React.FC = () => {
121146 inputText ,
122147 period . end ,
123148 period . start ,
124- readOnly
149+ readOnly ,
150+ inputRef
125151 ] ) ;
126152
127153 useEffect ( ( ) => {
@@ -178,6 +204,20 @@ const Input: React.FC = () => {
178204 [ toggleIcon ]
179205 ) ;
180206
207+ const getToggleClassName = useCallback ( ( ) => {
208+ const button = buttonRef . current ;
209+
210+ if (
211+ button &&
212+ typeof classNames !== "undefined" &&
213+ typeof classNames . toggleButton === "function"
214+ ) {
215+ return classNames . toggleButton ( button ) ;
216+ }
217+
218+ return `absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed ${ toggleClassName } ` ;
219+ } , [ toggleClassName , buttonRef , classNames ] ) ;
220+
181221 return (
182222 < >
183223 < input
@@ -203,7 +243,7 @@ const Input: React.FC = () => {
203243 type = "button"
204244 ref = { buttonRef }
205245 disabled = { disabled }
206- className = { `absolute right-0 h-full px-3 text-gray-400 focus:outline-none disabled:opacity-40 disabled:cursor-not-allowed ${ toggleClassName } ` }
246+ className = { getToggleClassName ( ) }
207247 >
208248 { renderToggleIcon ( inputText == null || ( inputText != null && ! inputText . length ) ) }
209249 </ button >
0 commit comments