|
@@ -2,15 +2,103 @@ import { Input, Checkbox } from 'antd';
|
|
|
import './goods.scss';
|
|
|
import store from '../../store';
|
|
|
import { getGoodsList } from './goodsSlice';
|
|
|
+import { useSelector } from 'react-redux';
|
|
|
+import { useState } from 'react';
|
|
|
|
|
|
function GoodsList() {
|
|
|
+ const list = useSelector(({ goods }) => goods.list);
|
|
|
+
|
|
|
+ // 双向绑定
|
|
|
+ let [searchName, setSearchName] = useState('');
|
|
|
+ let [showStocked, setShowStocked] = useState(false);
|
|
|
+
|
|
|
+ let cate = ''; // 记录上一次的分类名称
|
|
|
+
|
|
|
+ function renderList() {
|
|
|
+ let res = [];
|
|
|
+
|
|
|
+ // 检索包含searchName的所有商品
|
|
|
+ let renderList = list.filter((p) =>
|
|
|
+ p.name.toLowerCase().includes(searchName.toLowerCase())
|
|
|
+ );
|
|
|
+ // 如果复选框选中,将没有库存商品过滤掉
|
|
|
+ if (showStocked) {
|
|
|
+ renderList = renderList.filter((p) => p.amount);
|
|
|
+ }
|
|
|
+
|
|
|
+ renderList.forEach((l) => {
|
|
|
+ // // 如果当前商品的分类与上一次一直 就不需要渲染分类行
|
|
|
+ // if (l.category == cate) {
|
|
|
+ // res.push(
|
|
|
+ // <tr>
|
|
|
+ // <td>{l.name}</td>
|
|
|
+ // <td>{l.price}</td>
|
|
|
+ // </tr>
|
|
|
+ // );
|
|
|
+ // } else {
|
|
|
+ // // 这时就需要渲染分类行
|
|
|
+ // res.push(
|
|
|
+ // <tr>
|
|
|
+ // <td colSpan={2}>{l.category}</td>
|
|
|
+ // </tr>
|
|
|
+ // );
|
|
|
+ // res.push(
|
|
|
+ // <tr>
|
|
|
+ // <td>{l.name}</td>
|
|
|
+ // <td>{l.price}</td>
|
|
|
+ // </tr>
|
|
|
+ // );
|
|
|
+ // }
|
|
|
+ if (l.category != cate) {
|
|
|
+ // 如果当前商品分类 与上一次 不一样 就需要渲染分类行
|
|
|
+ res.push(
|
|
|
+ <tr className="cate">
|
|
|
+ <td colSpan={2}>{l.category}</td>
|
|
|
+ </tr>
|
|
|
+ );
|
|
|
+
|
|
|
+ // 记录当前分类标题
|
|
|
+ cate = l.category;
|
|
|
+ }
|
|
|
+ res.push(
|
|
|
+ <tr>
|
|
|
+ <td className={l.amount ? '' : 'unstocked'}>{l.name}</td>
|
|
|
+ <td>{`$${l.price}`}</td>
|
|
|
+ </tr>
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
return (
|
|
|
<div className="goods-list">
|
|
|
<div className="search-bar">
|
|
|
- <Input placeholder="通过商品名检索..." />
|
|
|
- <Checkbox>仅显示有库存的商品</Checkbox>
|
|
|
+ <Input
|
|
|
+ placeholder="通过商品名检索..."
|
|
|
+ value={searchName}
|
|
|
+ onChange={(e) => {
|
|
|
+ setSearchName(e.target.value);
|
|
|
+ }}
|
|
|
+ />
|
|
|
+ <Checkbox
|
|
|
+ checked={showStocked}
|
|
|
+ onChange={(e) => {
|
|
|
+ setShowStocked(e.target.checked);
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ 仅显示有库存的商品
|
|
|
+ </Checkbox>
|
|
|
</div>
|
|
|
- <table></table>
|
|
|
+ <table>
|
|
|
+ <thead>
|
|
|
+ <tr>
|
|
|
+ <th>商品名称</th>
|
|
|
+ <th>商品价格</th>
|
|
|
+ </tr>
|
|
|
+ </thead>
|
|
|
+ <tbody>{renderList()}</tbody>
|
|
|
+ </table>
|
|
|
</div>
|
|
|
);
|
|
|
}
|