@amazon-devices/vega-carousel 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +0 -0
- package/dist/Carousel/Carousel.d.ts +16 -0
- package/dist/Carousel/Carousel.js +102 -0
- package/dist/Carousel/Utils.d.ts +4 -0
- package/dist/Carousel/Utils.js +35 -0
- package/dist/Carousel/index.d.ts +7 -0
- package/dist/Carousel/index.js +23 -0
- package/dist/Carousel/types.d.ts +406 -0
- package/dist/Carousel/types.js +2 -0
- package/dist/CarouselData/CarouselDataAdapter.d.ts +141 -0
- package/dist/CarouselData/CarouselDataAdapter.js +67 -0
- package/dist/CarouselData/CarouselDataResolver.d.ts +100 -0
- package/dist/CarouselData/CarouselDataResolver.js +224 -0
- package/dist/CarouselData/CarouselDataTypes.d.ts +280 -0
- package/dist/CarouselData/CarouselDataTypes.js +84 -0
- package/dist/CarouselData/index.d.ts +3 -0
- package/dist/CarouselData/index.js +24 -0
- package/dist/CarouselScrollable/CarouselScrollable.d.ts +67 -0
- package/dist/CarouselScrollable/CarouselScrollable.js +77 -0
- package/dist/CarouselScrollable/CarouselScrollableProps.d.ts +499 -0
- package/dist/CarouselScrollable/CarouselScrollableProps.js +211 -0
- package/dist/CarouselScrollable/CarouselScrollableRef.d.ts +80 -0
- package/dist/CarouselScrollable/CarouselScrollableRef.js +7 -0
- package/dist/CarouselScrollable/CarouselScrollableTypes.d.ts +224 -0
- package/dist/CarouselScrollable/CarouselScrollableTypes.js +116 -0
- package/dist/CarouselScrollable/CarouselSimple.d.ts +71 -0
- package/dist/CarouselScrollable/CarouselSimple.js +111 -0
- package/dist/CarouselScrollable/CarouselSimpleProps.d.ts +29 -0
- package/dist/CarouselScrollable/CarouselSimpleProps.js +7 -0
- package/dist/CarouselScrollable/index.d.ts +6 -0
- package/dist/CarouselScrollable/index.js +27 -0
- package/dist/CarouselScrollable/native/CarouselNativeHostComponent.d.ts +109 -0
- package/dist/CarouselScrollable/native/CarouselNativeHostComponent.js +82 -0
- package/dist/CarouselScrollable/native/CarouselNativeItemHostComponent.d.ts +94 -0
- package/dist/CarouselScrollable/native/CarouselNativeItemHostComponent.js +42 -0
- package/dist/CarouselScrollable/native/CarouselNativeItemWrapper.d.ts +255 -0
- package/dist/CarouselScrollable/native/CarouselNativeItemWrapper.js +276 -0
- package/dist/CarouselScrollable/native/CarouselNativeLogic.d.ts +209 -0
- package/dist/CarouselScrollable/native/CarouselNativeLogic.js +349 -0
- package/dist/CarouselScrollable/native/CarouselNativeWrapper.d.ts +68 -0
- package/dist/CarouselScrollable/native/CarouselNativeWrapper.js +164 -0
- package/dist/CarouselScrollable/native/index.d.ts +5 -0
- package/dist/CarouselScrollable/native/index.js +26 -0
- package/dist/CarouselScrollable/recyclerview/JSRecyclerViewList.d.ts +25 -0
- package/dist/CarouselScrollable/recyclerview/JSRecyclerViewList.js +150 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +24 -0
- package/dist/kepler/aarch64-debug/lib/com.amazon.keplerscript.carousel.so +0 -0
- package/dist/kepler/aarch64-debug/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
- package/dist/kepler/aarch64-release/lib/com.amazon.keplerscript.carousel.so +0 -0
- package/dist/kepler/aarch64-release/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
- package/dist/kepler/armv7-debug/lib/com.amazon.keplerscript.carousel.so +0 -0
- package/dist/kepler/armv7-debug/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
- package/dist/kepler/armv7-release/lib/com.amazon.keplerscript.carousel.so +0 -0
- package/dist/kepler/armv7-release/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
- package/dist/kepler/tm-manifest.json +1 -0
- package/dist/kepler/x86_64-debug/lib/com.amazon.keplerscript.carousel.so +0 -0
- package/dist/kepler/x86_64-debug/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
- package/dist/kepler/x86_64-release/lib/com.amazon.keplerscript.carousel.so +0 -0
- package/dist/kepler/x86_64-release/lib/com.amazon.keplerscript.carousel.so.debug +0 -0
- package/dist/utils/CarouselLogger.d.ts +295 -0
- package/dist/utils/CarouselLogger.js +370 -0
- package/dist/utils/CarouselReactNativeUtilities.d.ts +108 -0
- package/dist/utils/CarouselReactNativeUtilities.js +150 -0
- package/dist/utils/HelperTypes.d.ts +5 -0
- package/dist/utils/HelperTypes.js +7 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +24 -0
- package/package.json +1 -0
- package/react-native.config.js +22 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { CarouselScrollableDataAdapterProps, CarouselScrollableResolverProps } from '../CarouselScrollableProps';
|
|
3
|
+
import { ScaleFactor, SelectionBorderStyle, SelectionBorderStrategy } from '../CarouselScrollableTypes';
|
|
4
|
+
import { ViewProps } from 'react-native';
|
|
5
|
+
/**
|
|
6
|
+
* Internal properties used by the wrapper item object used as children of the
|
|
7
|
+
* {@link CarouselNativeWrapper} component.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link CarouselNativeWrapper}
|
|
10
|
+
* @see {@link CarouselScrollableDataAdapterProps}
|
|
11
|
+
* @see {@link CarouselScrollableResolverProps}
|
|
12
|
+
*/
|
|
13
|
+
export type CarouselNativeItemProps<ItemT, KeyT = React.Key> = ViewProps & CarouselScrollableDataAdapterProps<ItemT, KeyT> & CarouselScrollableResolverProps<ItemT> & {
|
|
14
|
+
/**
|
|
15
|
+
* The parent's identifier ({@link CarouselScrollableProps.uniqueId}) for the list
|
|
16
|
+
* containing the item.
|
|
17
|
+
*
|
|
18
|
+
* Different from the parent's {@link CarouselScrollableProps.uniqueId}, this property
|
|
19
|
+
* has functional value, as, together with the {@link dataIndex}, it is used
|
|
20
|
+
* to determine when this item is being recycled.
|
|
21
|
+
*
|
|
22
|
+
* @default undefined
|
|
23
|
+
* @see {@link CarouselScrollableProps}
|
|
24
|
+
* @see {@link dataIndex}
|
|
25
|
+
*/
|
|
26
|
+
parentId?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The data index associated with this item.
|
|
29
|
+
*
|
|
30
|
+
* This and the {@link parentId} are used to control when this item is being
|
|
31
|
+
* recycled.
|
|
32
|
+
*
|
|
33
|
+
* @default undefined
|
|
34
|
+
* @see {@link parentId}
|
|
35
|
+
*/
|
|
36
|
+
dataIndex?: number;
|
|
37
|
+
/**
|
|
38
|
+
* The shift associated with this item.
|
|
39
|
+
*
|
|
40
|
+
* @default undefined
|
|
41
|
+
* @see {@link parentId}
|
|
42
|
+
*/
|
|
43
|
+
itemShift?: ScaleFactor;
|
|
44
|
+
/**
|
|
45
|
+
* When this flag is set to `true`, the selected item will have a styleable
|
|
46
|
+
* border enveloping the selected item.
|
|
47
|
+
*
|
|
48
|
+
* The look-and-feel of the selection border can be configured by the
|
|
49
|
+
* {@link selectionBorderStyle} property.
|
|
50
|
+
*
|
|
51
|
+
* @default false
|
|
52
|
+
* @see {@link selectionBorderStyle}
|
|
53
|
+
*/
|
|
54
|
+
showSelectionBorder?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* This property defines the look-and-feel of the border surrounding the
|
|
57
|
+
* selected item when it is enabled.
|
|
58
|
+
*/
|
|
59
|
+
selectionBorderStyle?: SelectionBorderStyle;
|
|
60
|
+
/**
|
|
61
|
+
* This property defines the strategy used for rendering the selection border
|
|
62
|
+
* relative to the content area.
|
|
63
|
+
*/
|
|
64
|
+
selectionBorderStrategy?: SelectionBorderStrategy;
|
|
65
|
+
/**
|
|
66
|
+
* The scale factor for an item being activated by the user (e.g., the user
|
|
67
|
+
* pressed down the select button in the device's remote control).
|
|
68
|
+
*
|
|
69
|
+
* @default 1.0
|
|
70
|
+
* @see {@link CarouselScrollableConfigProps.pressedItemScaleFactor}
|
|
71
|
+
*/
|
|
72
|
+
pressedItemScaleFactor: number;
|
|
73
|
+
/**
|
|
74
|
+
* The scale factor for selected items set on the parent's list.
|
|
75
|
+
*
|
|
76
|
+
* @default 1.0
|
|
77
|
+
* @see {@link CarouselScrollableConfigProps.focusedItemScaleFactor}
|
|
78
|
+
*/
|
|
79
|
+
focusedItemScaleFactor: number;
|
|
80
|
+
shiftItemsScaleFactor: ScaleFactor;
|
|
81
|
+
/**
|
|
82
|
+
* This element is used on the very first time the {@link CarouselNativeItemWrapper}
|
|
83
|
+
* resolvers.
|
|
84
|
+
*
|
|
85
|
+
* Further resolvers will use the {@link dataAdapter} and {@link renderItem}
|
|
86
|
+
* properties to generate the {@link React.ReactElement} to be used.
|
|
87
|
+
*/
|
|
88
|
+
initialElement?: React.ReactElement;
|
|
89
|
+
initialKey?: KeyT;
|
|
90
|
+
/**
|
|
91
|
+
* This function is called whenever the item receives a message from the
|
|
92
|
+
* native layer telling it to rebind itself to another data item.
|
|
93
|
+
*
|
|
94
|
+
* @param {number} index The `dataIndex` this item had before the binding.
|
|
95
|
+
*/
|
|
96
|
+
unbindCallback: (index: number) => void;
|
|
97
|
+
/**
|
|
98
|
+
* This function is called whenever the item receives a message from the
|
|
99
|
+
* native layer telling it to rebind itself to another data item.
|
|
100
|
+
*
|
|
101
|
+
* @param {number} index The `dataIndex` this item will have **after** the
|
|
102
|
+
* binding.
|
|
103
|
+
*/
|
|
104
|
+
bindCallback: (index: number) => void;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Type representing a list item state. Changes on any of the fields in this
|
|
108
|
+
* type generate a re-render of the object.
|
|
109
|
+
*/
|
|
110
|
+
export type CarouselNativeItemState = {
|
|
111
|
+
/**
|
|
112
|
+
* The data index associated with this item. For details, check the
|
|
113
|
+
* documentation of {@link CarouselNativeItemProps.dataIndex}.
|
|
114
|
+
*
|
|
115
|
+
* @see {@link CarouselNativeItemProps.dataIndex}
|
|
116
|
+
*/
|
|
117
|
+
dataIndex: number;
|
|
118
|
+
/**
|
|
119
|
+
* The parent's identifier ({@link CarouselScrollableProps.uniqueId}) for the list
|
|
120
|
+
* containing the item. For details, check the documentation of
|
|
121
|
+
* {@link CarouselNativeItemProps.parentId}.
|
|
122
|
+
*
|
|
123
|
+
* @see {@link CarouselNativeItemProps.parentId}
|
|
124
|
+
*/
|
|
125
|
+
parentId?: string;
|
|
126
|
+
/**
|
|
127
|
+
* This field is used by the "force-refresh" mechanism. It flips its value any
|
|
128
|
+
* time one needs to cause a re-render of this component.
|
|
129
|
+
*/
|
|
130
|
+
refreshToggle: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* This field is used for specifying per-item shift value.
|
|
133
|
+
*/
|
|
134
|
+
itemShift?: ScaleFactor;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Class component used to hoist a list item from {@link CarouselScrollable}.
|
|
138
|
+
*
|
|
139
|
+
* Although functional components are generally more performant than class ones,
|
|
140
|
+
* a functional component will always re-render when the parent re-renders, and
|
|
141
|
+
* for a recyclable list use-case, we need more control over _when_ that will
|
|
142
|
+
* happen.
|
|
143
|
+
*
|
|
144
|
+
* The `CarouselNativeItem` component will also be the entry point of a re-bind
|
|
145
|
+
* operation.
|
|
146
|
+
*
|
|
147
|
+
* @template ItemT The type of data item this component will use to resolve its
|
|
148
|
+
* child component.
|
|
149
|
+
* @template KeyT The type used as items unique identifiers (also called _keys_,
|
|
150
|
+
* or _stable IDs_) when verifying if this component should update;
|
|
151
|
+
*
|
|
152
|
+
* @see {@link CarouselNativeWrapper}
|
|
153
|
+
* @see {@link CarouselScrollable}
|
|
154
|
+
*/
|
|
155
|
+
export declare class CarouselNativeItemWrapper<ItemT, KeyT = React.Key> extends React.Component<CarouselNativeItemProps<ItemT, KeyT>, CarouselNativeItemState> {
|
|
156
|
+
private currentKey;
|
|
157
|
+
private useInitialElement;
|
|
158
|
+
constructor(props: CarouselNativeItemProps<ItemT, KeyT>);
|
|
159
|
+
/**
|
|
160
|
+
* Function called by the framework right before calling {@link render}, both
|
|
161
|
+
* on the initial mount, and on the following updates.
|
|
162
|
+
*
|
|
163
|
+
* The framework will use the returned state to check if the component's state
|
|
164
|
+
* changed, and to pass it to {@link shouldComponentUpdate}.
|
|
165
|
+
*
|
|
166
|
+
* When this function returns a new state the framework will re-render this
|
|
167
|
+
* component, even if {@link shouldComponentUpdate} returns `false`.
|
|
168
|
+
*
|
|
169
|
+
* @param {CarouselNativeItemProps<ItemT, KeyT>} props The next props that
|
|
170
|
+
* the component is about to render with.
|
|
171
|
+
* @param {CarouselNativeItemState} state The next state that the component
|
|
172
|
+
* is about to render with.
|
|
173
|
+
*
|
|
174
|
+
* @return {CarouselNativeItemState | null} Returns an object to update the
|
|
175
|
+
* component's state, or `null` to update nothing.
|
|
176
|
+
*
|
|
177
|
+
* @see [Official API documentation](https://react.dev/reference/react/Component#static-getderivedstatefromprops)
|
|
178
|
+
*/
|
|
179
|
+
static getDerivedStateFromProps<ItemT, KeyT>(props: CarouselNativeItemProps<ItemT, KeyT>, state: CarouselNativeItemState): CarouselNativeItemState | null;
|
|
180
|
+
/**
|
|
181
|
+
* Component should re-resolve if:
|
|
182
|
+
*
|
|
183
|
+
* 1. The `dataIndex` is different (component is changing frames);
|
|
184
|
+
* 2. The `refreshToggle` changed (it's a force refresh);
|
|
185
|
+
* 3. The item's key is different (component is changed within the same
|
|
186
|
+
* frame at the same location)
|
|
187
|
+
*
|
|
188
|
+
* When the third update state can be performance costly since it retrieves
|
|
189
|
+
* the data item and its key from the already rendered item, plus the one to
|
|
190
|
+
* be updated, even though, when we have an update, the new data item will be
|
|
191
|
+
* re-resolved.
|
|
192
|
+
*
|
|
193
|
+
* @param {Readonly<CarouselNativeItemProps<ItemT, KeyT>>} nextProps The
|
|
194
|
+
* updated properties for the list item.
|
|
195
|
+
* @param {Readonly<CarouselNativeItemState>} nextState The next state
|
|
196
|
+
* calculated by the {@link getDerivedStateFromProps} function.
|
|
197
|
+
*
|
|
198
|
+
* @return {boolean} Returns `true` if the component should update, and
|
|
199
|
+
* `false` otherwise.
|
|
200
|
+
*
|
|
201
|
+
* @see [Official API documentation](https://react.dev/reference/react/Component#shouldcomponentupdate)
|
|
202
|
+
*/
|
|
203
|
+
shouldComponentUpdate(nextProps: Readonly<CarouselNativeItemProps<ItemT, KeyT>>, nextState: Readonly<CarouselNativeItemState>): boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Called whenever the framework needs the component's definition to render a
|
|
206
|
+
* view in the screen.
|
|
207
|
+
*
|
|
208
|
+
* When this method is called, there is nothing for this component to resolve,
|
|
209
|
+
* so this call is as fast as it can be. However, be mindful that the other
|
|
210
|
+
* lifecycle methods in this class were already called, and the _perceived_
|
|
211
|
+
* render performance is a combination of all these methods.
|
|
212
|
+
*
|
|
213
|
+
* @see [Official API documentation](https://react.dev/reference/react/Component#render)
|
|
214
|
+
*/
|
|
215
|
+
render(): React.ReactNode;
|
|
216
|
+
/**
|
|
217
|
+
* Callback called by the native layer whenever it emits the `bind` event.
|
|
218
|
+
*
|
|
219
|
+
* This function will only perform any operation if the target bind index is
|
|
220
|
+
* different from the one it already has defined.
|
|
221
|
+
*
|
|
222
|
+
* @param {CarouselNativeItemHostComponentBindEvent} nativeEvent The event
|
|
223
|
+
* emitted by the native layer.
|
|
224
|
+
*
|
|
225
|
+
* @see {@link CarouselNativeItemHostComponentBindEvent}
|
|
226
|
+
*/
|
|
227
|
+
private onBind;
|
|
228
|
+
/**
|
|
229
|
+
* Data pooling function. This function will use the {@link resolveDataItems}
|
|
230
|
+
* function to retrieve and resolve an item at the component's data index
|
|
231
|
+
* position.
|
|
232
|
+
*
|
|
233
|
+
* @param {CarouselNativeItemState} state An optional state used to read the
|
|
234
|
+
* item's data index instead of the component's state.
|
|
235
|
+
*
|
|
236
|
+
* @param {CarouselNativeItemProps} props Optional props used to override the current props if newer props are known
|
|
237
|
+
* @return {React.ReactElement | undefined} Returns the fully resolved
|
|
238
|
+
* element or `undefined` if it is not found.
|
|
239
|
+
*
|
|
240
|
+
* @see {@link CarouselNativeItemState}
|
|
241
|
+
* @see {@link resolveDataItems}
|
|
242
|
+
*/
|
|
243
|
+
private resolveUpdatedItemContent;
|
|
244
|
+
/**
|
|
245
|
+
* Convenience method to render a native component with the
|
|
246
|
+
* {@link itemElement} as its child component.
|
|
247
|
+
*/
|
|
248
|
+
private renderStateItem;
|
|
249
|
+
/**
|
|
250
|
+
* This property is used to cache the resolved element of this item.
|
|
251
|
+
*
|
|
252
|
+
* @private
|
|
253
|
+
*/
|
|
254
|
+
private itemElement;
|
|
255
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2024 - 2025 Amazon.com, Inc. or its affiliates. All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* PROPRIETARY/CONFIDENTIAL. USE IS SUBJECT TO LICENSE TERMS.
|
|
6
|
+
*/
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.CarouselNativeItemWrapper = void 0;
|
|
12
|
+
const react_1 = __importDefault(require("react"));
|
|
13
|
+
const CarouselNativeItemHostComponent_1 = require("./CarouselNativeItemHostComponent");
|
|
14
|
+
const CarouselData_1 = require("../../CarouselData");
|
|
15
|
+
const CarouselScrollableTypes_1 = require("../CarouselScrollableTypes");
|
|
16
|
+
const utils_1 = require("../../utils");
|
|
17
|
+
const react_native_1 = require("react-native");
|
|
18
|
+
const defaultBorderStyle = {
|
|
19
|
+
borderColor: '#ffffff',
|
|
20
|
+
borderWidth: 8,
|
|
21
|
+
borderRadius: 8,
|
|
22
|
+
borderStrokeColor: '#000000',
|
|
23
|
+
borderStrokeWidth: 3,
|
|
24
|
+
borderStrokeRadius: 4,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Class component used to hoist a list item from {@link CarouselScrollable}.
|
|
28
|
+
*
|
|
29
|
+
* Although functional components are generally more performant than class ones,
|
|
30
|
+
* a functional component will always re-render when the parent re-renders, and
|
|
31
|
+
* for a recyclable list use-case, we need more control over _when_ that will
|
|
32
|
+
* happen.
|
|
33
|
+
*
|
|
34
|
+
* The `CarouselNativeItem` component will also be the entry point of a re-bind
|
|
35
|
+
* operation.
|
|
36
|
+
*
|
|
37
|
+
* @template ItemT The type of data item this component will use to resolve its
|
|
38
|
+
* child component.
|
|
39
|
+
* @template KeyT The type used as items unique identifiers (also called _keys_,
|
|
40
|
+
* or _stable IDs_) when verifying if this component should update;
|
|
41
|
+
*
|
|
42
|
+
* @see {@link CarouselNativeWrapper}
|
|
43
|
+
* @see {@link CarouselScrollable}
|
|
44
|
+
*/
|
|
45
|
+
class CarouselNativeItemWrapper extends react_1.default.Component {
|
|
46
|
+
currentKey;
|
|
47
|
+
useInitialElement;
|
|
48
|
+
constructor(props) {
|
|
49
|
+
super(props);
|
|
50
|
+
this.state = {
|
|
51
|
+
dataIndex: this.props.dataIndex ?? -1,
|
|
52
|
+
parentId: this.props.parentId,
|
|
53
|
+
refreshToggle: false,
|
|
54
|
+
itemShift: this.props.itemShift,
|
|
55
|
+
};
|
|
56
|
+
this.currentKey = this.props.initialKey;
|
|
57
|
+
this.useInitialElement = true;
|
|
58
|
+
}
|
|
59
|
+
////////////////////////////////////////
|
|
60
|
+
/// Lifecycle methods
|
|
61
|
+
/**
|
|
62
|
+
* Function called by the framework right before calling {@link render}, both
|
|
63
|
+
* on the initial mount, and on the following updates.
|
|
64
|
+
*
|
|
65
|
+
* The framework will use the returned state to check if the component's state
|
|
66
|
+
* changed, and to pass it to {@link shouldComponentUpdate}.
|
|
67
|
+
*
|
|
68
|
+
* When this function returns a new state the framework will re-render this
|
|
69
|
+
* component, even if {@link shouldComponentUpdate} returns `false`.
|
|
70
|
+
*
|
|
71
|
+
* @param {CarouselNativeItemProps<ItemT, KeyT>} props The next props that
|
|
72
|
+
* the component is about to render with.
|
|
73
|
+
* @param {CarouselNativeItemState} state The next state that the component
|
|
74
|
+
* is about to render with.
|
|
75
|
+
*
|
|
76
|
+
* @return {CarouselNativeItemState | null} Returns an object to update the
|
|
77
|
+
* component's state, or `null` to update nothing.
|
|
78
|
+
*
|
|
79
|
+
* @see [Official API documentation](https://react.dev/reference/react/Component#static-getderivedstatefromprops)
|
|
80
|
+
*/
|
|
81
|
+
static getDerivedStateFromProps(props, state) {
|
|
82
|
+
if (props.parentId !== state.parentId) {
|
|
83
|
+
return {
|
|
84
|
+
dataIndex: props.dataIndex ?? -1,
|
|
85
|
+
parentId: props.parentId,
|
|
86
|
+
refreshToggle: state.refreshToggle,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Component should re-resolve if:
|
|
93
|
+
*
|
|
94
|
+
* 1. The `dataIndex` is different (component is changing frames);
|
|
95
|
+
* 2. The `refreshToggle` changed (it's a force refresh);
|
|
96
|
+
* 3. The item's key is different (component is changed within the same
|
|
97
|
+
* frame at the same location)
|
|
98
|
+
*
|
|
99
|
+
* When the third update state can be performance costly since it retrieves
|
|
100
|
+
* the data item and its key from the already rendered item, plus the one to
|
|
101
|
+
* be updated, even though, when we have an update, the new data item will be
|
|
102
|
+
* re-resolved.
|
|
103
|
+
*
|
|
104
|
+
* @param {Readonly<CarouselNativeItemProps<ItemT, KeyT>>} nextProps The
|
|
105
|
+
* updated properties for the list item.
|
|
106
|
+
* @param {Readonly<CarouselNativeItemState>} nextState The next state
|
|
107
|
+
* calculated by the {@link getDerivedStateFromProps} function.
|
|
108
|
+
*
|
|
109
|
+
* @return {boolean} Returns `true` if the component should update, and
|
|
110
|
+
* `false` otherwise.
|
|
111
|
+
*
|
|
112
|
+
* @see [Official API documentation](https://react.dev/reference/react/Component#shouldcomponentupdate)
|
|
113
|
+
*/
|
|
114
|
+
shouldComponentUpdate(nextProps, nextState) {
|
|
115
|
+
let shouldUpdate = false;
|
|
116
|
+
const nextKey = (0, CarouselData_1.getItemKey)(nextProps.dataAdapter, nextState.dataIndex);
|
|
117
|
+
if (this.currentKey !== nextKey) {
|
|
118
|
+
shouldUpdate = true;
|
|
119
|
+
}
|
|
120
|
+
// the state will govern whether we should update the component
|
|
121
|
+
if (!shouldUpdate &&
|
|
122
|
+
(this.state.dataIndex !== nextState.dataIndex ||
|
|
123
|
+
this.state.parentId !== nextState.parentId ||
|
|
124
|
+
this.state.refreshToggle !== nextState.refreshToggle)) {
|
|
125
|
+
shouldUpdate = true;
|
|
126
|
+
}
|
|
127
|
+
if (!shouldUpdate &&
|
|
128
|
+
(this.props.focusedItemScaleFactor !== nextProps.focusedItemScaleFactor ||
|
|
129
|
+
this.props.shiftItemsScaleFactor.width !==
|
|
130
|
+
nextProps.shiftItemsScaleFactor.width ||
|
|
131
|
+
this.props.shiftItemsScaleFactor.height !==
|
|
132
|
+
nextProps.shiftItemsScaleFactor.height)) {
|
|
133
|
+
utils_1.Log.d('shouldComponentUpdate: PROPS CHANGED');
|
|
134
|
+
shouldUpdate = true;
|
|
135
|
+
}
|
|
136
|
+
// If the state did not determine if we should update, then check the new and current key
|
|
137
|
+
if (!shouldUpdate) {
|
|
138
|
+
if (nextKey !== this.currentKey) {
|
|
139
|
+
utils_1.Log.d('shouldComponentUpdate: KEYS CHANGED');
|
|
140
|
+
shouldUpdate = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// If we should update, ensure that the itemElement gets updated as well
|
|
144
|
+
if (shouldUpdate) {
|
|
145
|
+
this.currentKey = nextKey;
|
|
146
|
+
this.itemElement = this.resolveUpdatedItemContent(nextState, nextProps);
|
|
147
|
+
}
|
|
148
|
+
return shouldUpdate;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Called whenever the framework needs the component's definition to render a
|
|
152
|
+
* view in the screen.
|
|
153
|
+
*
|
|
154
|
+
* When this method is called, there is nothing for this component to resolve,
|
|
155
|
+
* so this call is as fast as it can be. However, be mindful that the other
|
|
156
|
+
* lifecycle methods in this class were already called, and the _perceived_
|
|
157
|
+
* render performance is a combination of all these methods.
|
|
158
|
+
*
|
|
159
|
+
* @see [Official API documentation](https://react.dev/reference/react/Component#render)
|
|
160
|
+
*/
|
|
161
|
+
render() {
|
|
162
|
+
try {
|
|
163
|
+
if (!this.itemElement) {
|
|
164
|
+
// This is the initial resolver of the item
|
|
165
|
+
if (this.useInitialElement && this.props.initialElement) {
|
|
166
|
+
this.itemElement = this.props.initialElement;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
this.itemElement = react_1.default.createElement(react_1.default.Fragment, null);
|
|
170
|
+
this.useInitialElement = false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return this.renderStateItem();
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
this.useInitialElement = false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
////////////////////////////////////////
|
|
180
|
+
/// Private callback functions
|
|
181
|
+
/**
|
|
182
|
+
* Callback called by the native layer whenever it emits the `bind` event.
|
|
183
|
+
*
|
|
184
|
+
* This function will only perform any operation if the target bind index is
|
|
185
|
+
* different from the one it already has defined.
|
|
186
|
+
*
|
|
187
|
+
* @param {CarouselNativeItemHostComponentBindEvent} nativeEvent The event
|
|
188
|
+
* emitted by the native layer.
|
|
189
|
+
*
|
|
190
|
+
* @see {@link CarouselNativeItemHostComponentBindEvent}
|
|
191
|
+
*/
|
|
192
|
+
onBind = (nativeEvent) => {
|
|
193
|
+
if (this.state.dataIndex !== nativeEvent.nativeEvent.index) {
|
|
194
|
+
const newState = {
|
|
195
|
+
...this.state,
|
|
196
|
+
dataIndex: nativeEvent.nativeEvent.index,
|
|
197
|
+
itemShift: (0, CarouselData_1.getSelectedItemOffset)(this.props.dataAdapter, nativeEvent.nativeEvent.index),
|
|
198
|
+
};
|
|
199
|
+
this.props.unbindCallback(this.state.dataIndex);
|
|
200
|
+
this.itemElement = this.resolveUpdatedItemContent(newState);
|
|
201
|
+
// Every time a frame gets re-bound/recycled update the currentKey
|
|
202
|
+
this.currentKey = (0, CarouselData_1.getItemKey)(this.props.dataAdapter, nativeEvent.nativeEvent.index);
|
|
203
|
+
this.setState(newState);
|
|
204
|
+
this.props.bindCallback(nativeEvent.nativeEvent.index);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const newState = {
|
|
208
|
+
...this.state,
|
|
209
|
+
refreshToggle: !this.state.refreshToggle,
|
|
210
|
+
itemShift: (0, CarouselData_1.getSelectedItemOffset)(this.props.dataAdapter, this.state.dataIndex),
|
|
211
|
+
};
|
|
212
|
+
this.setState(newState);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
////////////////////////////////////////
|
|
216
|
+
/// Private utility functions
|
|
217
|
+
/**
|
|
218
|
+
* Data pooling function. This function will use the {@link resolveDataItems}
|
|
219
|
+
* function to retrieve and resolve an item at the component's data index
|
|
220
|
+
* position.
|
|
221
|
+
*
|
|
222
|
+
* @param {CarouselNativeItemState} state An optional state used to read the
|
|
223
|
+
* item's data index instead of the component's state.
|
|
224
|
+
*
|
|
225
|
+
* @param {CarouselNativeItemProps} props Optional props used to override the current props if newer props are known
|
|
226
|
+
* @return {React.ReactElement | undefined} Returns the fully resolved
|
|
227
|
+
* element or `undefined` if it is not found.
|
|
228
|
+
*
|
|
229
|
+
* @see {@link CarouselNativeItemState}
|
|
230
|
+
* @see {@link resolveDataItems}
|
|
231
|
+
*/
|
|
232
|
+
resolveUpdatedItemContent = (state, props) => {
|
|
233
|
+
let element;
|
|
234
|
+
(0, CarouselData_1.resolveDataItems)(props?.dataAdapter ?? this.props.dataAdapter, props?.renderItem ?? this.props.renderItem, (0, CarouselData_1.dataReadAtTargetConfig)(state?.dataIndex ?? this.state.dataIndex), (resolved) => {
|
|
235
|
+
element = resolved.element;
|
|
236
|
+
});
|
|
237
|
+
// We need to update all undefined elements with an empty view in order to account for the loading state
|
|
238
|
+
// TODO : Update this logic once Scrollable getItem APIs support a loading state instead of just valid and invalid cases
|
|
239
|
+
if (!element) {
|
|
240
|
+
element = react_1.default.createElement(react_1.default.Fragment, null);
|
|
241
|
+
}
|
|
242
|
+
return element;
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* Convenience method to render a native component with the
|
|
246
|
+
* {@link itemElement} as its child component.
|
|
247
|
+
*/
|
|
248
|
+
renderStateItem = () => {
|
|
249
|
+
return (react_1.default.createElement(CarouselNativeItemHostComponent_1.CarouselNativeItemHost, { testID: this.props.testID, parentId: this.state.parentId, dataIndex: this.state.dataIndex, showSelectionBorder: this.props.showSelectionBorder ?? false, selectionBorderColor: this.props.showSelectionBorder
|
|
250
|
+
? (0, react_native_1.processColor)(this.props.selectionBorderStyle?.borderColor ??
|
|
251
|
+
defaultBorderStyle.borderColor)
|
|
252
|
+
: 0, selectionBorderWidth: this.props.selectionBorderStyle?.borderWidth ??
|
|
253
|
+
defaultBorderStyle.borderWidth, selectionBorderRadius: this.props.selectionBorderStyle?.borderRadius ??
|
|
254
|
+
defaultBorderStyle.borderRadius, selectionBorderStrokeColor: this.props.showSelectionBorder
|
|
255
|
+
? (0, react_native_1.processColor)(this.props.selectionBorderStyle?.borderStrokeColor ??
|
|
256
|
+
defaultBorderStyle.borderStrokeColor)
|
|
257
|
+
: 0, selectionBorderStrokeWidth: this.props.selectionBorderStyle?.borderStrokeWidth ??
|
|
258
|
+
defaultBorderStyle.borderStrokeWidth, selectionBorderStrokeRadius: this.props.selectionBorderStyle?.borderStrokeRadius ??
|
|
259
|
+
defaultBorderStyle.borderStrokeRadius, selectionBorderStrategy: this.props.selectionBorderStrategy
|
|
260
|
+
? (0, CarouselScrollableTypes_1.selectionBorderStrategyToBorderStrategy)(this.props.selectionBorderStrategy)
|
|
261
|
+
: 0, pressedItemScaleFactor: this.props.pressedItemScaleFactor, focusedItemScaleFactor: this.props.focusedItemScaleFactor, shiftItemsWidthScaleFactor: this.state.itemShift
|
|
262
|
+
? this.state.itemShift.width
|
|
263
|
+
: this.props.shiftItemsScaleFactor.width, shiftItemsHeightScaleFactor: this.state.itemShift
|
|
264
|
+
? this.state.itemShift.height
|
|
265
|
+
: this.props.shiftItemsScaleFactor.height, onBind: this.onBind }, this.itemElement));
|
|
266
|
+
};
|
|
267
|
+
////////////////////////////////////////
|
|
268
|
+
/// Private attributes
|
|
269
|
+
/**
|
|
270
|
+
* This property is used to cache the resolved element of this item.
|
|
271
|
+
*
|
|
272
|
+
* @private
|
|
273
|
+
*/
|
|
274
|
+
itemElement = undefined;
|
|
275
|
+
}
|
|
276
|
+
exports.CarouselNativeItemWrapper = CarouselNativeItemWrapper;
|