GithubHelp home page GithubHelp logo

Comments (14)

saturnonearth avatar saturnonearth commented on June 3, 2024 2

Also, on another note, is there a reason why we need to pass:

type SelectOption<T> = {
	value: T;
    label?: string
}

to the selected prop, as opposed to just a string value?

This seems odd to me because

  1. What purpose would the label serve in this situation?
  2. It strays away from the native Select where you just pass a straight value.

It's not a huge deal but it does (unless I am missing an easy way), prevent me from binding selected to a single writable store String, since it is now an object.

I'm sure there could be reasons for this so I don't want to sound rude!

from bits-ui.

pheuter avatar pheuter commented on June 3, 2024 1

On a possibly related note, <Select.Input name="favoriteFruit" /> doesn't appear to work, the value of the hidden input field is set to "[object Object]". It used to be set to the option.value automatically.

from bits-ui.

Rykuno avatar Rykuno commented on June 3, 2024 1

On a possibly related note, <Select.Input name="favoriteFruit" /> doesn't appear to work, the value of the hidden input field is set to "[object Object]". It used to be set to the option.value automatically.

Also having this issue atm with Superforms. If anyone has a solution that would be pretty cool in the meantime.

from bits-ui.

huntabyte avatar huntabyte commented on June 3, 2024 1

^ @saturnonearth that is correct. We had issues with SSR & attempting to keep the select label/value combo in sync, especially when you're conditionally rendering the options under an {#if open} (which we're doing in bits to support transitions).

I'm working on trying to fix the issues you all are experiencing though!

from bits-ui.

mcmxcdev avatar mcmxcdev commented on June 3, 2024

+1 Ohh this is bothering me too, great that you confirmed this!

I am using raw melt-ui though, that would need to be fixed there I assume.

from bits-ui.

huntabyte avatar huntabyte commented on June 3, 2024

@mcmxcdev , I believe I know why this is happening, if you wouldn't mind opening an issue on Melt explaining some situations that you've run into this, it would be incredible!

from bits-ui.

huntabyte avatar huntabyte commented on June 3, 2024

I believe I may have fixed this, can you confirm using the latest version of bits-ui?

from bits-ui.

saturnonearth avatar saturnonearth commented on June 3, 2024

I believe I may have fixed this, can you confirm using the latest version of bits-ui?

It seems to work but...it's not liking the new prop: selected.

Type 'string' is not assignable to type 'SelectOption<unknown>'

I am just passing a Store with a string in it.

Also, should it be onSelectChange instead of onSelectedChange to match melt-ui?

from bits-ui.

huntabyte avatar huntabyte commented on June 3, 2024

Yeah it should be onSelectedChange I will make that fix! So SelectOption looks like this:

type SelectOption<T> = {
	value: T;
    label?: string
}

So you'd need to set the value to that string!

from bits-ui.

saturnonearth avatar saturnonearth commented on June 3, 2024

I was wondering, is the default value that displays in the Select always supposed to be the placeholder, even if the selected value matches one of the Options? This seems to go against the native Select behavior, but I wasn't sure if this was intentional or not.

As an example...

If the user has the value "apple" value already selected when loading the page, one would assume the Select shows this:
CleanShot 2023-08-27 at 16 10 46@2x

But it's actually showing...
CleanShot 2023-08-27 at 16 11 34@2x

from bits-ui.

mcmxcdev avatar mcmxcdev commented on June 3, 2024

I think that was also an issue I had with Select, the label was not synced up with the value.

from bits-ui.

saturnonearth avatar saturnonearth commented on June 3, 2024

In regards to....

I was wondering, is the default value that displays in the Select always supposed to be the placeholder, even if the selected value matches one of the Options? This seems to go against the native Select behavior, but I wasn't sure if this was intentional or not.

and....

I think that was also an issue I had with Select, the label was not synced up with the value.

I found the reason for this behavior. Basically whatever the selected label is:

type SelectOption<T> = {
	value: T;
    label?: string
}

will show up as the default label (even if the value is set to the correct associated item.

If there is no label property present for selected, it will default to the Select.Trigger Select.Value placeholder value.

from bits-ui.

0xHati avatar 0xHati commented on June 3, 2024

Yeah it should be onSelectedChange I will make that fix! So SelectOption looks like this:

type SelectOption<T> = {
	value: T;
    label?: string
}

So you'd need to set the value to that string!

I don't get typing for the SelectOption in my onSelectedChange function.

(property) selected?: SelectOption<unknown> | undefined

I'm doing something like this to make a select component that I can reuse in my app, built on the blocks that shadcn gives me. Which in turn uses bits ui.

<script lang="ts">
	import * as Select from '$components/ui/select';
	import type { SelectOption } from '@melt-ui/svelte';
	import type { OnChangeFn } from 'bits-ui/dist/internal';

	export let data: Record<string, string>[];
	export let inputName: string;
	export let onSelectedChange: OnChangeFn<SelectOption<any> | undefined> | undefined;

	export let defaultSelected: SelectOption<string>;
</script>

<Select.Root
	selected={{ value: defaultSelected.value, label: defaultSelected.label }}
	{onSelectedChange}
>
	<Select.Trigger class="w-[180px]">
		<Select.Value />
	</Select.Trigger>
	<Select.Content>
		{#each data as { value, label }}
			<Select.Item {value} {label}>{label}</Select.Item>
		{/each}
	</Select.Content>
	<Select.Input name={inputName} />
</Select.Root>

The select primitive from shadcn

<script lang="ts">
	import { Select as SelectPrimitive } from 'bits-ui';

	type $$Props = SelectPrimitive.Props;

	export let selected: $$Props['selected'] = undefined;
	export let open: $$Props['open'] = undefined;
</script>

<SelectPrimitive.Root bind:selected bind:open {...$$restProps}>
	<slot />
</SelectPrimitive.Root>

Atm it's set to any, but I'd like it to set to the type of the selected. If I understand correctly that should be the case. I'm using bits and shadcn.

export let onSelectedChange: OnChangeFn<SelectOption<any> | undefined> | undefined;

from bits-ui.

huntabyte avatar huntabyte commented on June 3, 2024

Closed by #184

from bits-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.