DishList.jsx 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React, { useState } from 'react';
  2. import { List, Popconfirm, Select } from 'antd';
  3. const { Option } = Select;
  4. const Header = props => {
  5. const { onChange } = props;
  6. const [data, setData] = useState([]);
  7. const [value, setValue] = useState();
  8. const handleSearch = (newValue) => {
  9. if (newValue) {
  10. // fetch(newValue, setData);
  11. } else {
  12. setData([]);
  13. }
  14. };
  15. const handleChange = (newValue) => {
  16. setValue(newValue);
  17. if (newValue) {
  18. onChange(data.filter(x => x.id === newValue)[0]);
  19. }
  20. };
  21. const options = data.map(d => <Option key={d.id} value={d.id}>{d.name}</Option>);
  22. return (
  23. <Select
  24. style={{ width: '100%' }}
  25. showSearch
  26. value={value}
  27. placeholder='请选择菜肴'
  28. defaultActiveFirstOption={false}
  29. showArrow={false}
  30. filterOption={false}
  31. onSearch={handleSearch}
  32. onChange={handleChange}
  33. notFoundContent={null}
  34. >
  35. {options}
  36. </Select>
  37. );
  38. }
  39. export default (props) => {
  40. const [list, setList] = useState([]);
  41. const onAdd = item => {
  42. const origin = list.filter(x => x.id !== item.id)[0];
  43. if (!origin) {
  44. setList([...list, item]);
  45. }
  46. }
  47. return (
  48. <List
  49. header={<Header onChange={onAdd} />}
  50. bordered
  51. dataSource={list}
  52. renderItem={item => (
  53. <List.Item
  54. className={classNames({ active: current.id === item.id })}
  55. onClick={() => setCurrent(item)}
  56. actions={[
  57. <Popconfirm
  58. key="delete"
  59. title="确认删除套餐?"
  60. onConfirm={() => onDelete(item)}
  61. >
  62. <a href="#">删除</a>
  63. </Popconfirm>
  64. ]}
  65. >
  66. {item.name}
  67. </List.Item>
  68. )}
  69. />
  70. );
  71. }