carbon-design-system / ibm-products

A Carbon-powered React component library for IBM Products
https://ibm-products.carbondesignsystem.com
Apache License 2.0
93 stars 137 forks source link

NotificationsEmptyState has incorrect types #6046

Open oliver-wymer opened 1 week ago

oliver-wymer commented 1 week ago

Package

Carbon for IBM Products

Description

Firstly, thanks so much for TS support! Makes a big difference!

I have 2 issues which can kind of be wrapped into 1.

I'm trying to use the NotificationsEmptyState component as per the storybooks and docs in the story books but I'm seeing type errors. It looks like the types aren't correct and it'd also be nice if they were exported.

The type is incorrect (or iconDescription isn't a field and storybook is wrong)

As per the storybook we should be able to do this (example NotificationsEmptyState - With Action Icon Button):

<NotificationsEmptyState
  action={{
    iconDescription: 'Add icon',
    onClick: function Ua(){},
    renderIcon: function Ua(){},
    text: 'Create new'
  }}
  illustrationDescription="Test alt text"
  subtitle="Description text explaining why this section is empty."
  title="Empty state title"
/>

But the type for action is defined as:

 action?: {
        kind?: 'primary' | 'secondary' | 'tertiary';
        renderIcon?: CarbonIconType;
        onClick?: ButtonProps['onClick'];
        text?: string;
    };

Exporting the type would be helpful

So for example what I want to be able to do:

  const action: NotificationsEmptyStateProps["action"] = {
    text: "My text",
    kind: "primary"
    onClick: () => {},
  }

  <NotificationsEmptyState
        title="Create a rule to get started."
        subtitle="You need to create custom rules to monitor the task status and destinations to define where the notification will be sent."
        action={action}
      />

But what I have to do:

  // No auto-complete and type errors below
  // Notice "as const" to satisfy the type
  const action = {
    text: "My text",
    kind: "primary" as const,
    onClick: () => {},
  }

<NotificationsEmptyState
        title="Create a rule to get started."
        subtitle="You need to create custom rules to monitor the task status and destinations to define where the notification will be sent."
        action={action}
      />

I tried to create sandbox from component in storybook but it couldn't be found...

Component(s) impacted

NotificationsEmptyState

Browser

Chrome

@carbon/ibm-products (previously @carbon/ibm-cloud-cognitive) version

2.49.0

Suggested Severity

Severity 3 = The problem is visible or noticeable to users but does not impede the usability or functionality. Affects minor functionality, has a workaround.

Product/offering

API Connect

CodeSandbox or Stackblitz example

https://codesandbox.io/p/sandbox/github/carbon-design-system/ibm-products/tree/main/examples/carbon-for-ibm-products/NotificationsEmptyState?file=%2Fsrc%2FExample%2FExample.jsx

Steps to reproduce the issue (if applicable)

No response

Release date (if applicable)

No response

Code of Conduct

nchevsky commented 1 week ago

@oliver-wymer You can reference the type of the action prop like so:

React.ComponentProps<typeof NotificationsEmptyState>['action']

This is always more robust and future-proof than relying on one-off type exports. I personally would inline your action altogether, eliminating the need for explicit typing in the first place.

Regarding the type itself being incorrect, your description doesn't clearly point out what exactly is wrong with it (is it the missing iconDescription?)—I recommend making this clear for when a Carbon maintainer picks up the issue. 🙂