Skip to content

Commit

Permalink
Move CardsList to typescript (getredash#5136)
Browse files Browse the repository at this point in the history
* 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
max-voronov authored and andrewdever committed Oct 5, 2020
1 parent 9e8dbc1 commit 6bc975c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 84 deletions.
84 changes: 0 additions & 84 deletions client/app/components/cards-list/CardsList.jsx

This file was deleted.

80 changes: 80 additions & 0 deletions client/app/components/cards-list/CardsList.tsx
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,
};

0 comments on commit 6bc975c

Please sign in to comment.