Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
753 B
23
Indexable
export default function ProductTable({ filterText, inStockOnly }) {
  const rows = products
    .filter(product => 
      product.name.toLowerCase().includes(filterText.toLowerCase()) &&
      (product.stocked || !inStockOnly)
    )
    .map((product, index, array) => [
      index === 0 || array[index - 1].category !== product.category ? (
        <ProductCategoryRow key={product.category} category={product.category} />
      ) : null,
      <ProductRow key={product.id} product={product} />
    ])
    .reduce((acc, val) => acc.concat(val), []);

  return (
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>{rows}</tbody>
    </table>
  );
}
Leave a Comment