Skip to content

Commit ecc0fe5

Browse files
authored
Merge pull request #296 from csandman/chore/cleanup
chore: Fix `GroupHeading` props + some other cleanup
2 parents 084095c + e434952 commit ecc0fe5

File tree

7 files changed

+70
-57
lines changed

7 files changed

+70
-57
lines changed

src/chakra-components/control.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const Control = <
9898
data-focus-visible={isFocused ? true : undefined}
9999
data-invalid={isInvalid ? true : undefined}
100100
data-disabled={isDisabled ? true : undefined}
101-
aria-readonly={isReadOnly ? true : undefined}
101+
data-readonly={isReadOnly ? true : undefined}
102102
>
103103
{children}
104104
</Box>
@@ -139,10 +139,11 @@ export const IndicatorSeparator = <
139139
/**
140140
* Borrowed from the `@chakra-ui/icons` package to prevent needing it as a dependency
141141
*
142-
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/main/packages/icons/src/ChevronDown.tsx}
142+
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/61f965a/packages/components/icons/src/ChevronDown.tsx}
143+
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/61f965a/packages/components/select/src/select.tsx#L168-L179}
143144
*/
144145
export const DownChevron = (props: IconProps) => (
145-
<Icon {...props}>
146+
<Icon role="presentation" focusable="false" aria-hidden="true" {...props}>
146147
<path
147148
fill="currentColor"
148149
d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"
@@ -187,7 +188,7 @@ export const DropdownIndicator = <
187188
};
188189
const iconSize = iconSizes[size];
189190

190-
const initialSx: SystemStyleObject = {
191+
const initialDropdownIndicatorSx: SystemStyleObject = {
191192
...inputStyles.addon,
192193
display: "flex",
193194
alignItems: "center",
@@ -205,17 +206,17 @@ export const DropdownIndicator = <
205206
cursor: "inherit",
206207
}),
207208
};
208-
const sx = chakraStyles?.dropdownIndicator
209-
? chakraStyles.dropdownIndicator(initialSx, props)
210-
: initialSx;
209+
const dropdownIndicatorSx = chakraStyles?.dropdownIndicator
210+
? chakraStyles.dropdownIndicator(initialDropdownIndicatorSx, props)
211+
: initialDropdownIndicatorSx;
211212

212-
const initialIconStyles = {
213+
const initialDownChevronSx: SystemStyleObject = {
213214
height: "1em",
214215
width: "1em",
215216
};
216-
const iconSx: SystemStyleObject = chakraStyles?.downChevron
217-
? chakraStyles.downChevron(initialIconStyles, props)
218-
: initialIconStyles;
217+
const downChevronSx = chakraStyles?.downChevron
218+
? chakraStyles.downChevron(initialDownChevronSx, props)
219+
: initialDownChevronSx;
219220

220221
return (
221222
<Box
@@ -227,17 +228,17 @@ export const DropdownIndicator = <
227228
},
228229
className
229230
)}
230-
sx={sx}
231+
sx={dropdownIndicatorSx}
231232
>
232-
{children || <DownChevron sx={iconSx} />}
233+
{children || <DownChevron sx={downChevronSx} />}
233234
</Box>
234235
);
235236
};
236237

237238
/**
238239
* Borrowed from Chakra UI source
239240
*
240-
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/13c6d2e08b61e179773be4722bb81173dd599306/packages/close-button/src/close-button.tsx#L14}
241+
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/61f965a/packages/components/close-button/src/close-button.tsx#L12-L21}
241242
*/
242243
export const CrossIcon = (props: IconProps) => (
243244
<Icon focusable="false" aria-hidden {...props}>

src/chakra-components/input.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Input = <
1616
className,
1717
cx,
1818
value,
19-
selectProps: { chakraStyles, isReadOnly, isRequired },
19+
selectProps: { chakraStyles, isReadOnly },
2020
} = props;
2121
const { innerRef, isDisabled, isHidden, inputClassName, ...innerProps } =
2222
cleanCommonProps(props);
@@ -76,8 +76,6 @@ const Input = <
7676
sx={inputSx}
7777
disabled={isDisabled}
7878
readOnly={isReadOnly ? true : undefined}
79-
aria-readonly={isReadOnly ? true : undefined}
80-
aria-required={isRequired ? true : undefined}
8179
{...innerProps}
8280
/>
8381
</Box>

src/chakra-components/menu.tsx

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
OptionProps,
1515
} from "react-select";
1616
import type { SizeProps, ThemeObject } from "../types";
17-
import { useSize } from "../utils";
17+
import { cleanCommonProps, useSize } from "../utils";
1818

