Can we have this customizations in chakra react select #37
-
First of all huge thanks for Building this awesome component and library.Just wanted some suggestion if following possibilites are possible in the library. Can we checkbox the inside the dropdown along with the label Please find the attached below for references. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Let's start with the easiest question first, the focused border color of the input can be changed using the As for the rest, it is possible to style a selected option with a checkmark, however its not a checkbox exactly, it's the style of the selected menu option from chakra. This can be used instead of the default color highlighting by passing If you want to change the styles of a selected option by adding your own custom icon, there are two ways you could do it. The first way would be to wrap my import { chakraComponents } from "chakra-react-select" The second option would be to completely replace my For documentation on how to implement either of those options, you should check out the original react-select docs on custom components. If it wasn't Friday evening I'd throw together a quick example of how you might accomplish this but if you can wait a bit I'll try and get you an example sometime next week! |
Beta Was this translation helpful? Give feedback.
-
So I know I said I'd make an example for how to do this before, but I kind of spaced on it for a while. I did end up making an example of how you could implement this since then though. In fact I made 2 versions, one using the styles from the Chakra UI RadioYou example image looks very close to the chakra-ui Radio so I'll start with that. In order to implement it I, I made a custom CodeSandbox: https://codesandbox.io/s/chakra-react-select-radio-q1vvgg?file=/example.js import { useMultiStyleConfig, chakra } from "@chakra-ui/react";
import { Select, chakraComponents } from "chakra-react-select";
export const colorOptions = [
{ value: "blue", label: "Blue" },
{ value: "purple", label: "Purple" },
{ value: "red", label: "Red" },
{ value: "orange", label: "Orange" },
{ value: "yellow", label: "Yellow" },
{ value: "green", label: "Green" }
];
const Option = ({ children, isSelected, ...props }) => {
const { control } = useMultiStyleConfig("Radio", { colorScheme: "orange" });
// Extra styles added to the radio control
// https://github.com/chakra-ui/chakra-ui/blob/main/packages/radio/src/radio.tsx#L111
const css = {
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
flexShrink: 0,
...control,
...(isSelected && control._checked)
};
return (
<chakraComponents.Option {...props}>
<chakra.span __css={css} mr={3} />
{children}
</chakraComponents.Option>
);
};
const Example = () => (
<Select
name="colors"
options={colorOptions}
placeholder="Select some colors..."
closeMenuOnSelect={false}
components={{ Option }}
/>
); CheckboxWhile you didn't request a CodeSandbox: https://codesandbox.io/s/chakra-react-select-checkbox-uvn325?file=/example.js import { useMultiStyleConfig, chakra } from "@chakra-ui/react";
import { Select, chakraComponents } from "chakra-react-select";
import { CheckboxIcon } from "./components/checkbox-icon";
export const colorOptions = [
{ value: "blue", label: "Blue" },
{ value: "purple", label: "Purple" },
{ value: "red", label: "Red" },
{ value: "orange", label: "Orange" },
{ value: "yellow", label: "Yellow" },
{ value: "green", label: "Green" }
];
// Based on the source for @chakra-ui/checkbox
// https://github.com/chakra-ui/chakra-ui/blob/main/packages/checkbox/src/checkbox.tsx#L18
const CheckboxControl = chakra("span", {
baseStyle: {
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
verticalAlign: "top",
userSelect: "none",
flexShrink: 0
}
});
const Option = ({ children, isSelected, ...props }) => {
const { control, icon } = useMultiStyleConfig("Checkbox", {
colorScheme: "orange"
});
// https://github.com/chakra-ui/chakra-ui/blob/main/packages/checkbox/src/checkbox.tsx#L134
const iconStyles = {
opacity: isSelected ? 1 : 0,
transform: isSelected ? "scale(1)" : "scale(0.95)",
...icon
};
return (
<chakraComponents.Option {...props}>
<CheckboxControl
__css={{ ...control, ...(isSelected && control._checked) }}
mr={3}
>
<CheckboxIcon __css={iconStyles} isChecked={isSelected} />
</CheckboxControl>
{children}
</chakraComponents.Option>
);
};
const Example = () => (
<Select
isMulti
name="colors"
options={colorOptions}
placeholder="Select some colors..."
closeMenuOnSelect={false}
hideSelectedOptions={false}
components={{ Option }}
/>
); |
Beta Was this translation helpful? Give feedback.
-
Damm Thankyou so much for All the Support and Help. I overlooked this stuff but thanks for getting into details helping me out. |
Beta Was this translation helpful? Give feedback.
-
@csandman thanks for these suggestions. But how to change the bg color for all selected options? (
|
Beta Was this translation helpful? Give feedback.
So I know I said I'd make an example for how to do this before, but I kind of spaced on it for a while. I did end up making an example of how you could implement this since then though. In fact I made 2 versions, one using the styles from the Chakra UI
Radio
and one using theCheckbox
.Radio
You example image looks very close to the chakra-ui Radio so I'll start with that. In order to implement it I, I made a custom
Option
component and pulled the styles for theRadio
from the Chakra theme. Here's the code/example image:CodeSandbox: https://codesandbox.io/s/chakra-react-select-radio-q1vvgg?file=/example.js