Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting value by array of ids #64

Open
robodna opened this issue Jul 6, 2024 · 5 comments
Open

Setting value by array of ids #64

robodna opened this issue Jul 6, 2024 · 5 comments
Labels
question Further information is requested

Comments

@robodna
Copy link

robodna commented Jul 6, 2024

Is there a way to set the value of the solid select by using an index/key value instead of setting the value to and object or array of objects? So if my object is {id:1,name:"test"} I would want set value to 1 to select.

@martinpengellyphillips
Copy link
Contributor

I'm not clear what you are asking. Please share the code you have so far to explain the issue better 😀

Also, have you looked through the examples on https://solid-select.com/ ?

At a guess, https://solid-select.com/?example=Format or https://solid-select.com/?example=Format%2520%28Options%29 might help.

@martinpengellyphillips martinpengellyphillips added the question Further information is requested label Jul 6, 2024
@robodna
Copy link
Author

robodna commented Jul 6, 2024

I meant to be able to set the value of solid select like this: <Select value={1} or <Select value={[1,2,3]} instead of <Select value={{id:1,name="Test"}}

@martinpengellyphillips
Copy link
Contributor

🤔 You can set whatever you want as a value (e.g. <Select initialValue={1} /> would display '1' in the select control).

If you can share a full example of what you want to achieve and where it is not working then I'll be able to guide better.

@martinpengellyphillips
Copy link
Contributor

I'm not sure why you'd want to do this, but maybe this is the sort of thing you are after?

import { Select } from "@thisbeyond/solid-select";
import { createSignal } from "solid-js";

export const IndirectionExample = () => {
  const options = [
    { id: 1, name: "apple" },
    { id: 2, name: "pear" },
  ];

  const [value, setValue] = createSignal(1);

  return (
    <div
      style={{
        flex: 1,
        display: "flex",
        "flex-direction": "column",
        gap: "8px",
      }}
    >
      <Select
        initialValue={value()}
        options={options}
        format={(item, type) =>
          type === "option"
            ? item.name
            : options.find((o) => o.id === item).name
        }
        optionToValue={(option) => option.id}
        onChange={setValue}
      />
      Current value: {value()}
    </div>
  );
};

@neerava2023
Copy link

neerava2023 commented Nov 18, 2024

Is it not possible to add a 'selectedOption' prop which takes a callback like so (item: T) => boolean with this its up to the user to decide how they would like to manage default selected options?

Also I noticed when the 'initialValue' is set you call onChange which is really not such a great idea.
In my case the onchange triggers a prompt which asks the user whether they truly want to update the selected option.
Please consider this addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants