salesforce / salesforce/design-system-react

Carousel: Changing the number of items in the carousel crashes the component

Open
#2,968 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale
Dominant language
JavaScript
Stars
982
Forks
440
Avg merge
1d 21h
Merged PRs (30d)
4

Description

In CarouselIndicators.render() each panel index is looped through:

{[...Array(props.noOfIndicators).keys()].map((index) => {

later inside the loop, the code is trying to access props.items using that index, however that index is an index into the number of panels not items so this:

id = props.items[index].id;

causes a TypeError: Cannot read properties of undefined (reading 'id').

To duplicate:

  render() {
    let items = [
      {
        id: 0,
        heading: 'Zero',
      },
      {
        id: 1,
        heading: 'One'
      },
      {
        id: 2,
        heading: 'Two',
      },
      {
        id: 3,
        heading: 'Three'
      }
    ];
    if (this.state.filter) {
      items = [items[0]];
    }

    return (
      <div>
        <Button label="Toggle" onClick={() => this.setState({ filter: true }) } />
        <Carousel
            hasPreviousNextPanelNavigation
            items={items}
            itemsPerPanel={3}
        />
      </div>
    );
  }

When you click the button you will experience the error.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at CarouselIndicators.render(), especially the loop over props.noOfIndicators and its access to props.items[index]. Reproduce the crash with the filtering example from the issue, then verify that changing the item count no longer causes a TypeError and that the carousel indicators remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.