custom-input-demo.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React, { FC, useState } from "react";
  2. import { DageDefaultColumnTypes, DageEditable } from "@dage/pc-components";
  3. import { Input, InputNumber } from "antd";
  4. interface Item {
  5. key: string | number;
  6. name: string;
  7. age: number;
  8. address: string;
  9. }
  10. export const DageEditableDemo: FC = () => {
  11. const [dataSource, setDataSource] = useState<Item[]>([
  12. {
  13. key: "0",
  14. name: "Edward King 0",
  15. age: 32,
  16. address: "London, Park Lane no. 0",
  17. },
  18. {
  19. key: "1",
  20. name: "Edward King 1",
  21. age: 22,
  22. address: "London, Park Lane no. 1",
  23. },
  24. ]);
  25. const defaultColumns: DageDefaultColumnTypes<Item>[] = [
  26. {
  27. title: "name",
  28. dataIndex: "name",
  29. width: "30%",
  30. editable: true,
  31. Input: <Input placeholder="请输入内容,最多5字" maxLength={5} />,
  32. },
  33. {
  34. title: "age",
  35. dataIndex: "age",
  36. editable: true,
  37. Input: <InputNumber placeholder="请输入" />,
  38. },
  39. {
  40. title: "address",
  41. dataIndex: "address",
  42. },
  43. ];
  44. const handleSave = (row: Item) => {
  45. const newData = [...dataSource];
  46. const index = newData.findIndex((item) => row.key === item.key);
  47. const item = newData[index];
  48. newData.splice(index, 1, {
  49. ...item,
  50. ...row,
  51. });
  52. setDataSource(newData);
  53. };
  54. return (
  55. <DageEditable
  56. bordered
  57. dataSource={dataSource}
  58. defaultColumns={defaultColumns}
  59. handleSave={handleSave}
  60. />
  61. );
  62. };