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

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

Maintainers usually reply within 1 day

@ashutosh887 is already working on this.

Since Jan 15, 2022.

Assessment

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

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.

Description

stale

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.

Dominant language
JavaScript
Stars
994
Forks
444
Avg merge
3d 23h
Merged PRs (30d)
44

Getting set up

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.

More from salesforce/design-system-react

All issues in salesforce/design-system-react

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.