forked from raystack/apsara
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: overload component height if provided in props
- Loading branch information
Ayushi Sharma
committed
Apr 24, 2024
1 parent
e297069
commit 5a2df37
Showing
5 changed files
with
151 additions
and
229 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
packages/apsara-ui/src/InfiniteScroll/InfiniteScroll.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
import InfiniteScroll from "./InfiniteScroll"; | ||
import styled from "styled-components"; | ||
|
||
export default { | ||
title: "Data Display/InfiniteScroll", | ||
component: InfiniteScroll, | ||
}; | ||
|
||
interface User { | ||
key: number; | ||
name: string; | ||
status: "active" | "inactive"; | ||
age: number; | ||
address: string; | ||
} | ||
|
||
function getData(page = 1, page_size = 10): User[] { | ||
return new Array(page * page_size).fill(0).map((_, index) => { | ||
return { | ||
key: index, | ||
name: `name ${index}`, | ||
status: index % 2 ? "active" : "inactive", | ||
age: index, | ||
address: `A${index} Downing Street`, | ||
}; | ||
}); | ||
} | ||
|
||
function getDataAsync(page = 1, page_size = 10): Promise<User[]> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(getData(page, page_size)); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
export const basic = () => { | ||
const [data, setData] = useState<User[]>([]); | ||
const [isFetching, setIsFetching] = useState<boolean>(false); | ||
|
||
const fetchData = useCallback(() => { | ||
setIsFetching(true); | ||
getDataAsync() | ||
.then((d) => setData((curr) => curr.concat(d))) | ||
.finally(() => setIsFetching(false)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
fetchData(); | ||
}, [fetchData]); | ||
|
||
const onScroll = () => { | ||
if (isFetching) return; | ||
|
||
fetchData(); | ||
}; | ||
|
||
return ( | ||
<div style={{ height: "80vh" }}> | ||
<InfiniteScroll<User> | ||
items={data} | ||
renderItem={(item) => <Card user={item} />} | ||
onBottomScroll={onScroll} | ||
isLoading={isFetching} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const Card = ({ user }: { user: User }) => { | ||
return ( | ||
<UserCard> | ||
<div className="title">{user.name}</div> | ||
<div className="body"> | ||
<div className="body-left"> | ||
<div className="description">Address</div> | ||
<div>{user.address}</div> | ||
</div> | ||
<div className="body-right"> | ||
<div className="description">Age</div> | ||
<div>{user.age}</div> | ||
</div> | ||
</div> | ||
</UserCard> | ||
); | ||
}; | ||
|
||
const UserCard = styled.div` | ||
margin-bottom: 16px; | ||
border-radius: 2px; | ||
box-shadow: rgb(179 179 179 / 31%) 0px 1px 5px; | ||
.title { | ||
margin: 10px; | ||
color: #4d85f4; | ||
font-size: 16px; | ||
} | ||
.description { | ||
margin-top: 20px; | ||
word-break: break-all; | ||
} | ||
.body { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
padding: 0px 20px 12px 20px; | ||
> .body-left { | ||
margin-top: 16px; | ||
flex: 1; | ||
} | ||
> .body-right { | ||
width: 240px; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-end; | ||
justify-content: space-between; | ||
> div { | ||
display: flex; | ||
align-items: flex-end; | ||
flex-direction: column; | ||
} | ||
.tag_content { | ||
text-transform: uppercase; | ||
} | ||
} | ||
} | ||
`; |
8 changes: 0 additions & 8 deletions
8
packages/apsara-ui/src/InfiniteScroll/InfiniteScroll.styles.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.