-
Notifications
You must be signed in to change notification settings - Fork 8
/
row.js
110 lines (101 loc) · 2.85 KB
/
row.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
104
105
106
107
108
109
110
import React, { useRef, useImperativeHandle } from 'react';
import {
requireNativeComponent,
UIManager,
findNodeHandle,
} from 'react-native';
const LeanbackNativeRow = requireNativeComponent('LeanbackRow');
const REQUEST_FOCUS_ACTION = 'request-focus';
const Row = React.forwardRef(
(
{
attributes,
forbiddenFocusDirections,
nextFocusUpId,
nextFocusDownId,
nextFocusLeftId,
nextFocusRightId,
imageTransformationMode,
showOnlyFocusedInfo,
data,
...restOfProps
},
ref
) => {
const rowRef = useRef();
// Validate timestamps
data.forEach((item) => {
if (
typeof item.programStartTimestamp === 'string' &&
item?.programStartTimestamp?.length &&
item?.programStartTimestamp?.length !== 13
)
throw new Error(
'Timestamp is of incorrect format. Must meet JS standart - miliseconds!'
);
if (
typeof item.programEndTimestamp === 'string' &&
item?.programStartTimestamp?.length &&
item?.programEndTimestamp?.length !== 13
)
throw new Error(
'Timestamp is of incorrect format. Must meet JS standart - miliseconds!'
);
});
const attrs = {
data,
attributes: {
width: attributes?.width || 513,
height: attributes?.height || 176,
forbiddenFocusDirections:
forbiddenFocusDirections && Array.isArray(forbiddenFocusDirections)
? forbiddenFocusDirections
: [],
focusedCardAlignment: attributes?.focusedCardAlignment || 'left',
numberOfRows: attributes?.numberOfRows || 1,
nextFocusUpId: nextFocusUpId || -1,
nextFocusDownId: nextFocusDownId || -1,
nextFocusLeftId: nextFocusLeftId || -1,
nextFocusRightId: nextFocusRightId || -1,
cardShape: attributes?.cardShape || 'square',
borderRadius: attributes?.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, []);
};
return (
<LeanbackNativeRow
{...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 Row;