Skip to content

Combo: multiple options whose IDs coerce to the same number (e.g., “0”, “00”, “0E1”) are not selectable by mouse #17

@EugeneGoroschenya

Description

@EugeneGoroschenya

According to doc it is allowed to use values of string type as option.id: https://docs.svar.dev/svelte/core/combo/api/props/options/#usage

But in case there are options with id like "0", "00", "0E1" they can't be selected by mouse.

<script>
  import { Combo, Willow } from "@svar-ui/svelte-core";

  const ids = ['', '0', '000', '0E1'];
  const options = ids.map(id => ({id, label: `"${id}" option`}));
  let value = $state(ids.at(-1));
</script>

<Willow>
  <Combo {options} bind:value={value} />
  <p>Selected option: {JSON.stringify(value)}</p>	
</Willow>

Playground: https://svelte.dev/playground/ef444580799640838ccc1954887b102d?version=5.44.0

Selection by keyboard works except for option with "" (empty string) id.

As it seems the issue is related to internal usage of data-id attribute.

const id = locateID(ev);
// if we have id:"1", the locateID will return 1 as number
// so using non-strict comparison
const index = options.findIndex(a => a.id == id);

https://github.com/svar-widgets/lib-dom/blob/c33f4f203db888988e564e4c21e9f84ce3c24a86/src/locate.ts#L49-L52

	if (typeof value === "string") {
		const t = (value as undefined as number) * 1;
		if (!isNaN(t)) return t;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions