eui icon indicating copy to clipboard operation
eui copied to clipboard

[EuiCard] `layout`, `description` `ExclusiveUnion`

Open thompsongl opened this issue 3 years ago • 3 comments

EuiCard accepts a complicated prop interface where multiple props are optional, restricted, or required based on other props: layout and the description-children complex are the main sources of concern.

TypeScript errors occur given the following configuration, which should valid:

<EuiCard
  layout={isGrid ? 'vertical' : 'horizontal'}
  icon={<EuiAvatar name={item.name} color={item.color} size="l" />}
  title={item.name}
  description={item.description}
  onClick={() => {}}
/>

Having a layout ternary with children is accepted, and having description with a simple layout value is accepted, but the combination presents errors.

thompsongl avatar Jan 18 '22 16:01 thompsongl

At a minimum, the children and description types need to be refactored. They are declared 2-4 times and the union might not be as restrictive as intended:

(
    | {
        // description becomes optional when children is present
        description?: NonNullable<ReactNode>;
        children: ReactNode;
      }
    | {
        // description is required if children is omitted
        description: NonNullable<ReactNode>;
      }
  )

thompsongl avatar Jan 18 '22 16:01 thompsongl

General comment about ExclusiveUnion, but I think we should be writing unit tests for all components with exclusive union props or conditional logic around props. Here's an example of a ExclusiveUnion prop unit test that I wrote recently.

Whatever fix we add for EuiCard should have tests for sure, but it also might be worth revisiting all components with ExclusiveUnion as a tech debt item for the future.

cee-chen avatar Jan 18 '22 16:01 cee-chen

On the consumer side, components that use ExclusiveUnion are very painful to configure. TS doesn't seem to be smart enough to know that certain conditions happen together. I honestly suggest removing this pattern and opting for CLI/console errors/warnings especially in cases where they don't really conflict they just can't render both so it prioritizes.

cchaos avatar Jan 18 '22 16:01 cchaos

👋 Hi there - this issue hasn't had any activity in 6 months. If the EUI team has not explicitly expressed that this is something on our roadmap, it's unlikely that we'll pick this issue up. We would sincerely appreciate a PR/community contribution if this is something that matters to you! If not, and there is no further activity on this issue for another 6 months (i.e. it's stale for over a year), the issue will be auto-closed.

github-actions[bot] avatar Sep 22 '25 00:09 github-actions[bot]