1919
const alignToControl = (placement: CoercedMenuPlacement) => {
2020
const placementToCSSProp = { bottom: "top", top: "bottom" };
@@ -118,6 +118,7 @@ export const MenuList = <
118118

119119
return (
120120
<Box
121+
role="listbox"
121122
{...innerProps}
122123
className={cx(
123124
{
@@ -259,7 +260,10 @@ export const Group = <
259260

260261
const { chakraStyles } = selectProps;
261262

262-
const sx = chakraStyles?.group ? chakraStyles.group({}, props) : {};
263+
const initialSx: SystemStyleObject = {};
264+
const sx = chakraStyles?.group
265+
? chakraStyles.group(initialSx, props)
266+
: initialSx;
263267

264268
return (
265269
<Box {...innerProps} className={cx({ group: true }, className)} sx={sx}>
@@ -288,11 +292,12 @@ export const GroupHeading = <
288292
const {
289293
cx,
290294
className,
291-
children,
292295
// eslint-disable-next-line deprecation/deprecation
293296
selectProps: { chakraStyles, size: sizeProp, hasStickyGroupHeaders },
294297
} = props;
295298

299+
const { data, ...innerProps } = cleanCommonProps(props);
300+
296301
const menuStyles = useMultiStyleConfig("Menu");
297302

298303
const size = useSize(sizeProp);
@@ -325,9 +330,11 @@ export const GroupHeading = <
325330
: initialSx;
326331

327332
return (
328-
<Box className={cx({ "group-heading": true }, className)} sx={sx}>
329-
{children}
330-
</Box>
333+
<Box
334+
{...innerProps}
335+
className={cx({ "group-heading": true }, className)}
336+
sx={sx}
337+
/>
331338
);
332339
};
333340

@@ -371,20 +378,24 @@ export const Option = <
371378
},
372379
} = props;
373380

374-
const size = useSize(sizeProp);
375-
376381
const menuItemStyles: ThemeObject = useMultiStyleConfig("Menu").item;
377382

378-
const paddings: SizeProps = {
379-
sm: "0.3rem 0.6rem",
380-
md: "0.4rem 0.8rem",
381-
lg: "0.5rem 1rem",
383+
const size = useSize(sizeProp);
384+
const horizontalPaddingOptions: SizeProps = {
385+
sm: "0.6rem",
386+
md: "0.8rem",
387+
lg: "1rem",
388+
};
389+
const verticalPaddingOptions: SizeProps = {
390+
sm: "0.3rem",
391+
md: "0.4rem",
392+
lg: "0.5rem",
382393
};
383394

384395
/**
385-
* Use the same selected color as the border of the select component
396+
* Use the same selected color as the border/shadow of the select/input components
386397
*
387-
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/13c6d2e08b61e179773be4722bb81173dd599306/packages/theme/src/components/input.ts#L73}
398+
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/61f965a/packages/components/theme/src/components/input.ts#L92-L93}
388399
*/
389400
const selectedBg = useColorModeValue(
390401
`${selectedOptionColorScheme}.500`,
@@ -394,39 +405,38 @@ export const Option = <
394405

395406
// Don't create exta space for the checkmark if using a multi select with
396407
// options that dissapear when they're selected
397-
const showCheckIcon: boolean =
408+
const showCheckIcon =
398409
selectedOptionStyle === "check" &&
399410
(!isMulti || hideSelectedOptions === false);
400411

401-
const shouldHighlight: boolean =
402-
selectedOptionStyle === "color" && isSelected;
412+
const shouldHighlight = selectedOptionStyle === "color";
403413

404414
const initialSx: SystemStyleObject = {
405415
...menuItemStyles,
416+
cursor: "pointer",
406417
display: "flex",
407418
alignItems: "center",
408419
width: "100%",
409420
textAlign: "start",
410421
fontSize: size,
411-
padding: paddings[size],
412-
...(isFocused && menuItemStyles._focus),
422+
paddingX: horizontalPaddingOptions[size],
423+
paddingY: verticalPaddingOptions[size],
413424
...(shouldHighlight && {
414-
bg: selectedBg,
415-
color: selectedColor,
416-
_active: { bg: selectedBg },
425+
_selected: {
426+
bg: selectedBg,
427+
color: selectedColor,
428+
_active: { bg: selectedBg },
429+
},
417430
}),
418-
...(isDisabled && menuItemStyles._disabled),
419-
...(isDisabled && { _active: {} }),
420431
};
421432

422433
const sx = chakraStyles?.option
423434
? chakraStyles.option(initialSx, props)
424435
: initialSx;
425-
426436
return (
427437
<Box
438+
role="option"
428439
{...innerProps}
429-
role="button"
430440
className={cx(
431441
{
432442
option: true,
@@ -438,8 +448,9 @@ export const Option = <
438448
)}
439449
sx={sx}
440450
ref={innerRef}
441-
data-disabled={isDisabled ? true : undefined}
451+
data-focus={isFocused ? true : undefined}
442452
aria-disabled={isDisabled ? true : undefined}
453+
aria-selected={isSelected}
443454
>
444455
{showCheckIcon && (
445456
<MenuIcon

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type {
1818
Size,
1919
TagVariant,
2020
SelectedOptionStyle,
21+
ColorScheme,
2122
StylesFunction,
2223
ChakraStylesConfig,
2324
OptionBase,

src/module-augmentation.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { SystemStyleObject } from "@chakra-ui/system";
33
import type { GroupBase, StylesConfig, ThemeConfig } from "react-select";
44
import type {
55
ChakraStylesConfig,
6+
ColorScheme,
67
SelectedOptionStyle,
78
SizeProp,
89
TagVariant,
@@ -68,11 +69,14 @@ declare module "react-select/base" {
6869
/**
6970
* If true, the form control will be required. This has 2 side effects:
7071
*
71-
* - The `FormLabel` will show a required indicator
72-
* - The form element (e.g, Input) will have `aria-required` set to true
72+
* - The hidden input element will get the required attribute, triggering native form validation on submit
73+
* - The combobox input will have `aria-required` set to true
7374
*
7475
* @see {@link https://chakra-ui.com/docs/components/input/props}
7576
* @see {@link https://chakra-ui.com/docs/components/form-control/props}
77+
@@ -86,7 +87,7 @@ declare module "react-select/base" {
78+
* @see {@link https://github.com/csandman/chakra-react-select#colorscheme}
79+
* @see {@link https://chakra-ui.com/docs/components/tag/props}
7680
*/
7781
isRequired?: boolean;
7882

@@ -86,7 +90,7 @@ declare module "react-select/base" {
8690
* @see {@link https://github.com/csandman/chakra-react-select#colorscheme}
8791
* @see {@link https://chakra-ui.com/docs/components/tag/props}
8892
*/
89-
colorScheme?: string;
93+
colorScheme?: ColorScheme;
9094

9195
/**
9296
* The `variant` prop that will be forwarded to your `MultiValue` component
@@ -132,12 +136,12 @@ declare module "react-select/base" {
132136
* @defaultValue `blue`
133137
* @see {@link https://github.com/csandman/chakra-react-select#selectedoptioncolorscheme--default-blue}
134138
*/
135-
selectedOptionColorScheme?: string;
139+
selectedOptionColorScheme?: ColorScheme;
136140

137141
/**
138142
* @deprecated Replaced by {@link selectedOptionColorScheme}
139143
*/
140-
selectedOptionColor?: string;
144+
selectedOptionColor?: ColorScheme;
141145

142146
/**
143147
* The color value to style the border of the `Control` with when the

src/types.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
Pseudos,
33
ResponsiveObject,
44
SystemStyleObject,
5+
ThemeTypings,
56
} from "@chakra-ui/system";
67
import type {
78
ClearIndicatorProps,
@@ -45,16 +46,13 @@ export type Size = "sm" | "md" | "lg";
4546

4647
export type SizeProp = Size | ResponsiveObject<Size> | Size[];
4748

48-
export type TagVariant = "subtle" | "solid" | "outline" | (string & {});
49+
export type TagVariant = ThemeTypings["components"]["Tag"]["variants"];
4950

5051
export type SelectedOptionStyle = "color" | "check";
5152

52-
export type Variant =
53-
| "outline"
54-
| "filled"
55-
| "flushed"
56-
| "unstyled"
57-
| (string & {});
53+
export type Variant = ThemeTypings["components"]["Input"]["variants"];
54+
55+
export type ColorScheme = ThemeTypings["colorSchemes"];
5856

5957
export type StylesFunction<ComponentProps> = (
6058
provided: SystemStyleObject,

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Size, SizeProp } from "./types";
88
*
99
* Borrowed from the original `react-select` package
1010
*
11-
* @see {@link https://github.com/JedWatson/react-select/blob/edf5265ee0158c026c9e8527a6d0490a5ac2ef23/packages/react-select/src/utils.ts#L75-L110}
11+
* @see {@link https://github.com/JedWatson/react-select/blob/2f94e8d/packages/react-select/src/utils.ts#L79-L110}
1212
*/
1313
export const cleanCommonProps = <
1414
Option,

0 commit comments

Comments
 (0)