1
1
import React , { useEffect , useRef , useState , useImperativeHandle , useCallback , DOMAttributes } from 'react' ;
2
2
import 'jb-date-input' ;
3
3
// eslint-disable-next-line no-duplicate-imports
4
- import { JBDateInputWebComponent , JBDateInputInputTypes , JBDateInputValidationItem , JBDDateInputInboxElementWebComponent , JBDateInputValueObject } from 'jb-date-input' ;
4
+ import { JBDateInputWebComponent , JBDateInputInputTypes , ValidationItem , JBDDateInputInboxElementWebComponent , JBDateInputValueObject } from 'jb-date-input' ;
5
5
import { useEvent } from '../../../common/hooks/use-event' ;
6
+ import { JBDateInputValidationValue } from 'jb-date-input/dist/web-component/jb-date-input/lib/types' ;
6
7
7
- export { JBDateInputInputTypes , JBDateInputValueObject } ;
8
+ export { JBDateInputInputTypes , JBDateInputValueObject } ;
8
9
9
- type CustomEvents < K extends string > = { [ key in K ] : ( event : CustomEvent ) => void } ;
10
+ type CustomEvents < K extends string > = { [ key in K ] : ( event : CustomEvent ) => void } ;
10
11
11
12
type CustomElement < T , K extends string > = Partial < T & DOMAttributes < T > & { children : any } & CustomEvents < `on${K } `> > ;
12
13
declare global {
13
- // eslint-disable-next-line @typescript-eslint/no-namespace
14
- namespace JSX {
15
- interface IntrinsicElements {
16
- 'jb-date-input' : JBDateInputType ;
17
- 'jb-date-input-inbox-element' :CustomElement < JBDDateInputInboxElementWebComponent , 'Click' > ;
18
- }
19
- interface JBDateInputType extends React . DetailedHTMLProps < React . HTMLAttributes < JBDateInputWebComponent > , JBDateInputWebComponent > {
20
- class ?:string ,
21
- label ?: string ,
22
- name ?:string ,
23
- "value-type" ?: string ,
24
- "input-type" ?:string ,
25
- // ref:React.RefObject<JBDateInputWebComponent>,
26
- }
27
- }
14
+ // eslint-disable-next-line @typescript-eslint/no-namespace
15
+ namespace JSX {
16
+ interface IntrinsicElements {
17
+ 'jb-date-input' : JBDateInputType ;
18
+ 'jb-date-input-inbox-element' : CustomElement < JBDDateInputInboxElementWebComponent , 'Click' > ;
19
+ }
20
+ interface JBDateInputType extends React . DetailedHTMLProps < React . HTMLAttributes < JBDateInputWebComponent > , JBDateInputWebComponent > {
21
+ class ?: string ,
22
+ label ?: string ,
23
+ name ?: string ,
24
+ "value-type" ?: string ,
25
+ "input-type" ?: string ,
26
+ // ref:React.RefObject<JBDateInputWebComponent>,
27
+ }
28
+ }
28
29
}
29
30
export type JBDateInputEventType < T > = T & {
30
- target : JBDateInputWebComponent
31
+ target : JBDateInputWebComponent
31
32
}
32
33
export type JBDateInputProps = {
33
- label ?: string ,
34
- style ?:string ,
35
- name ?:string ,
36
- min ?: string | null | undefined | Date ,
37
- max ?: string | null | undefined | Date ,
38
- format ?: string ,
39
- className ?:string ,
40
- onKeyup ?: ( e :JBDateInputEventType < KeyboardEvent > ) => void ,
41
- onChange ?: ( e :JBDateInputEventType < Event > ) => void ,
42
- onSelect ?: ( e :JBDateInputEventType < CustomEvent > ) => void ,
43
- valueType ?: 'GREGORIAN' | 'JALALI' | 'TIME_STAMP' ,
44
- inputType ?: 'GREGORIAN' | 'JALALI' ,
45
- direction ?: 'ltr' | 'rtl' ,
46
- value ?: string | Date | null | undefined ,
47
- validationList ?: JBDateInputValidationItem [ ] ,
48
- required ?: boolean ,
49
- calendarDefaultDateView ?:{ year :number , month : number , dateType :JBDateInputInputTypes } ,
50
- usePersianNumber ?: boolean ,
51
- placeholder ?:string | null | undefined ,
52
- jalaliMonthList ?:string [ ] | null | undefined ,
53
- gregorianMonthList ?:string [ ] | null | undefined ,
54
- children ?:React . ReactNode | React . ReactNode [ ] ,
34
+ label ?: string ,
35
+ style ?: string ,
36
+ name ?: string ,
37
+ min ?: string | null | undefined | Date ,
38
+ max ?: string | null | undefined | Date ,
39
+ format ?: string ,
40
+ className ?: string ,
41
+ onKeyup ?: ( e : JBDateInputEventType < KeyboardEvent > ) => void ,
42
+ onChange ?: ( e : JBDateInputEventType < Event > ) => void ,
43
+ onSelect ?: ( e : JBDateInputEventType < CustomEvent > ) => void ,
44
+ valueType ?: 'GREGORIAN' | 'JALALI' | 'TIME_STAMP' ,
45
+ inputType ?: 'GREGORIAN' | 'JALALI' ,
46
+ direction ?: 'ltr' | 'rtl' ,
47
+ value ?: string | Date | null | undefined ,
48
+ validationList ?: ValidationItem < JBDateInputValidationValue > [ ] ,
49
+ required ?: boolean ,
50
+ calendarDefaultDateView ?: { year : number , month : number , dateType : JBDateInputInputTypes } ,
51
+ usePersianNumber ?: boolean ,
52
+ placeholder ?: string | null | undefined ,
53
+ jalaliMonthList ?: string [ ] | null | undefined ,
54
+ gregorianMonthList ?: string [ ] | null | undefined ,
55
+ children ?: React . ReactNode | React . ReactNode [ ] ,
55
56
}
56
57
57
58
export const JBDateInput = React . forwardRef ( ( props : JBDateInputProps , ref ) => {
58
59
const element = useRef < JBDateInputWebComponent > ( null ) ;
59
60
const [ refChangeCount , refChangeCountSetter ] = useState ( 0 ) ;
60
- const onFormatChangeCallBackQueueRef = useRef < ( ( ) => void ) [ ] > ( [ ] ) ;
61
+ const onFormatChangeCallBackQueueRef = useRef < ( ( ) => void ) [ ] > ( [ ] ) ;
61
62
useImperativeHandle (
62
63
ref ,
63
64
( ) => ( element ? element . current : { } ) ,
@@ -66,17 +67,17 @@ export const JBDateInput = React.forwardRef((props: JBDateInputProps, ref) => {
66
67
useEffect ( ( ) => {
67
68
refChangeCountSetter ( refChangeCount + 1 ) ;
68
69
} , [ element . current ] ) ;
69
- const onchange = useCallback ( ( e :JBDateInputEventType < Event > ) => {
70
+ const onchange = useCallback ( ( e : JBDateInputEventType < Event > ) => {
70
71
if ( props . onChange ) {
71
72
props . onChange ( e ) ;
72
73
}
73
74
} , [ props . onChange ] ) ;
74
- const onKeyup = useCallback ( ( e :JBDateInputEventType < KeyboardEvent > ) => {
75
+ const onKeyup = useCallback ( ( e : JBDateInputEventType < KeyboardEvent > ) => {
75
76
if ( props . onKeyup ) {
76
77
props . onKeyup ( e ) ;
77
78
}
78
79
} , [ props . onKeyup ] ) ;
79
- const onSelect = useCallback ( ( e :JBDateInputEventType < CustomEvent > ) => {
80
+ const onSelect = useCallback ( ( e : JBDateInputEventType < CustomEvent > ) => {
80
81
if ( props . onSelect ) {
81
82
props . onSelect ( e ) ;
82
83
}
@@ -90,7 +91,7 @@ export const JBDateInput = React.forwardRef((props: JBDateInputProps, ref) => {
90
91
element . current ?. setAttribute ( 'format' , props . format ) ;
91
92
}
92
93
if ( onFormatChangeCallBackQueueRef . current . length > 0 ) {
93
- onFormatChangeCallBackQueueRef . current . forEach ( ( callBack :( ) => void ) => {
94
+ onFormatChangeCallBackQueueRef . current . forEach ( ( callBack : ( ) => void ) => {
94
95
callBack ( ) ;
95
96
} ) ;
96
97
onFormatChangeCallBackQueueRef . current = [ ] ;
@@ -102,7 +103,7 @@ export const JBDateInput = React.forwardRef((props: JBDateInputProps, ref) => {
102
103
if ( props . max ) {
103
104
if ( props . format && props . format !== element . current ?. valueFormat ) {
104
105
onFormatChangeCallBackQueueRef . current . push ( ( ) => {
105
- if ( props . max ) { element . current ?. setMaxDate ( props . max ) ; }
106
+ if ( props . max ) { element . current ?. setMaxDate ( props . max ) ; }
106
107
} ) ;
107
108
} else {
108
109
element . current ?. setMaxDate ( props . max ) ;
@@ -128,12 +129,12 @@ export const JBDateInput = React.forwardRef((props: JBDateInputProps, ref) => {
128
129
} , [ props . value ] ) ;
129
130
useEffect ( ( ) => {
130
131
if ( element . current && Array . isArray ( props . jalaliMonthList ) ) {
131
- element . current . setMonthList ( "JALALI" , props . jalaliMonthList ) ;
132
+ element . current . setMonthList ( "JALALI" , props . jalaliMonthList ) ;
132
133
}
133
134
} , [ props . jalaliMonthList ] ) ;
134
135
useEffect ( ( ) => {
135
136
if ( element . current && Array . isArray ( props . gregorianMonthList ) ) {
136
- element . current . setMonthList ( "GREGORIAN" , props . gregorianMonthList ) ;
137
+ element . current . setMonthList ( "GREGORIAN" , props . gregorianMonthList ) ;
137
138
}
138
139
} , [ props . gregorianMonthList ] ) ;
139
140
useEffect ( ( ) => {
@@ -143,28 +144,31 @@ export const JBDateInput = React.forwardRef((props: JBDateInputProps, ref) => {
143
144
} , [ props . placeholder ] ) ;
144
145
useEffect ( ( ) => {
145
146
if ( element . current ) {
146
- if ( typeof props . style == "string" ) {
147
- element . current . setAttribute ( "style" , props . style ) ;
147
+ if ( typeof props . style == "string" ) {
148
+ element . current . setAttribute ( "style" , props . style ) ;
148
149
}
149
150
}
150
151
} , [ props . style ] ) ;
151
152
useEffect ( ( ) => {
152
153
if ( element . current && Array . isArray ( props . validationList ) ) {
153
- element . current . validationList = props . validationList ;
154
+ element . current . validation . list = props . validationList ;
154
155
}
155
156
} , [ props . validationList ] ) ;
156
157
useEffect ( ( ) => {
157
158
if ( element . current && props . direction ) {
158
159
element . current . setAttribute ( 'direction' , props . direction ) ;
159
160
}
160
- } , [ props . direction ] ) ;
161
+ } , [ props . direction ] ) ;
161
162
useEffect ( ( ) => {
162
- if ( props . required ) {
163
- element . current ?. setAttribute ( 'required' , "true" ) ;
164
- } else {
165
- element . current ?. removeAttribute ( 'required' ) ;
163
+ if ( element . current ) {
164
+ if ( props . required ) {
165
+ element . current . required = true ;
166
+ } else {
167
+ element . current . required = false ;
168
+ }
166
169
}
167
- } , [ props . required ] ) ;
170
+
171
+ } , [ props . required , element . current ] ) ;
168
172
useEffect ( ( ) => {
169
173
if ( typeof props . calendarDefaultDateView == "object" && props . calendarDefaultDateView . year && props . calendarDefaultDateView . month ) {
170
174
element . current ?. setCalendarDefaultDateView ( props . calendarDefaultDateView . year , props . calendarDefaultDateView . month , props . calendarDefaultDateView . dateType ) ;
0 commit comments