简体   繁体   English

如何在 Polaris Index Table 单元格中换行?

[英]How to wrap text in Polaris Index Table cell?

am using Polaris Index Table to display some data in my Shopify app.我正在使用 Polaris Index Table 在我的 Shopify 应用程序中显示一些数据。 One of the cells in my table has a long string of text and I want to make it wrap so that it fits the size of the screen.我表格中的一个单元格有一长串文本,我想让它换行以适合屏幕大小。 Is there a way to do this in Polaris?有没有办法在北极星中做到这一点?

Here is my code:这是我的代码:

import {IndexTable, Card, useIndexResourceState, Text} from '@shopify/polaris';
import React from 'react';

function SimpleIndexTableExample() {
  const customers = [
    {
      id: '3411',
      url: 'customers/341',
      name: 'Mae Jemison long text here, very very long......',
      location: 'Decatur, USA',
      orders: 20,
      amountSpent: '$2,400',
    },
    {
      id: '2561',
      url: 'customers/256',
      name: 'Ellen Ochoa',
      location: 'Los Angeles, USA',
      orders: 30,
      amountSpent: '$140',
    },
  ];
  const resourceName = {
    singular: 'customer',
    plural: 'customers',
  };

  const {selectedResources, allResourcesSelected, handleSelectionChange} =
    useIndexResourceState(customers);

  const rowMarkup = customers.map(
    ({id, name, location, orders, amountSpent}, index) => (
      <IndexTable.Row
        id={id}
        key={id}
        selected={selectedResources.includes(id)}
        position={index}
      >
        <IndexTable.Cell>
          <Text variant="bodyMd" fontWeight="bold" as="span">
            {name}
          </Text>
        </IndexTable.Cell>
        <IndexTable.Cell>{location}</IndexTable.Cell>
        <IndexTable.Cell>{orders}</IndexTable.Cell>
        <IndexTable.Cell>{amountSpent}</IndexTable.Cell>
      </IndexTable.Row>
    ),
  );

  return (
    <Card>
      <IndexTable
        resourceName={resourceName}
        itemCount={customers.length}
        selectedItemsCount={
          allResourcesSelected ? 'All' : selectedResources.length
        }
        onSelectionChange={handleSelectionChange}
        headings={[
          {title: 'Name'},
          {title: 'Location'},
          {title: 'Order count'},
          {title: 'Amount spent'},
        ]}
      >
        {rowMarkup}
      </IndexTable>
    </Card>
  );
}

I've tried modifying the cells, styles, etc.我试过修改单元格,styles 等。

To make the text wrap in a cell in a Polaris IndexTable, you can apply the textWrap property to the Text component that you are using within the cell.要使文本在 Polaris IndexTable 的单元格中换行,您可以将 textWrap 属性应用于您在单元格中使用的文本组件。

You may try this,你可以试试这个,

 import {IndexTable, Card, useIndexResourceState, Text} from '@shopify/polaris'; import React from 'react'; function SimpleIndexTableExample() { //... const rowMarkup = customers.map( ({id, name, location, orders, amountSpent}, index) => ( <IndexTable.Row id={id} key={id} selected={selectedResources.includes(id)} position={index} > <IndexTable.Cell> <Text variant="bodyMd" fontWeight="bold" as="span" textWrap> {name} </Text> </IndexTable.Cell> <IndexTable.Cell>{location}</IndexTable.Cell> <IndexTable.Cell>{orders}</IndexTable.Cell> <IndexTable.Cell>{amountSpent}</IndexTable.Cell> </IndexTable.Row> ), ); //... }

I found a workaround for this – in my case there was a HTML representation of a text string available.我找到了一个解决方法——在我的例子中,有一个 HTML 表示的文本字符串可用。 Then I have used following construction in IndexTable.Row content:然后我在IndexTable.Row内容中使用了以下结构:

<div dangerouslySetInnerHTML={{__html: myHTMLContent}} />

This allowed using line breaks and other HTML that can be formatted in a way you need.这允许使用换行符和其他可以按您需要的方式格式化的 HTML。 Of course I understand that this adds another problem of creating HTML string, but this is just a workaround.当然我知道这增加了创建 HTML 字符串的另一个问题,但这只是一种解决方法。

PS that this method adds a risk of XSS attack so can recommend to use this only if you 100% understand the security side. PS ,此方法增加了 XSS 攻击的风险,因此建议您仅在 100% 了解安全方面的情况下才使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM