@onekeyfe/react-native-native-list 3.0.105
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/README.md +342 -0
- package/android/CMakeLists.txt +20 -0
- package/android/build.gradle +105 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/onekey/nativelist/HybridNativeList.kt +125 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListAdapter.kt +165 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListModels.kt +147 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListPackage.kt +25 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListRowView.kt +2939 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListView.kt +1794 -0
- package/common/fonts/Roobert-Bold.ttf +0 -0
- package/common/fonts/Roobert-Medium.ttf +0 -0
- package/common/fonts/Roobert-Regular.ttf +0 -0
- package/common/fonts/Roobert-SemiBold.ttf +0 -0
- package/docs/DESIGN.md +129 -0
- package/ios/HybridNativeList.swift +131 -0
- package/ios/NativeListCell.swift +2858 -0
- package/ios/NativeListDesignAssets.swift +110 -0
- package/ios/NativeListModels.swift +215 -0
- package/ios/RNCNativeListView.swift +1737 -0
- package/ios/Resources/NativeListIcons.xcassets/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_arrow_bottom.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_arrow_bottom.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_arrow_top.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_arrow_top.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chart_trending_up.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chart_trending_up.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_checkbox_checked.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_checkbox_checked.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_checkbox_indeterminate.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_checkbox_indeterminate.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_bottom.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_bottom.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_grabber_ver.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_grabber_ver.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_right_small.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_right_small.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_top.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_chevron_top.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_dot_hor.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_dot_hor.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_drag.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_drag.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_error_solid.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_error_solid.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_image_square_waves.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_image_square_waves.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_info_circle.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_info_circle.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_minus_circle.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_minus_circle.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_minus_circle_solid.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_minus_circle_solid.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_pencil.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_pencil.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_plus_circle.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_plus_circle.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_plus_small.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_plus_small.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_questionmark_solid.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_questionmark_solid.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_shield_exclamation.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_shield_exclamation.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_speaker_promote.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_speaker_promote.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_star.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_star.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_star_solid.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_star_solid.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_swap_hor.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_swap_hor.imageset/icon.svg +1 -0
- package/lib/module/NativeList.js +183 -0
- package/lib/module/NativeList.nitro.js +4 -0
- package/lib/module/NativeList.types.js +4 -0
- package/lib/module/NativeList.web.js +167 -0
- package/lib/module/index.js +6 -0
- package/lib/module/models.js +2 -0
- package/lib/module/package.json +1 -0
- package/lib/module/scrolling.js +121 -0
- package/lib/module/selection.js +85 -0
- package/lib/module/validation.js +562 -0
- package/lib/module/web/NativeListWebEngine.js +2238 -0
- package/lib/nitrogen/generated/.gitattributes +1 -0
- package/lib/nitrogen/generated/android/c++/JFunc_void_std__string.hpp +76 -0
- package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +201 -0
- package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +84 -0
- package/lib/nitrogen/generated/android/c++/JNativeListScrollAlignment.hpp +64 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +104 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.hpp +54 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/Func_void_std__string.kt +78 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +177 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/NativeListScrollAlignment.kt +25 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/nativelistOnLoad.kt +35 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/views/HybridNativeListManager.kt +96 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/views/HybridNativeListStateUpdater.kt +23 -0
- package/lib/nitrogen/generated/android/nativelist+autolinking.cmake +83 -0
- package/lib/nitrogen/generated/android/nativelist+autolinking.gradle +27 -0
- package/lib/nitrogen/generated/android/nativelistOnLoad.cpp +58 -0
- package/lib/nitrogen/generated/android/nativelistOnLoad.hpp +34 -0
- package/lib/nitrogen/generated/ios/NativeListModule+autolinking.rb +62 -0
- package/lib/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.cpp +41 -0
- package/lib/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.hpp +91 -0
- package/lib/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Umbrella.hpp +49 -0
- package/lib/nitrogen/generated/ios/NativeListModuleAutolinking.mm +33 -0
- package/lib/nitrogen/generated/ios/NativeListModuleAutolinking.swift +26 -0
- package/lib/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.cpp +11 -0
- package/lib/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +180 -0
- package/lib/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +180 -0
- package/lib/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +69 -0
- package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +447 -0
- package/lib/nitrogen/generated/ios/swift/NativeListScrollAlignment.swift +48 -0
- package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +43 -0
- package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +87 -0
- package/lib/nitrogen/generated/shared/c++/NativeListScrollAlignment.hpp +84 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +46 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +103 -0
- package/lib/nitrogen/generated/shared/json/NativeListConfig.json +16 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeList.d.ts +18 -0
- package/lib/typescript/src/NativeList.nitro.d.ts +29 -0
- package/lib/typescript/src/NativeList.types.d.ts +87 -0
- package/lib/typescript/src/NativeList.web.d.ts +19 -0
- package/lib/typescript/src/index.d.ts +7 -0
- package/lib/typescript/src/models.d.ts +456 -0
- package/lib/typescript/src/scrolling.d.ts +32 -0
- package/lib/typescript/src/selection.d.ts +27 -0
- package/lib/typescript/src/validation.d.ts +7 -0
- package/lib/typescript/src/web/NativeListWebEngine.d.ts +203 -0
- package/nitro.json +23 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/c++/JFunc_void_std__string.hpp +76 -0
- package/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +201 -0
- package/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +84 -0
- package/nitrogen/generated/android/c++/JNativeListScrollAlignment.hpp +64 -0
- package/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +104 -0
- package/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.hpp +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/Func_void_std__string.kt +78 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +177 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/NativeListScrollAlignment.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/nativelistOnLoad.kt +35 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/views/HybridNativeListManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/views/HybridNativeListStateUpdater.kt +23 -0
- package/nitrogen/generated/android/nativelist+autolinking.cmake +83 -0
- package/nitrogen/generated/android/nativelist+autolinking.gradle +27 -0
- package/nitrogen/generated/android/nativelistOnLoad.cpp +58 -0
- package/nitrogen/generated/android/nativelistOnLoad.hpp +34 -0
- package/nitrogen/generated/ios/NativeListModule+autolinking.rb +62 -0
- package/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.cpp +41 -0
- package/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Bridge.hpp +91 -0
- package/nitrogen/generated/ios/NativeListModule-Swift-Cxx-Umbrella.hpp +49 -0
- package/nitrogen/generated/ios/NativeListModuleAutolinking.mm +33 -0
- package/nitrogen/generated/ios/NativeListModuleAutolinking.swift +26 -0
- package/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +180 -0
- package/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +180 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +69 -0
- package/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +447 -0
- package/nitrogen/generated/ios/swift/NativeListScrollAlignment.swift +48 -0
- package/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +43 -0
- package/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +87 -0
- package/nitrogen/generated/shared/c++/NativeListScrollAlignment.hpp +84 -0
- package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +46 -0
- package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +103 -0
- package/nitrogen/generated/shared/json/NativeListConfig.json +16 -0
- package/package.json +126 -0
- package/react-native-native-list.podspec +38 -0
- package/react-native.config.js +19 -0
- package/src/NativeList.nitro.ts +48 -0
- package/src/NativeList.tsx +298 -0
- package/src/NativeList.types.ts +125 -0
- package/src/NativeList.web.tsx +258 -0
- package/src/index.ts +35 -0
- package/src/models.ts +604 -0
- package/src/scrolling.ts +189 -0
- package/src/selection.ts +159 -0
- package/src/validation.ts +877 -0
- package/src/web/NativeListWebEngine.ts +3767 -0
package/src/scrolling.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ScrollAlignment,
|
|
3
|
+
ScrollPositionOptions,
|
|
4
|
+
ScrollToIndexFailedInfo,
|
|
5
|
+
ScrollToIndexParams,
|
|
6
|
+
ScrollToKeyParams,
|
|
7
|
+
ScrollToLocationParams,
|
|
8
|
+
} from './NativeList.types';
|
|
9
|
+
import type { RowModel } from './models';
|
|
10
|
+
|
|
11
|
+
export type NormalizedPositionScroll = Readonly<{
|
|
12
|
+
animated: boolean;
|
|
13
|
+
alignment: ScrollAlignment;
|
|
14
|
+
viewPosition: number;
|
|
15
|
+
viewOffset: number;
|
|
16
|
+
}>;
|
|
17
|
+
|
|
18
|
+
export type AlignedOffsetInput = Readonly<{
|
|
19
|
+
itemOffset: number;
|
|
20
|
+
itemLength: number;
|
|
21
|
+
viewportLength: number;
|
|
22
|
+
contentLength: number;
|
|
23
|
+
currentOffset: number;
|
|
24
|
+
alignment: ScrollAlignment;
|
|
25
|
+
viewPosition: number;
|
|
26
|
+
viewOffset: number;
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
function assertNonNegativeInteger(value: number, name: string): void {
|
|
30
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
31
|
+
throw new Error(`NativeList ${name} must be a non-negative integer`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function assertFiniteNumber(value: number, name: string): void {
|
|
36
|
+
if (!Number.isFinite(value)) {
|
|
37
|
+
throw new Error(`NativeList ${name} must be a finite number`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function positionForAlignment(alignment: ScrollAlignment): number {
|
|
42
|
+
switch (alignment) {
|
|
43
|
+
case 'center':
|
|
44
|
+
return 0.5;
|
|
45
|
+
case 'end':
|
|
46
|
+
return 1;
|
|
47
|
+
default:
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function normalizePositionScroll(
|
|
53
|
+
options: ScrollPositionOptions,
|
|
54
|
+
defaultAlignment: ScrollAlignment
|
|
55
|
+
): NormalizedPositionScroll {
|
|
56
|
+
const alignment = options.alignment ?? defaultAlignment;
|
|
57
|
+
if (options.alignment !== undefined && options.viewPosition !== undefined) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
'NativeList alignment and viewPosition cannot be used together'
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
const viewPosition = options.viewPosition ?? positionForAlignment(alignment);
|
|
63
|
+
if (!Number.isFinite(viewPosition) || viewPosition < 0 || viewPosition > 1) {
|
|
64
|
+
throw new Error('NativeList viewPosition must be between 0 and 1');
|
|
65
|
+
}
|
|
66
|
+
const viewOffset = options.viewOffset ?? 0;
|
|
67
|
+
assertFiniteNumber(viewOffset, 'viewOffset');
|
|
68
|
+
return {
|
|
69
|
+
animated: options.animated ?? true,
|
|
70
|
+
alignment:
|
|
71
|
+
options.viewPosition === undefined ? alignment : ('start' as const),
|
|
72
|
+
viewPosition,
|
|
73
|
+
viewOffset,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function normalizeIndexScroll(
|
|
78
|
+
paramsOrIndex: ScrollToIndexParams | number,
|
|
79
|
+
animated?: boolean,
|
|
80
|
+
alignment?: ScrollAlignment
|
|
81
|
+
): Readonly<{ index: number; scroll: NormalizedPositionScroll }> {
|
|
82
|
+
if (typeof paramsOrIndex === 'number') {
|
|
83
|
+
assertNonNegativeInteger(paramsOrIndex, 'scrollToIndex index');
|
|
84
|
+
return {
|
|
85
|
+
index: paramsOrIndex,
|
|
86
|
+
scroll: normalizePositionScroll({ animated, alignment }, 'nearest'),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
assertNonNegativeInteger(paramsOrIndex.index, 'scrollToIndex index');
|
|
90
|
+
return {
|
|
91
|
+
index: paramsOrIndex.index,
|
|
92
|
+
scroll: normalizePositionScroll(paramsOrIndex, 'start'),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function normalizeKeyScroll(
|
|
97
|
+
paramsOrKey: ScrollToKeyParams | string,
|
|
98
|
+
animated?: boolean,
|
|
99
|
+
alignment?: ScrollAlignment
|
|
100
|
+
): Readonly<{ key: string; scroll: NormalizedPositionScroll }> {
|
|
101
|
+
if (typeof paramsOrKey === 'string') {
|
|
102
|
+
return {
|
|
103
|
+
key: paramsOrKey,
|
|
104
|
+
scroll: normalizePositionScroll({ animated, alignment }, 'nearest'),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
key: paramsOrKey.key,
|
|
109
|
+
scroll: normalizePositionScroll(paramsOrKey, 'start'),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function validateOffset(offset: number): void {
|
|
114
|
+
assertFiniteNumber(offset, 'scroll offset');
|
|
115
|
+
if (offset < 0) {
|
|
116
|
+
throw new Error('NativeList scroll offset must be non-negative');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function resolveLocationIndex(
|
|
121
|
+
rows: readonly RowModel[],
|
|
122
|
+
params: ScrollToLocationParams
|
|
123
|
+
): number | undefined {
|
|
124
|
+
assertNonNegativeInteger(
|
|
125
|
+
params.sectionIndex,
|
|
126
|
+
'scrollToLocation sectionIndex'
|
|
127
|
+
);
|
|
128
|
+
assertNonNegativeInteger(params.itemIndex, 'scrollToLocation itemIndex');
|
|
129
|
+
const headers = rows
|
|
130
|
+
.map((row, index) => ({ row, index }))
|
|
131
|
+
.filter(
|
|
132
|
+
(entry) =>
|
|
133
|
+
entry.row.type === 'sectionHeader' && entry.row.variant !== 'summary'
|
|
134
|
+
);
|
|
135
|
+
const section = headers[params.sectionIndex];
|
|
136
|
+
if (!section || section.row.type !== 'sectionHeader') return undefined;
|
|
137
|
+
const end = headers[params.sectionIndex + 1]?.index ?? rows.length;
|
|
138
|
+
const items = rows
|
|
139
|
+
.slice(section.index + 1, end)
|
|
140
|
+
.map((row, offset) => ({ row, index: section.index + 1 + offset }))
|
|
141
|
+
.filter(
|
|
142
|
+
(entry) =>
|
|
143
|
+
entry.row.type !== 'sectionHeader' &&
|
|
144
|
+
entry.row.sectionKey === section.row.sectionKey
|
|
145
|
+
);
|
|
146
|
+
return items[params.itemIndex]?.index;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function scrollFailure(
|
|
150
|
+
rows: readonly RowModel[],
|
|
151
|
+
index: number,
|
|
152
|
+
reason: ScrollToIndexFailedInfo['reason'],
|
|
153
|
+
averageItemLength = 0
|
|
154
|
+
): ScrollToIndexFailedInfo {
|
|
155
|
+
return {
|
|
156
|
+
index,
|
|
157
|
+
itemCount: rows.length,
|
|
158
|
+
highestMeasuredFrameIndex: rows.length - 1,
|
|
159
|
+
averageItemLength,
|
|
160
|
+
reason,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function calculateAlignedScrollOffset({
|
|
165
|
+
itemOffset,
|
|
166
|
+
itemLength,
|
|
167
|
+
viewportLength,
|
|
168
|
+
contentLength,
|
|
169
|
+
currentOffset,
|
|
170
|
+
alignment,
|
|
171
|
+
viewPosition,
|
|
172
|
+
viewOffset,
|
|
173
|
+
}: AlignedOffsetInput): number {
|
|
174
|
+
let resolvedPosition = viewPosition;
|
|
175
|
+
if (alignment === 'nearest') {
|
|
176
|
+
if (itemOffset < currentOffset) resolvedPosition = 0;
|
|
177
|
+
else if (itemOffset + itemLength > currentOffset + viewportLength)
|
|
178
|
+
resolvedPosition = 1;
|
|
179
|
+
else return currentOffset;
|
|
180
|
+
}
|
|
181
|
+
const target =
|
|
182
|
+
itemOffset -
|
|
183
|
+
resolvedPosition * Math.max(0, viewportLength - itemLength) -
|
|
184
|
+
viewOffset;
|
|
185
|
+
return Math.min(
|
|
186
|
+
Math.max(0, target),
|
|
187
|
+
Math.max(0, contentLength - viewportLength)
|
|
188
|
+
);
|
|
189
|
+
}
|
package/src/selection.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CheckboxState,
|
|
3
|
+
NativeListSnapshot,
|
|
4
|
+
RowModel,
|
|
5
|
+
SelectionDeltaEvent,
|
|
6
|
+
SelectionMode,
|
|
7
|
+
} from './models';
|
|
8
|
+
|
|
9
|
+
export type SelectionState = Readonly<{
|
|
10
|
+
mode: SelectionMode;
|
|
11
|
+
selectedKeys: ReadonlySet<string>;
|
|
12
|
+
}>;
|
|
13
|
+
|
|
14
|
+
export type SelectionAction =
|
|
15
|
+
| Readonly<{ scope: 'row'; key: string }>
|
|
16
|
+
| Readonly<{ scope: 'section'; sectionKey: string }>
|
|
17
|
+
| Readonly<{ scope: 'list' }>
|
|
18
|
+
| Readonly<{ scope: 'replace'; selectedKeys: readonly string[] }>;
|
|
19
|
+
|
|
20
|
+
export type SelectionResult = Readonly<{
|
|
21
|
+
state: SelectionState;
|
|
22
|
+
delta: SelectionDeltaEvent;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
export function isSelectableRow(row: RowModel): boolean {
|
|
26
|
+
return (
|
|
27
|
+
!row.disabled &&
|
|
28
|
+
(row.type === 'identity' ||
|
|
29
|
+
row.type === 'rail' ||
|
|
30
|
+
row.type === 'activity' ||
|
|
31
|
+
row.type === 'message' ||
|
|
32
|
+
row.type === 'dataRow' ||
|
|
33
|
+
row.type === 'mediaTile' ||
|
|
34
|
+
row.type === 'metricCard')
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function deltaBetween(
|
|
39
|
+
before: ReadonlySet<string>,
|
|
40
|
+
after: ReadonlySet<string>,
|
|
41
|
+
source: SelectionDeltaEvent['source'],
|
|
42
|
+
sourceKey?: string
|
|
43
|
+
): SelectionDeltaEvent {
|
|
44
|
+
return {
|
|
45
|
+
addedKeys: [...after].filter((key) => !before.has(key)),
|
|
46
|
+
removedKeys: [...before].filter((key) => !after.has(key)),
|
|
47
|
+
source,
|
|
48
|
+
sourceKey,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function targetKeys(
|
|
53
|
+
action: SelectionAction,
|
|
54
|
+
rows: readonly RowModel[]
|
|
55
|
+
): string[] {
|
|
56
|
+
switch (action.scope) {
|
|
57
|
+
case 'row':
|
|
58
|
+
return rows.some((row) => row.key === action.key && isSelectableRow(row))
|
|
59
|
+
? [action.key]
|
|
60
|
+
: [];
|
|
61
|
+
case 'section':
|
|
62
|
+
return rows
|
|
63
|
+
.filter(
|
|
64
|
+
(row) => row.sectionKey === action.sectionKey && isSelectableRow(row)
|
|
65
|
+
)
|
|
66
|
+
.map((row) => row.key);
|
|
67
|
+
case 'list':
|
|
68
|
+
return rows.filter(isSelectableRow).map((row) => row.key);
|
|
69
|
+
case 'replace':
|
|
70
|
+
return action.selectedKeys.filter((key) =>
|
|
71
|
+
rows.some((row) => row.key === key && isSelectableRow(row))
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function reduceSelection(
|
|
77
|
+
state: SelectionState,
|
|
78
|
+
action: SelectionAction,
|
|
79
|
+
rows: readonly RowModel[]
|
|
80
|
+
): SelectionResult {
|
|
81
|
+
const before = new Set(state.selectedKeys);
|
|
82
|
+
if (state.mode === 'none') {
|
|
83
|
+
return { state, delta: { addedKeys: [], removedKeys: [], source: 'list' } };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const targets = targetKeys(action, rows);
|
|
87
|
+
const source = action.scope === 'replace' ? 'list' : action.scope;
|
|
88
|
+
const sourceKey =
|
|
89
|
+
action.scope === 'row'
|
|
90
|
+
? action.key
|
|
91
|
+
: action.scope === 'section'
|
|
92
|
+
? action.sectionKey
|
|
93
|
+
: undefined;
|
|
94
|
+
if (targets.length === 0 && action.scope !== 'replace') {
|
|
95
|
+
return {
|
|
96
|
+
state,
|
|
97
|
+
delta: deltaBetween(before, before, source, sourceKey),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const after = new Set(before);
|
|
102
|
+
if (action.scope === 'replace') {
|
|
103
|
+
after.clear();
|
|
104
|
+
const replacement = state.mode === 'single' ? targets.slice(0, 1) : targets;
|
|
105
|
+
replacement.forEach((key) => after.add(key));
|
|
106
|
+
} else if (state.mode === 'single') {
|
|
107
|
+
const key = targets[0];
|
|
108
|
+
after.clear();
|
|
109
|
+
if (key && !before.has(key)) after.add(key);
|
|
110
|
+
} else {
|
|
111
|
+
const allSelected =
|
|
112
|
+
targets.length > 0 && targets.every((key) => before.has(key));
|
|
113
|
+
targets.forEach((key) =>
|
|
114
|
+
allSelected ? after.delete(key) : after.add(key)
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const nextState = { mode: state.mode, selectedKeys: after };
|
|
119
|
+
return {
|
|
120
|
+
state: nextState,
|
|
121
|
+
delta: deltaBetween(before, after, source, sourceKey),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function checkboxStateForKeys(
|
|
126
|
+
keys: readonly string[],
|
|
127
|
+
selectedKeys: ReadonlySet<string>
|
|
128
|
+
): CheckboxState {
|
|
129
|
+
if (keys.length === 0) return 'unchecked';
|
|
130
|
+
const selectedCount = keys.reduce(
|
|
131
|
+
(count, key) => count + (selectedKeys.has(key) ? 1 : 0),
|
|
132
|
+
0
|
|
133
|
+
);
|
|
134
|
+
if (selectedCount === 0) return 'unchecked';
|
|
135
|
+
if (selectedCount === keys.length) return 'checked';
|
|
136
|
+
return 'indeterminate';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function checkboxStateForSection(
|
|
140
|
+
sectionKey: string,
|
|
141
|
+
rows: readonly RowModel[],
|
|
142
|
+
selectedKeys: ReadonlySet<string>
|
|
143
|
+
): CheckboxState {
|
|
144
|
+
return checkboxStateForKeys(
|
|
145
|
+
rows
|
|
146
|
+
.filter((row) => row.sectionKey === sectionKey && isSelectableRow(row))
|
|
147
|
+
.map((row) => row.key),
|
|
148
|
+
selectedKeys
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function selectionStateFromSnapshot(
|
|
153
|
+
snapshot: NativeListSnapshot
|
|
154
|
+
): SelectionState {
|
|
155
|
+
return {
|
|
156
|
+
mode: snapshot.selection?.mode ?? 'none',
|
|
157
|
+
selectedKeys: new Set(snapshot.selection?.selectedKeys ?? []),
|
|
158
|
+
};
|
|
159
|
+
}
|