carbon-design-system / carbon-components-svelte

Svelte implementation of the Carbon Design System
https://svelte.carbondesignsystem.com
Apache License 2.0
2.69k stars 261 forks source link

specify which DataTable row can be expanded #861

Closed MrVoshel closed 2 years ago

MrVoshel commented 2 years ago

Is it currently possible to decide which row can be expanded in a DataTable? My issue is that when I specify the expandable prop it applies globally, even if I don't really need it.

<script>
    import { DataTable } from "carbon-components-svelte";

    const headers = [
        { key: 'id', value: 'ID' },
        { key: 'name', value: 'Name'}
    ];

    const rows = [
            { id: '1', name: 'a' },
            { id: '2', name: 'b' },
            { id: '3', name: 'c' }
    ];

</script>

<main>
    <DataTable expandable {headers} {rows}>
        <span slot="expanded-row" let:row>
            {#if row.id === '1'}
                <p>Some details for row {row.id}</p>
            {/if}
            <!-- I don't have any details for row.id 2, so I want to set
            the expandable prop to false -->
            {#if row.id === '3'}
                <p>Additional details for row {row.id}</p>
            {/if}
        </span>
    </DataTable>
</main>

brave_GpnZ6dSXSP

metonym commented 2 years ago

It's not currently supported, but this is a valid feature request.

metonym commented 2 years ago

@MrVoshel Would this API address your use case?

Code

Full code in PR

<DataTable
  expandable
  nonExpandableRowIds={["a", "b"]}
  headers={[
    { key: "name", value: "Name" },
    { key: "protocol", value: "Protocol" },
    { key: "port", value: "Port" },
    { key: "rule", value: "Rule" },
  ]}
  {rows}
/>

Result

expandable-datatable
MrVoshel commented 2 years ago

This completely solves my problem, thank you!

metonym commented 2 years ago

I'll keep this open until #862 is merged.

metonym commented 2 years ago

Released in v0.47.0.

"Non-expandable rows" example in the docs.

MrVoshel commented 2 years ago

@metonym it seems to mess up the UI of batchExpansion a bit when everything is expanded.

<DataTable batchExpansion nonExpandableRowIds={['2']} {headers} {rows}>
        <span slot="expanded-row" let:row>
            {#if row.id === '1'}
                <p>Some details for row {row.id}</p>
            {/if}
            <!-- I don't have any details for row.id 2, so I want to set
            the expandable prop to false -->
            {#if row.id === '3'}
                <p>Additional details for row {row.id}</p>
            {/if}
        </span>
    </DataTable>

image

metonym commented 2 years ago

Please be more specific. Expected vs unexpected behavior etc.

MrVoshel commented 2 years ago

This is how it should be image but if I use the batchExpansion this is what happens image The ID 2 is not clearly separated from the rest. Moreover if I use the batchExpansion to expand everything, but then close each row individually there is another problem image Hope that was clear.

metonym commented 2 years ago

What you're describing seems to be the intended design for a batch expandable data table.

This is what I'm seeing locally:

https://user-images.githubusercontent.com/10718366/138024164-74514ec5-6541-4337-9d67-753583b2eaa2.mov

Code:

<script>
  import { Grid, DataTable, Row, Column } from "carbon-components-svelte";

  const rows = [
    {
      id: "a",
      name: "Load Balancer 3",
      protocol: "HTTP",
      port: 3000,
      rule: "Round robin",
    },
    {
      id: "b",
      name: "Load Balancer 1",
      protocol: "HTTP",
      port: 443,
      rule: "Round robin",
    },
    {
      id: "c",
      name: "Load Balancer 2",
      protocol: "HTTP",
      port: 80,
      rule: "DNS delegation",
    },
    {
      id: "d",
      name: "Load Balancer 6",
      protocol: "HTTP",
      port: 3000,
      rule: "Round robin",
    },
    {
      id: "e",
      name: "Load Balancer 4",
      protocol: "HTTP",
      port: 443,
      rule: "Round robin",
    },
    {
      id: "f",
      name: "Load Balancer 5",
      protocol: "HTTP",
      port: 80,
      rule: "DNS delegation",
    },
  ];
</script>

<Grid padding>
  <Row>
    <Column>
      <DataTable
        batchExpansion
        nonExpandableRowIds={["a", "b"]}
        headers={[
          { key: "name", value: "Name" },
          { key: "protocol", value: "Protocol" },
          { key: "port", value: "Port" },
          { key: "rule", value: "Rule" },
        ]}
        {rows}
      >
        <svelte:fragment slot="expanded-row" let:row>
          {row.id}
        </svelte:fragment>
      </DataTable>
    </Column>
  </Row>
</Grid>
MrVoshel commented 2 years ago

Maybe I can explain it better with a video, let me know if this is intended or not, just seems a bit strange to me.

https://user-images.githubusercontent.com/54818101/138103670-8d06bb97-0390-4f57-b138-3106dd9937bb.mp4

metonym commented 2 years ago

@MrVoshel Understood – thanks

metonym commented 2 years ago

Fixed in v0.47.1

metonym commented 2 years ago

@MrVoshel Creating a separate issue (#867) to track the batchExpansion "expand all" issue