@@ -927,8 +927,7 @@ function AsyncLoadingExample(props) {
927
927
let url = new URL ( 'https://www.reddit.com/r/upliftingnews.json' ) ;
928
928
if ( cursor ) {
929
929
url . searchParams . append ( 'after' , cursor ) ;
930
- }
931
-
930
+ }
932
931
let res = await fetch ( url . toString ( ) , { signal} ) ;
933
932
let json = await res . json ( ) ;
934
933
return { items : json . data . children , cursor : json . data . after } ;
@@ -983,6 +982,153 @@ export const AsyncLoading: TableStory = {
983
982
name : 'async loading'
984
983
} ;
985
984
985
+ async function fakeFetch ( ) {
986
+ return new Promise (
987
+ ( resolve ) => {
988
+ setTimeout ( ( ) => resolve ( { json : async function ( ) {
989
+ return { data : { children : [
990
+ { data : { id : 'foo' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
991
+ { data : { id : 'fooo' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
992
+ { data : { id : 'foooo' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
993
+ { data : { id : 'fooooo' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
994
+ { data : { id : 'foooooo' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
995
+ { data : { id : 'doo' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
996
+ { data : { id : '1' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
997
+ { data : { id : '2' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
998
+ { data : { id : '3' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
999
+ { data : { id : '4' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
1000
+ { data : { id : '5' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } } ,
1001
+ { data : { id : '6' , thumbnail : 'https://i.imgur.com/Z7AzH2c.jpg' , ups : '7' , title : 'cats' , created : Date . now ( ) } }
1002
+ ] } } ;
1003
+ } } ) , 1000 ) ;
1004
+ }
1005
+ ) ;
1006
+ }
1007
+
1008
+ export const AsyncLoadingQuarryTest : TableStory = {
1009
+ args : {
1010
+ 'aria-label' : 'Top news from Reddit' ,
1011
+ selectionMode : 'multiple' ,
1012
+ height : 400 ,
1013
+ width : '100%'
1014
+ } ,
1015
+ render : ( args ) => < AsyncLoadingExampleQuarryTest { ...args } /> ,
1016
+ name : 'async reload on sort'
1017
+ } ;
1018
+
1019
+ function AsyncLoadingExampleQuarryTest ( props ) {
1020
+ let [ filters , setFilters ] = React . useState ( { } ) ;
1021
+
1022
+ const rColumns = [
1023
+ {
1024
+ key : 'title' ,
1025
+ label : 'Title'
1026
+ } ,
1027
+ {
1028
+ key : 'ups' ,
1029
+ label : 'Upvotes' ,
1030
+ width : 200
1031
+ } ,
1032
+ {
1033
+ key : 'created' ,
1034
+ label : 'Created' ,
1035
+ width : 350
1036
+ }
1037
+ ] ;
1038
+ interface Post {
1039
+ id : string ,
1040
+ key : string ,
1041
+ preview ?: string ,
1042
+ ups : number ,
1043
+ title : string ,
1044
+ created : string ,
1045
+ url : string
1046
+ }
1047
+
1048
+ let list = useAsyncList < Post > ( {
1049
+ getKey : ( item ) => item . id ,
1050
+ async load ( { cursor} ) {
1051
+ const url = new URL ( 'https://www.reddit.com/r/aww.json' ) ;
1052
+
1053
+ if ( cursor ) {
1054
+ url . searchParams . append ( 'after' , cursor ) ;
1055
+ }
1056
+
1057
+ const res = await fakeFetch ( ) ;
1058
+ // @ts -ignore
1059
+ const json = await res . json ( ) ;
1060
+ const items = json . data . children . map ( ( item ) => {
1061
+ return {
1062
+ id : item . data . id ,
1063
+ preview : item . data . thumbnail ,
1064
+ ups : item . data . ups ,
1065
+ title : item . data . title ,
1066
+ created : new Date ( item . data . created * 1000 ) . toISOString ( )
1067
+ } ;
1068
+ } ) ;
1069
+ return {
1070
+ items,
1071
+ cursor : json . data . after ,
1072
+ total : 987
1073
+ } ;
1074
+ } ,
1075
+ async sort ( { items, sortDescriptor} ) {
1076
+ return {
1077
+ items : items . slice ( ) . sort ( ( a , b ) => {
1078
+ let cmp = a [ sortDescriptor . column ] < b [ sortDescriptor . column ] ? - 1 : 1 ;
1079
+
1080
+ if ( sortDescriptor . direction === 'descending' ) {
1081
+ cmp *= - 1 ;
1082
+ }
1083
+
1084
+ return cmp ;
1085
+ } )
1086
+
1087
+ } ;
1088
+ }
1089
+ } ) ;
1090
+
1091
+ let reloadDeps = [ filters ] ;
1092
+
1093
+ useMountEffect ( ( ) : void => {
1094
+ list . reload ( ) ;
1095
+ } , reloadDeps ) ;
1096
+
1097
+ return (
1098
+ < TableView { ...props } width = "90vw" sortDescriptor = { list . sortDescriptor } selectedKeys = { list . selectedKeys } onSelectionChange = { list . setSelectedKeys } onSortChange = { setFilters } >
1099
+ < TableHeader columns = { rColumns } >
1100
+ { ( column ) => (
1101
+ < Column key = { column . key } allowsSorting >
1102
+ { column . label }
1103
+ </ Column >
1104
+ ) }
1105
+ </ TableHeader >
1106
+ < TableBody items = { list . items } loadingState = { list . loadingState } onLoadMore = { list . loadMore } >
1107
+ { item =>
1108
+ ( < Row key = { item . id } >
1109
+ { key =>
1110
+ key === 'title'
1111
+ ? < Cell textValue = { item . title } > < Link isQuiet > < a href = { item . url } target = "_blank" > { item . title } </ a > </ Link > </ Cell >
1112
+ : < Cell > { item [ key ] } </ Cell >
1113
+ }
1114
+ </ Row > )
1115
+ }
1116
+ </ TableBody >
1117
+ </ TableView >
1118
+ ) ;
1119
+ }
1120
+
1121
+ function useMountEffect ( fn : ( ) => void , deps : Array < unknown > ) : void {
1122
+ const mounted = React . useRef ( false ) ;
1123
+ React . useEffect ( ( ) => {
1124
+ if ( mounted . current ) {
1125
+ fn ( ) ;
1126
+ } else {
1127
+ mounted . current = true ;
1128
+ }
1129
+ } , deps ) ;
1130
+ }
1131
+
986
1132
export const HideHeader : TableStory = {
987
1133
args : {
988
1134
'aria-label' : 'TableView with static contents' ,
0 commit comments