1
+ import { Color , CoreTypes , KeyedTemplate , Template , View } from '@nativescript/core' ;
1
2
import * as React from 'react' ;
2
- import { View as TNSView } from '@nativescript/core' ;
3
- import { ItemsSource , Pager as NativeScriptPager , PagerItem as NativeScriptPagerItem } from '..' ;
4
- import { registerElement } from 'react-nativescript' ;
5
- import { Template , Color , CoreTypes , View , KeyedTemplate } from '@nativescript/core' ;
6
- import { render as RNSRender , unmountComponentAtNode , NSVRoot , NSVElement , ViewAttributes , NativeScriptProps , GridLayoutAttributes } from "react-nativescript" ;
7
- import { Orientation } from '..' ;
3
+ import { GridLayoutAttributes , NSVElement , NSVRoot , NativeScriptProps , render as RNSRender , ViewAttributes , registerElement , unmountComponentAtNode } from 'react-nativescript' ;
4
+ import { ItemsSource , Pager as NativeScriptPager , PagerItem as NativeScriptPagerItem , Orientation } from '..' ;
8
5
9
6
registerElement ( 'pager' , ( ) => require ( '../' ) . Pager ) ;
10
7
registerElement ( 'pagerItem' , ( ) => require ( '../' ) . PagerItem ) ;
@@ -26,15 +23,14 @@ export declare type PagerAttributes = ViewAttributes & {
26
23
transformers ?: string ;
27
24
loadMoreCount ?: number ;
28
25
disableSwipe ?: boolean ;
29
- indicator ?: any
26
+ indicator ?: any ;
30
27
showIndicator ?: boolean ;
31
28
indicatorColor ?: Color | string ;
32
29
indicatorSelectedColor ?: Color | string ;
33
30
ios ?: any ;
34
31
android ?: any ;
35
32
} ;
36
33
37
-
38
34
export type CellViewContainer = View ;
39
35
type CellFactory = ( item : any ) => React . ReactElement ;
40
36
@@ -54,15 +50,18 @@ type OwnProps = {
54
50
55
51
onLoadMoreItems ?: ( args : any ) => void ;
56
52
_debug ?: {
57
- logLevel : " debug" | " info" ;
53
+ logLevel : ' debug' | ' info' ;
58
54
onCellFirstLoad ?: ( container : CellViewContainer ) => void ;
59
55
onCellRecycle ?: ( container : CellViewContainer ) => void ;
60
56
} ;
61
- } & Omit < PagerAttributes , " onItemLoading" > ;
57
+ } & Omit < PagerAttributes , ' onItemLoading' > ;
62
58
type Props = OwnProps & { forwardedRef ?: React . RefObject < NSVElement < NativeScriptPager > > } ;
63
59
64
60
type NumberKey = number | string ;
65
- type RootKeyAndTNSView = { rootKey : string ; nativeView : View } ;
61
+ interface RootKeyAndTNSView {
62
+ rootKey : string ;
63
+ nativeView : View ;
64
+ }
66
65
67
66
interface State {
68
67
nativeCells : Record < NumberKey , CellViewContainer > ;
@@ -74,10 +73,10 @@ interface State {
74
73
export class _Pager extends React . Component < Props , State > {
75
74
static readonly defaultProps = {
76
75
_debug : {
77
- logLevel : " info" as " info" ,
76
+ logLevel : ' info' as ' info' ,
78
77
onCellFirstLoad : undefined ,
79
- onCellRecycle : undefined ,
80
- } ,
78
+ onCellRecycle : undefined
79
+ }
81
80
} ;
82
81
83
82
constructor ( props : Props ) {
@@ -86,37 +85,32 @@ export class _Pager extends React.Component<Props, State> {
86
85
this . state = {
87
86
nativeCells : { } ,
88
87
nativeCellToItemIndex : new Map ( ) ,
89
- itemIndexToNativeCell : props . _debug . logLevel === " debug" ? new Map ( ) : undefined ,
88
+ itemIndexToNativeCell : props . _debug . logLevel === ' debug' ? new Map ( ) : undefined
90
89
} ;
91
90
}
92
91
93
92
private readonly myRef = React . createRef < NSVElement < NativeScriptPager > > ( ) ;
94
93
private readonly argsViewToRootKeyAndRef : Map < View , RootKeyAndTNSView > = new Map ( ) ;
95
94
private roots : Set < string > = new Set ( ) ;
96
95
97
-
98
96
private readonly defaultOnItemLoading : ( args : any ) => void = ( args : any ) => {
99
97
const { logLevel, onCellRecycle, onCellFirstLoad } = this . props . _debug ;
100
98
const { items, itemTemplateSelector } = this . props ;
101
99
const item : any = _Pager . isItemsSource ( items ) ? items . getItem ( args . index ) : items [ args . index ] ;
102
100
const template : string | null = itemTemplateSelector
103
- ? typeof itemTemplateSelector === " string"
101
+ ? typeof itemTemplateSelector === ' string'
104
102
? itemTemplateSelector
105
- : ( itemTemplateSelector as ( ( item : any , index : number , items : any ) => string ) ) ( item , args . index , items )
103
+ : ( itemTemplateSelector as ( item : any , index : number , items : any ) => string ) ( item , args . index , items )
106
104
: null ;
107
105
const cellFactory : CellFactory | undefined =
108
- template === null
109
- ? this . props . cellFactory
110
- : this . props . cellFactories
111
- ? this . props . cellFactories . get ( template ) . cellFactory
112
- : this . props . cellFactory ;
106
+ template === null ? this . props . cellFactory : this . props . cellFactories ? this . props . cellFactories . get ( template ) . cellFactory : this . props . cellFactory ;
113
107
114
- if ( typeof cellFactory === " undefined" ) {
108
+ if ( typeof cellFactory === ' undefined' ) {
115
109
console . warn ( `Pager: No cell factory found, given template ${ template } !` ) ;
116
110
return ;
117
111
}
118
112
119
- let view : View | undefined = args . view ;
113
+ const view : View | undefined = args . view ;
120
114
if ( ! view ) {
121
115
const rootKeyAndRef : RootKeyAndTNSView = this . renderNewRoot ( item , cellFactory ) ;
122
116
@@ -127,16 +121,16 @@ export class _Pager extends React.Component<Props, State> {
127
121
128
122
if ( onCellFirstLoad ) onCellFirstLoad ( rootKeyAndRef . nativeView ) ;
129
123
} else {
130
- console . log ( ` [Pager] existing view: ` , view ) ;
131
- if ( onCellRecycle ) onCellRecycle ( view as CellViewContainer ) ;
124
+ console . log ( ' [Pager] existing view: ' , view ) ;
125
+ if ( onCellRecycle ) onCellRecycle ( view ) ;
132
126
133
127
const { rootKey, nativeView } = this . argsViewToRootKeyAndRef . get ( view ) ;
134
- if ( typeof rootKey === " undefined" ) {
135
- console . error ( ` Unable to find root key that args.view corresponds to!` , view ) ;
128
+ if ( typeof rootKey === ' undefined' ) {
129
+ console . error ( ' Unable to find root key that args.view corresponds to!' , view ) ;
136
130
return ;
137
131
}
138
132
if ( ! nativeView ) {
139
- console . error ( ` Unable to find ref that args.view corresponds to!` , view ) ;
133
+ console . error ( ' Unable to find ref that args.view corresponds to!' , view ) ;
140
134
return ;
141
135
}
142
136
@@ -153,23 +147,24 @@ export class _Pager extends React.Component<Props, State> {
153
147
} ;
154
148
155
149
protected getNativeView ( ) : NativeScriptPager | null {
156
- const ref = ( this . props . forwardedRef || this . myRef ) ;
150
+ const ref = this . props . forwardedRef || this . myRef ;
157
151
return ref . current ? ref . current . nativeView : null ;
158
152
}
159
153
160
154
private readonly renderNewRoot = ( item : any , cellFactory : CellFactory ) : RootKeyAndTNSView => {
161
155
const node : NativeScriptPager | null = this . getNativeView ( ) ;
162
156
if ( ! node ) {
163
- throw new Error ( " Unable to get ref to Pager" ) ;
157
+ throw new Error ( ' Unable to get ref to Pager' ) ;
164
158
}
165
159
166
- console . log ( ` [Pager] no existing view.` ) ;
160
+ console . log ( ' [Pager] no existing view.' ) ;
167
161
const rootKey : string = `Pager-${ node . _domId } -${ this . roots . size . toString ( ) } ` ;
168
162
169
163
const root = new NSVRoot < View > ( ) ;
170
164
RNSRender (
171
165
cellFactory ( item ) ,
172
- root , ( ) => {
166
+ root ,
167
+ ( ) => {
173
168
// console.log(`Rendered into cell! ref:`);
174
169
} ,
175
170
rootKey
@@ -186,7 +181,7 @@ export class _Pager extends React.Component<Props, State> {
186
181
componentDidMount ( ) {
187
182
const node : NativeScriptPager | null = this . getNativeView ( ) ;
188
183
if ( ! node ) {
189
- console . warn ( ` React ref to NativeScript View lost, so unable to set item templates.` ) ;
184
+ console . warn ( ' React ref to NativeScript View lost, so unable to set item templates.' ) ;
190
185
return ;
191
186
}
192
187
@@ -203,26 +198,24 @@ export class _Pager extends React.Component<Props, State> {
203
198
this . argsViewToRootKeyAndRef . set ( rootKeyAndRef . nativeView , rootKeyAndRef ) ;
204
199
205
200
return rootKeyAndRef . nativeView ;
206
- } ,
201
+ }
207
202
} ) ;
208
203
} ) ;
209
204
node . itemTemplates = itemTemplates ;
210
205
}
211
206
}
212
207
213
208
componentWillUnmount ( ) {
214
- this . roots . forEach ( root => unmountComponentAtNode ( root ) ) ;
209
+ this . roots . forEach ( ( root ) => unmountComponentAtNode ( root ) ) ;
215
210
}
216
211
217
212
public static isItemsSource ( arr : any [ ] | ItemsSource ) : arr is ItemsSource {
218
213
// Same implementation as: https://github.com/NativeScript/NativeScript/blob/b436ecde3605b695a0ffa1757e38cc094e2fe311/tns-core-modules/ui/list-picker/list-picker-common.ts#L74
219
- return typeof ( arr as ItemsSource ) . getItem === " function" ;
214
+ return typeof ( arr as ItemsSource ) . getItem === ' function' ;
220
215
}
221
216
222
-
223
-
224
217
render ( ) {
225
- console . log ( ` Pager's render()` ) ;
218
+ console . log ( " Pager's render()" ) ;
226
219
const {
227
220
// Only used by the class component; not the JSX element.
228
221
forwardedRef,
@@ -242,25 +235,20 @@ export class _Pager extends React.Component<Props, State> {
242
235
// ref: forwardedRef || this.myRef,
243
236
// onItemLoading: this.defaultOnItemLoading
244
237
// }, children)
245
- < pager
246
- { ...rest }
247
- onItemLoading = { this . defaultOnItemLoading }
248
- ref = { forwardedRef || this . myRef }
249
- children = { children }
250
- />
238
+ < pager { ...rest } onItemLoading = { this . defaultOnItemLoading } ref = { forwardedRef || this . myRef } children = { children } />
251
239
) ;
252
240
}
253
241
}
254
242
255
- export type PagerItemAttributes = GridLayoutAttributes & { forwardedRef ?: React . RefObject < NSVElement < NativeScriptPagerItem > > } ;
243
+ export type PagerItemAttributes = GridLayoutAttributes & { forwardedRef ?: React . RefObject < NSVElement < NativeScriptPagerItem > > } ;
256
244
257
245
export class _PagerItem extends React . Component < PagerItemAttributes , { } > {
258
246
private readonly myRef = React . createRef < NSVElement < NativeScriptPagerItem > > ( ) ;
259
247
private readonly item = new NativeScriptPagerItem ( ) ;
260
248
261
249
componentDidMount ( ) : void {
262
- const { forwardedRef} = this . props ;
263
- const view = ( forwardedRef || this . myRef ) . current ! . nativeView ;
250
+ const { forwardedRef } = this . props ;
251
+ const view = ( forwardedRef || this . myRef ) . current . nativeView ;
264
252
const parent : any = view && view . parent ? view . parent : null ;
265
253
if ( parent ) {
266
254
// remove parent
@@ -281,38 +269,30 @@ export class _PagerItem extends React.Component<PagerItemAttributes, {}> {
281
269
...rest
282
270
} = this . props ;
283
271
284
-
285
272
return React . createElement (
286
273
'pagerItem' ,
287
274
{
288
275
...rest ,
289
- ref : forwardedRef || this . myRef ,
276
+ ref : forwardedRef || this . myRef
290
277
} ,
291
278
children
292
279
) ;
293
280
}
294
281
}
295
282
296
- export const Pager = React . forwardRef < NSVElement < NativeScriptPager > , OwnProps > (
297
- ( props : OwnProps , ref : React . RefObject < NSVElement < NativeScriptPager > > ) => {
298
- return < _Pager { ...props } forwardedRef = { ref } /> ;
299
- }
300
- ) ;
301
-
302
-
303
- export const PagerItem = React . forwardRef < NSVElement < NativeScriptPagerItem > , PagerItemAttributes > (
304
- ( props : PagerItemAttributes , ref : React . RefObject < NSVElement < NativeScriptPagerItem > > ) => {
305
- return < _PagerItem { ...props } forwardedRef = { ref } /> ;
306
- }
307
- ) ;
308
-
283
+ //@ts -ignore
284
+ export const Pager = React . forwardRef < NSVElement < NativeScriptPager > , OwnProps > ( ( props : OwnProps , ref : React . RefObject < NSVElement < NativeScriptPager > > ) => < _Pager { ...props } forwardedRef = { ref } /> ) ;
309
285
286
+ export const PagerItem = React . forwardRef < NSVElement < NativeScriptPagerItem > , PagerItemAttributes > ( ( props : PagerItemAttributes , ref : React . RefObject < NSVElement < NativeScriptPagerItem > > ) => (
287
+ //@ts -ignore
288
+ < _PagerItem { ...props } forwardedRef = { ref } />
289
+ ) ) ;
310
290
311
291
declare global {
312
- module JSX {
292
+ namespace JSX {
313
293
interface IntrinsicElements {
314
294
pager : NativeScriptProps < PagerAttributes , NativeScriptPager > ;
315
- pagerItem : NativeScriptProps < PagerItemAttributes , NativeScriptPagerItem >
295
+ pagerItem : NativeScriptProps < PagerItemAttributes , NativeScriptPagerItem > ;
316
296
}
317
297
}
318
- }
298
+ }
0 commit comments