-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgrid.js
103 lines (93 loc) · 2.5 KB
/
grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, { useRef, useImperativeHandle } from 'react';
import {
requireNativeComponent,
UIManager,
findNodeHandle,
} from 'react-native';
const LeanbackGrid6Col = requireNativeComponent('LeanbackGrid6Col', null);
const LeanbackGrid5Col = requireNativeComponent('LeanbackGrid5Col', null);
const LeanbackGrid4Col = requireNativeComponent('LeanbackGrid4Col', null);
const REQUEST_FOCUS_ACTION = 'request-focus';
const getGridView = (numOfCols) => {
switch (numOfCols) {
case 6:
return LeanbackGrid6Col;
case 5:
return LeanbackGrid5Col;
default:
return LeanbackGrid4Col;
}
};
const Grid = React.forwardRef(
(
{
attributes,
forbiddenFocusDirections,
showOnlyFocusedInfo,
nextFocusUpId,
nextFocusDownId,
nextFocusLeftId,
nextFocusRightId,
data,
numOfCols,
...restOfProps
},
ref
) => {
const rowRef = useRef();
const attrs = {
data,
attributes: {
width: attributes?.width || 513,
height: attributes?.height || 176,
forbiddenFocusDirections:
forbiddenFocusDirections && Array.isArray(forbiddenFocusDirections)
? forbiddenFocusDirections
: [],
nextFocusUpId: nextFocusUpId || -1,
nextFocusDownId: nextFocusDownId || -1,
nextFocusLeftId: nextFocusLeftId || -1,
nextFocusRightId: nextFocusRightId || -1,
cardShape: attributes?.cardShape || 'square',
borderRadius: 0,
showOnlyFocusedInfo: showOnlyFocusedInfo ?? false,
hasImageOnly: attributes.hasImageOnly ?? false,
imageTransformationMode:
attributes?.imageTransformationMode || 'fitCenter',
},
};
useImperativeHandle(ref, () => ({
requestFocus: () => {
requestFocus();
},
}));
const requestFocus = () => {
const node = findNodeHandle(rowRef.current);
UIManager.dispatchViewManagerCommand(node, REQUEST_FOCUS_ACTION, []);
};
const GridView = getGridView(numOfCols);
return (
<GridView
{...restOfProps}
ref={rowRef}
dataAndAttributes={attrs}
onFocus={(event) => {
const { item } = event.nativeEvent;
if (restOfProps.onFocus) restOfProps.onFocus(JSON.parse(item));
}}
onPress={(event) => {
const { item } = event.nativeEvent;
if (restOfProps.onPress) restOfProps.onPress(JSON.parse(item));
}}
onDataIdsReady={(event) => {
const { data: stringifiedData } = event.nativeEvent;
if (restOfProps.onDataIdsReady) {
const data = JSON.parse(stringifiedData);
if (data.length) restOfProps.onDataIdsReady(data);
}
}}
/>
);
}
);
export default Grid;