Skip to content

Commit c69641a

Browse files
committed
feat: add support for rendering custom options
1 parent 868327e commit c69641a

File tree

4 files changed

+45
-281
lines changed

4 files changed

+45
-281
lines changed

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

Lines changed: 0 additions & 275 deletions
This file was deleted.

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ const SelectPicker = ({
127127
isMulti,
128128
name,
129129
classNamePrefix,
130+
renderCustomOptions,
131+
shouldRenderCustomOptions = false,
132+
isSearchable,
130133
...props
131134
}: SelectPickerProps) => {
132135
const { inputId, required, isDisabled, controlShouldRenderValue } = props
@@ -135,6 +138,7 @@ const SelectPicker = ({
135138
// Only large variant is supported for multi select picker
136139
const selectSize = isMulti && !controlShouldRenderValue ? ComponentSizeType.large : size
137140
const shouldShowSelectedOptionIcon = !isMulti && showSelectedOptionIcon
141+
const isSelectSearchable = !shouldRenderCustomOptions && isSearchable
138142

139143
const labelId = `${inputId}-label`
140144
const errorElementId = `${inputId}-error-msg`
@@ -163,9 +167,14 @@ const SelectPicker = ({
163167

164168
const renderMenuList = useCallback(
165169
(menuProps: MenuListProps<SelectPickerOptionType>) => (
166-
<SelectPickerMenuList {...menuProps} renderMenuListFooter={renderMenuListFooter} />
170+
<SelectPickerMenuList
171+
{...menuProps}
172+
renderMenuListFooter={renderMenuListFooter}
173+
renderCustomOptions={renderCustomOptions}
174+
shouldRenderCustomOptions={shouldRenderCustomOptions}
175+
/>
167176
),
168-
[],
177+
[shouldRenderCustomOptions],
169178
)
170179

171180
const renderValueContainer = useCallback(
@@ -228,6 +237,7 @@ const SelectPicker = ({
228237
isMulti
229238
name={name || inputId}
230239
classNamePrefix={classNamePrefix || inputId}
240+
isSearchable={isSelectSearchable}
231241
placeholder={placeholder}
232242
components={{
233243
IndicatorSeparator: null,
@@ -260,6 +270,7 @@ const SelectPicker = ({
260270
{...props}
261271
name={name || inputId}
262272
classNamePrefix={classNamePrefix || inputId}
273+
isSearchable={isSelectSearchable}
263274
placeholder={placeholder}
264275
components={{
265276
IndicatorSeparator: null,

src/Shared/Components/SelectPicker/common.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,18 @@ export const SelectPickerOption = ({
144144

145145
export const SelectPickerMenuList = ({
146146
renderMenuListFooter,
147+
shouldRenderCustomOptions,
148+
renderCustomOptions,
147149
...props
148-
}: MenuListProps<SelectPickerOptionType> & Pick<SelectPickerProps, 'renderMenuListFooter'>) => {
150+
}: MenuListProps<SelectPickerOptionType> &
151+
Pick<SelectPickerProps, 'renderMenuListFooter' | 'shouldRenderCustomOptions' | 'renderCustomOptions'>) => {
149152
const { children } = props
150153

151154
return (
152155
<components.MenuList {...props}>
153-
<div className="py-4 cursor">{children}</div>
156+
<div className="py-4 cursor">{shouldRenderCustomOptions ? renderCustomOptions() : children}</div>
154157
{/* Added to the bottom of menu list to prevent from hiding when the menu is opened close to the bottom of the screen */}
155-
{renderMenuListFooter && (
158+
{!shouldRenderCustomOptions && renderMenuListFooter && (
156159
<div className=" dc__position-sticky dc__bottom-0 dc__bottom-radius-4 bcn-0">
157160
{renderMenuListFooter()}
158161
</div>

src/Shared/Components/SelectPicker/type.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export type SelectPickerProps = Pick<
4646
| 'getOptionLabel'
4747
| 'getOptionValue'
4848
| 'isOptionSelected'
49+
| 'menuIsOpen'
50+
| 'onMenuOpen'
51+
| 'onMenuClose'
4952
> &
5053
Required<Pick<SelectProps, 'inputId'>> & {
5154
/**
@@ -111,7 +114,7 @@ export type SelectPickerProps = Pick<
111114
* @default false
112115
*/
113116
disableDescriptionEllipsis?: boolean
114-
// TODO: Improve typing and add support for custom option list; isMenuOpen prop expose?
117+
// TODO: ref support?
115118
} & (
116119
| {
117120
isMulti?: never
@@ -141,4 +144,26 @@ export type SelectPickerProps = Pick<
141144
getIsOptionValid?: (option: SelectPickerOptionType) => boolean
142145
}
143146
}
147+
) &
148+
(
149+
| {
150+
/**
151+
* If true, custom options are rendered in the menuList component of react select
152+
*
153+
* Note: renderCustomOptions is required to be passed; renderMenuListFooter is also not called
154+
*
155+
* @default false
156+
*/
157+
shouldRenderCustomOptions: boolean
158+
/**
159+
* Callback handler for custom options
160+
*
161+
* Imp Note: The menu open/close needs to handled by the consumer in this case
162+
*/
163+
renderCustomOptions: () => ReactElement
164+
}
165+
| {
166+
shouldRenderCustomOptions?: never
167+
renderCustomOptions?: never
168+
}
144169
)

0 commit comments

Comments
 (0)