forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CardsList to typescript (getredash#5136)
* Refactor CardsList - pass a suffix for list item Adding :id to an item to be used as a key suffix is redundant and the same can be accomplished by using :index from the map function. * Move CardsList to typescript * Convert CardsList component to functional component * CR1 * CR2
- Loading branch information
1 parent
9e8dbc1
commit 6bc975c
Showing
2 changed files
with
80 additions
and
84 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { includes, isEmpty } from "lodash"; | ||
import PropTypes from "prop-types"; | ||
import React, { useState } from "react"; | ||
import Input from "antd/lib/input"; | ||
import Link from "@/components/Link"; | ||
import EmptyState from "@/components/items-list/components/EmptyState"; | ||
|
||
import "./CardsList.less"; | ||
|
||
export interface CardsListItem { | ||
title: string; | ||
imgSrc: string; | ||
onClick?: () => void; | ||
href?: string; | ||
} | ||
|
||
export interface CardsListProps { | ||
items?: CardsListItem[]; | ||
showSearch?: boolean; | ||
} | ||
|
||
interface ListItemProps { | ||
item: CardsListItem; | ||
keySuffix: string; | ||
} | ||
|
||
function ListItem({ item, keySuffix }: ListItemProps) { | ||
return ( | ||
<Link key={`card${keySuffix}`} className="visual-card" onClick={item.onClick} href={item.href}> | ||
<img alt={item.title} src={item.imgSrc} /> | ||
<h3>{item.title}</h3> | ||
</Link> | ||
); | ||
} | ||
|
||
export default function CardsList({ items = [], showSearch = false }: CardsListProps) { | ||
const [searchText, setSearchText] = useState(""); | ||
const filteredItems = items.filter( | ||
item => isEmpty(searchText) || includes(item.title.toLowerCase(), searchText.toLowerCase()) | ||
); | ||
|
||
return ( | ||
<div data-test="CardsList"> | ||
{showSearch && ( | ||
<div className="row p-10"> | ||
<div className="col-md-4 col-md-offset-4"> | ||
<Input.Search | ||
placeholder="Search..." | ||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setSearchText(e.target.value)} | ||
autoFocus | ||
/> | ||
</div> | ||
</div> | ||
)} | ||
{isEmpty(filteredItems) ? ( | ||
<EmptyState className="" /> | ||
) : ( | ||
<div className="row"> | ||
<div className="col-lg-12 d-inline-flex flex-wrap visual-card-list"> | ||
{filteredItems.map((item: CardsListItem, index: number) => ( | ||
<ListItem key={index} item={item} keySuffix={index.toString()} /> | ||
))} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
CardsList.propTypes = { | ||
items: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
title: PropTypes.string.isRequired, | ||
imgSrc: PropTypes.string.isRequired, | ||
onClick: PropTypes.func, | ||
href: PropTypes.string, | ||
}) | ||
), | ||
showSearch: PropTypes.bool, | ||
}; |