Skip to content

Commit 5ab6ea2

Browse files
committed
Add hasStickyGroupHeaders option
1 parent d798446 commit 5ab6ea2

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
This component is a wrapper for the popular react component [react-select](https://react-select.com/home) made using the UI library [Chakra UI](https://chakra-ui.com/).
1010

11-
![Chakra React Select Banner](https://raw.githubusercontent.com/csandman/chakra-react-select/main/github/chakra-react-select.png)
11+
![Chakra React Select Banner](./github/chakra-react-select.png)
1212

1313
Check out the demo here: https://codesandbox.io/s/chakra-react-select-demo-65ohb?file=/example.js
1414

@@ -49,7 +49,8 @@ return (
4949
```js
5050
return (
5151
<Select
52-
colorScheme="purple" // The global color scheme
52+
{/* The global color scheme */}
53+
colorScheme="purple"
5354
options={[
5455
{
5556
label: "I am red",
@@ -71,7 +72,8 @@ return (
7172
```js
7273
return (
7374
<Select
74-
tagVariant="solid" // The global variant
75+
{/* The global variant */}
76+
tagVariant="solid"
7577
options={[
7678
{
7779
label: "I have the outline style",
@@ -108,7 +110,15 @@ return (
108110
);
109111
```
110112

111-
- In your options objects, you can add the key `isFixed: true` to emulate the example in the [react-select docs](https://react-select.com/home#fixed-options). This will prevent the options which have this flag from having the remove button on its corresponding tag. This only applies when using `isMulti`
113+
- One thing I added which isn't specific to Chakra or react-select is sticky group headers. It adds a border to the bottom of the header and keeps it in view while its corresponding group of options is visible. This can be very nice for when you have long lists of grouped options so you can always tell which group of options you're looking at. To add it, pass the `hasStickyGroupHeaders` prop to the select component.
114+
115+
```js
116+
return <Select hasStickyGroupHeaders />;
117+
```
118+
119+
![Chakra React Select Banner](./github/sticky-group-headers.png)
120+
121+
- In your options objects, you can add the key `isFixed: true` to emulate the example in the [react-select docs](https://react-select.com/home#fixed-options). This will prevent the options which have this flag from having the remove button on its corresponding tag. This only applies when using `isMulti` is passed.
112122

113123
```js
114124
return (

github/sticky-group-headers.png

63.1 KB
Loading

src/chakra-react-select.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,15 @@ const chakraComponents: ChakraSelectProps["components"] = {
266266
</Box>
267267
);
268268
},
269-
GroupHeading: ({ innerProps, children, selectProps: { size } }) => {
270-
const { groupTitle } = useStyles();
269+
GroupHeading: ({
270+
innerProps,
271+
children,
272+
selectProps: { size, hasStickyGroupHeaders },
273+
}) => {
274+
const {
275+
groupTitle,
276+
list: { bg },
277+
} = useStyles();
271278

272279
const chakraTheme = useTheme();
273280
const fontSizes: SizeProps = {
@@ -288,6 +295,10 @@ const chakraComponents: ChakraSelectProps["components"] = {
288295
fontSize: fontSizes[size as Size],
289296
p: paddings[size as Size],
290297
m: 0,
298+
borderBottomWidth: hasStickyGroupHeaders ? "1px" : 0,
299+
position: hasStickyGroupHeaders ? "sticky" : "static",
300+
top: -2,
301+
bg,
291302
}}
292303
{...innerProps}
293304
>
@@ -346,6 +357,7 @@ const ChakraReactSelect = ({
346357
isInvalid,
347358
inputId,
348359
tagVariant,
360+
hasStickyGroupHeaders = false,
349361
...props
350362
}: ChakraSelectProps): ReactElement => {
351363
const chakraTheme = useTheme();
@@ -426,6 +438,7 @@ const ChakraReactSelect = ({
426438
isDisabled: inputProps.disabled,
427439
isInvalid: !!inputProps["aria-invalid"],
428440
inputId: inputId || inputProps.id,
441+
hasStickyGroupHeaders,
429442
...props,
430443
// aria-invalid can be passed to react-select, so we allow that to
431444
// override the `isInvalid` prop

src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ export type Size = "sm" | "md" | "lg";
1010
export type TagVariant = "subtle" | "solid" | "outline" | undefined;
1111

1212
export interface ChakraSelectProps extends Props {
13-
size: Size;
14-
colorScheme: string;
15-
isInvalid: boolean;
13+
size?: Size;
14+
colorScheme?: string;
15+
isInvalid?: boolean;
1616
tagVariant?: TagVariant;
17+
hasStickyGroupHeaders?: boolean;
1718
}
1819

1920
export type OptionalTheme = {

0 commit comments

Comments
 (0)