Code Monkey home page Code Monkey logo

Comments (4)

lcswillems avatar lcswillems commented on May 27, 2024 1

Thanks a lot for your help!!

from chakra-react-select.

csandman avatar csandman commented on May 27, 2024

I'll take a look at this! I'm not sure why this package would handle react nodes as labels differently than the original package. Do you have an example of what happens, like a CodeSandbox? I have templates in my Readme if you need. Also are you using TypeScript or vanilla JS?

from chakra-react-select.

lcswillems avatar lcswillems commented on May 27, 2024

After digging in, you are right that the limitation comes from the react-select. Maybe this issue should be closed and I should open a new one in react-select

from chakra-react-select.

csandman avatar csandman commented on May 27, 2024

Ah ok, that makes more sense. However, you aren't fully out of options. One way you could get the behavior you're looking for is by extending the Option component exported from this package to render custom elements you keep on your options object. This feature is explained in depth in the react-select docs

Here's a quick example I made demonstrating how to do it: https://codesandbox.io/s/chakra-react-select-custom-option-d99s7?file=/example.js

import { Icon } from "@chakra-ui/react";
import { Select, chakraComponents } from "chakra-react-select";
import {
  GiJellyBeans,
  GiChocolateBar,
  GiStrawberry,
  GiCherry
} from "react-icons/gi";

const flavorOptions = [
  {
    value: "vanilla",
    label: "Vanilla",
    icon: <Icon as={GiJellyBeans} color="orange.700" mr={2} h={5} w={5} />
  },
  {
    value: "chocolate",
    label: "Chocolate",
    icon: <Icon as={GiChocolateBar} color="yellow.800" mr={2} h={5} w={5} />
  },
  {
    value: "strawberry",
    label: "Strawberry",
    icon: <Icon as={GiStrawberry} color="red.500" mr={2} h={5} w={5} />
  },
  {
    value: "cherry",
    label: "Cherry",
    icon: <Icon as={GiCherry} color="red.600" mr={2} h={5} w={5} />
  }
];

const customComponents = {
  Option: ({ children, ...props }) => (
    <chakraComponents.Option {...props}>
      {props.data.icon} {children}
    </chakraComponents.Option>
  )
};

const Example = () => (
  <Select
    isMulti
    name="flavors"
    options={flavorOptions}
    placeholder="Select some flavors..."
    components={customComponents}
  />
);

If this suits your needs then feel free to close it for now. I don't plan on adding any features that fundamentally change the inner workings of react-select, so you're better off asking there if it's something you'd like natively added.

However, if you have any more questions let me know! Or if you want me to make a TypeScript example instead I could do that as well.

from chakra-react-select.

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.