svelte-material-ui icon indicating copy to clipboard operation
svelte-material-ui copied to clipboard

Keyed Segmented Button breaks with keys of type number

Open BonaFideIT0 opened this issue 10 months ago • 3 comments

Describe the bug When building a keyed segmented button, using number keys breaks the button.

To Reproduce

Create keyed segmented button with keys of type number that don't match the array index (i.e. offset by one):

[{key: 1, label: test1}, ...]

Expected behavior Expected the button to work as intended.

Additional context

function selectSegment(indexOrSegmentId: any) {
    let index = segments.findIndex(
      (segment) => key(segment) === indexOrSegmentId,
    );
    if (index === -1) {
      index = indexOrSegmentId;
    }

This piece of code is called with the index of the segment but assumes the key never matches the index and determines if the argument is an index or key. The mismatched case breaks the whole element.

Temporary solution is to create a key like this: segment-${value}.

BonaFideIT0 avatar Jun 12 '25 10:06 BonaFideIT0

I would at least think a note in the demo should be added to warn of not using numbers as keys.

BonaFideIT0 avatar Jun 12 '25 10:06 BonaFideIT0