Skip to content

Commit 095765d

Browse files
committed
fix: closure issue with renderMenuListFooter
1 parent 662cdd3 commit 095765d

File tree

3 files changed

+44
-61
lines changed

3 files changed

+44
-61
lines changed

src/Shared/Components/SelectPicker/SelectPicker.component.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import ReactSelect, {
1818
ControlProps,
1919
GroupHeadingProps,
20-
MenuListProps,
2120
MultiValueProps,
2221
OptionProps,
2322
SelectInstance,
@@ -182,10 +181,9 @@ import { SelectPickerOptionType, SelectPickerProps, SelectPickerVariantType } fr
182181
* />
183182
* ```
184183
*/
185-
const SelectPicker = <OptionValue, IsMulti extends boolean>({
184+
const SelectPicker = <OptionValue extends string | number, IsMulti extends boolean>({
186185
error,
187186
icon,
188-
renderMenuListFooter,
189187
helperText,
190188
placeholder = 'Select a option',
191189
label,
@@ -201,7 +199,6 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
201199
isMulti,
202200
name,
203201
classNamePrefix,
204-
renderCustomOptions,
205202
shouldRenderCustomOptions = false,
206203
isSearchable,
207204
selectRef,
@@ -264,18 +261,6 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
264261
[icon, shouldShowSelectedOptionIcon],
265262
)
266263

267-
const renderMenuList = useCallback(
268-
(menuProps: MenuListProps<SelectPickerOptionType<OptionValue>>) => (
269-
<SelectPickerMenuList
270-
{...menuProps}
271-
renderMenuListFooter={renderMenuListFooter}
272-
renderCustomOptions={renderCustomOptions}
273-
shouldRenderCustomOptions={shouldRenderCustomOptions}
274-
/>
275-
),
276-
[shouldRenderCustomOptions],
277-
)
278-
279264
const renderValueContainer = useCallback(
280265
(valueContainerProps: ValueContainerProps<SelectPickerOptionType<OptionValue>>) => (
281266
<SelectPickerValueContainer
@@ -335,6 +320,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
335320
'aria-invalid': !!error,
336321
'aria-labelledby': labelId,
337322
hideSelectedOptions: false,
323+
shouldRenderCustomOptions: shouldRenderCustomOptions || false,
338324
}),
339325
[
340326
name,
@@ -348,6 +334,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
348334
error,
349335
labelId,
350336
isMulti,
337+
shouldRenderCustomOptions,
351338
],
352339
)
353340

@@ -386,7 +373,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
386373
DropdownIndicator: SelectPickerDropdownIndicator,
387374
Control: renderControl,
388375
Option: renderOption,
389-
MenuList: renderMenuList,
376+
MenuList: SelectPickerMenuList,
390377
ClearIndicator: SelectPickerClearIndicator,
391378
ValueContainer: renderValueContainer,
392379
MultiValueLabel: renderMultiValueLabel,
@@ -410,7 +397,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
410397
DropdownIndicator: SelectPickerDropdownIndicator,
411398
Control: renderControl,
412399
Option: renderOption,
413-
MenuList: renderMenuList,
400+
MenuList: SelectPickerMenuList,
414401
ClearIndicator: SelectPickerClearIndicator,
415402
ValueContainer: renderValueContainer,
416403
}}

src/Shared/Components/SelectPicker/common.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,12 @@ export const SelectPickerOption = <OptionValue, IsMulti extends boolean>({
173173
)
174174
}
175175

176-
export const SelectPickerMenuList = <OptionValue, IsMulti extends boolean>({
177-
renderMenuListFooter,
178-
shouldRenderCustomOptions,
179-
renderCustomOptions,
180-
...props
181-
}: MenuListProps<SelectPickerOptionType<OptionValue>> &
182-
Pick<
183-
SelectPickerProps<OptionValue, IsMulti>,
184-
'renderMenuListFooter' | 'shouldRenderCustomOptions' | 'renderCustomOptions'
185-
>) => {
176+
export const SelectPickerMenuList = <OptionValue extends string | number>(
177+
props: MenuListProps<SelectPickerOptionType<OptionValue>>,
178+
) => {
186179
const {
187180
children,
188-
selectProps: { inputValue, value },
181+
selectProps: { inputValue, value, renderMenuListFooter, shouldRenderCustomOptions, renderCustomOptions },
189182
} = props
190183

191184
return (

src/Shared/Components/SelectPicker/type.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { ComponentSizeType } from '@Shared/constants'
1919
import { MutableRefObject, ReactElement, ReactNode } from 'react'
2020
import { GroupBase, GroupHeadingProps, Props as ReactSelectProps, SelectInstance } from 'react-select'
2121
import { CreatableProps } from 'react-select/creatable'
22+
// This import allows to extend the base interface in react-select module via module augmentation
23+
import type {} from 'react-select/base'
2224

2325
export interface SelectPickerOptionType<OptionValue = string | number> extends OptionType<OptionValue, ReactNode> {
2426
/**
@@ -35,17 +37,41 @@ export interface SelectPickerOptionType<OptionValue = string | number> extends O
3537
endIcon?: ReactElement
3638
}
3739

38-
export enum SelectPickerVariantType {
39-
DEFAULT = 'default',
40-
BORDER_LESS = 'border-less',
41-
}
42-
4340
type SelectProps<OptionValue, IsMulti extends boolean> = ReactSelectProps<
4441
SelectPickerOptionType<OptionValue>,
4542
IsMulti,
4643
GroupBase<SelectPickerOptionType<OptionValue>>
4744
>
4845

46+
declare module 'react-select/base' {
47+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
48+
export interface Props<Option, IsMulti extends boolean, Group extends GroupBase<Option>> {
49+
/**
50+
* Render function for the footer at the bottom of menu list. It is sticky by default
51+
*/
52+
renderMenuListFooter?: (selectedOptions: SelectProps<string | number, IsMulti>['value']) => ReactNode
53+
/**
54+
* If true, custom options are rendered in the menuList component of react select
55+
*
56+
* Note: renderCustomOptions is required to be passed; renderMenuListFooter is also not called
57+
*
58+
* @default false
59+
*/
60+
shouldRenderCustomOptions?: boolean
61+
/**
62+
* Callback handler for custom options
63+
*
64+
* Imp Note: The menu open/close needs to handled by the consumer in this case
65+
*/
66+
renderCustomOptions?: () => ReactElement
67+
}
68+
}
69+
70+
export enum SelectPickerVariantType {
71+
DEFAULT = 'default',
72+
BORDER_LESS = 'border-less',
73+
}
74+
4975
export type SelectPickerProps<OptionValue = number | string, IsMulti extends boolean = false> = Pick<
5076
SelectProps<OptionValue, IsMulti>,
5177
| 'name'
@@ -72,6 +98,9 @@ export type SelectPickerProps<OptionValue = number | string, IsMulti extends boo
7298
| 'onMenuClose'
7399
| 'autoFocus'
74100
| 'onBlur'
101+
| 'renderMenuListFooter'
102+
| 'shouldRenderCustomOptions'
103+
| 'renderCustomOptions'
75104
> &
76105
Required<Pick<SelectProps<OptionValue, IsMulti>, 'inputId'>> & {
77106
/**
@@ -82,10 +111,6 @@ export type SelectPickerProps<OptionValue = number | string, IsMulti extends boo
82111
* Error message for the select
83112
*/
84113
error?: ReactNode
85-
/**
86-
* Render function for the footer at the bottom of menu list. It is sticky by default
87-
*/
88-
renderMenuListFooter?: (selectedOptions: SelectProps<OptionValue, IsMulti>['value']) => ReactNode
89114
/**
90115
* Info text for the select, if any
91116
*/
@@ -195,29 +220,7 @@ export type SelectPickerProps<OptionValue = number | string, IsMulti extends boo
195220
: {
196221
isMulti?: never
197222
multiSelectProps?: never
198-
}) &
199-
(
200-
| {
201-
/**
202-
* If true, custom options are rendered in the menuList component of react select
203-
*
204-
* Note: renderCustomOptions is required to be passed; renderMenuListFooter is also not called
205-
*
206-
* @default false
207-
*/
208-
shouldRenderCustomOptions: boolean
209-
/**
210-
* Callback handler for custom options
211-
*
212-
* Imp Note: The menu open/close needs to handled by the consumer in this case
213-
*/
214-
renderCustomOptions: () => ReactElement
215-
}
216-
| {
217-
shouldRenderCustomOptions?: never
218-
renderCustomOptions?: never
219-
}
220-
)
223+
})
221224

222225
// Doing like this, because of global export error GroupHeadingPropsDefinedProps
223226
export type SelectPickerGroupHeadingProps<OptionValue> = GroupHeadingProps<SelectPickerOptionType<OptionValue>> & {

0 commit comments

Comments
 (0)