RenderGoodsSpec.tsx 1.67 KB
import React, { useCallback, useState } from 'react';
import { Button, Modal, Popover, Table } from 'antd';

interface Props {
  specList: StanderList.AuthSpecList[]
}

export default function RenderGoodsSpec({ specList }: Props) {
  const [visible, setVisible] = useState(false);
  const items = (specList || []).map(i => `${i.standardSpecName}:${i.specDetails}`);
  const RenderSpecModal = useCallback(() => (visible ? (
    <SpecModal
      visible={visible}
      onCancel={() => setVisible(false)}
      items={specList}
    />
  ) : null), [visible]);
  return (
    <>
      <Popover
        placement="topLeft"
        content={<div style={{ maxWidth: 260 }}>{items.join(';')}</div>}
      >
        <div>
          {items.slice(0, 3).map((i: string) => {
            return (
              <p key={i} style={{ margin: 0, fontSize: 12 }}>{i}</p>
            );
          })}
          {items.length > 3 ? (<span>...</span>) : null}
        </div>
        <Button type="link" style={{ fontSize: 12, padding: 0 }} onClick={() => setVisible(true)}>查看</Button>
      </Popover>
      {RenderSpecModal()}
    </>
  );
}

function SpecModal({ items, onCancel, visible }: any) {
  const columns = [
    {
      title: '规格名称',
      dataIndex: 'standardSpecName',
    },
    {
      title: '规格内容',
      dataIndex: 'specDetails',
    },
    {
      title: '排序',
      dataIndex: 'sort',
    },
  ];

  return (
    <Modal title="规格属性" open={visible} onCancel={onCancel} footer={false}>
      <Table
        pagination={false}
        bordered
        rowKey="standardSpecName"
        dataSource={items}
        size="small"
        columns={columns}
      />
    </Modal>
  );
}