Skip to content

Commit c3ba8dd

Browse files
committed
chore: async select component
1 parent 51d31f3 commit c3ba8dd

File tree

6 files changed

+155
-4
lines changed

6 files changed

+155
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.9.6",
3+
"version": "1.9.6-beta-2",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Components/ConfirmationModal/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export { default as ConfirmationModal, BaseConfirmationModal } from './Confirmat
1818
export { DeleteConfirmationModal } from './DeleteConfirmationModal'
1919
export { ForceDeleteConfirmationModal } from './ForceDeleteConfirmationModal'
2020
export { CannotDeleteModal } from './CannotDeleteModal'
21-
export { ConfirmationModalVariantType, type ConfirmationModalProps } from './types'
21+
export { ConfirmationModalVariantType, type ConfirmationModalProps, type DeleteConfirmationModalProps } from './types'
2222
export { ConfirmationModalProvider } from './ConfirmationModalContext'
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { useCallback, useMemo, useRef } from 'react'
2+
import { SelectInstance, ValueContainerProps } from 'react-select'
3+
import AsyncSelect from 'react-select/async'
4+
import {
5+
SelectPickerClearIndicator,
6+
SelectPickerControl,
7+
SelectPickerDropdownIndicator,
8+
SelectPickerInput,
9+
SelectPickerLoadingIndicator,
10+
SelectPickerMenuList,
11+
SelectPickerMultiValueRemove,
12+
SelectPickerOption,
13+
SelectPickerValueContainer,
14+
} from './common'
15+
import { AsyncSelectProps, SelectPickerOptionType } from './type'
16+
import { FormFieldWrapper } from '../FormFieldWrapper'
17+
import { getCommonSelectStyle } from './utils'
18+
19+
export const AsyncSelectPicker = ({
20+
defaultOptions,
21+
loadOptions,
22+
noOptionsMessage,
23+
onChange,
24+
error,
25+
size,
26+
menuSize,
27+
variant,
28+
getIsOptionValid,
29+
isGroupHeadingSelectable,
30+
shouldMenuAlignRight,
31+
inputId,
32+
layout,
33+
label,
34+
helperText,
35+
warningText,
36+
required,
37+
fullWidth,
38+
ariaLabel,
39+
borderConfig,
40+
borderRadiusConfig,
41+
labelTippyCustomizedConfig,
42+
labelTooltipConfig,
43+
}: AsyncSelectProps) => {
44+
const selectRef = useRef<SelectInstance>(null)
45+
46+
// Option disabled, group null state, checkbox hover, create option visibility (scroll reset on search)
47+
const selectStyles = useMemo(
48+
() =>
49+
getCommonSelectStyle({
50+
error,
51+
size,
52+
menuSize,
53+
variant,
54+
getIsOptionValid,
55+
isGroupHeadingSelectable,
56+
shouldMenuAlignRight,
57+
}),
58+
[error, size, menuSize, variant, isGroupHeadingSelectable, shouldMenuAlignRight],
59+
)
60+
61+
const handleOnKeyDown = (event) => {
62+
if (event.key === 'Escape') {
63+
selectRef.current?.inputRef.blur()
64+
}
65+
}
66+
type OptionValue = string | number
67+
68+
const renderValueContainer = useCallback(
69+
(valueContainerProps: ValueContainerProps<SelectPickerOptionType<OptionValue>>) => (
70+
<SelectPickerValueContainer {...valueContainerProps} />
71+
),
72+
[],
73+
)
74+
75+
return (
76+
<FormFieldWrapper
77+
inputId={inputId}
78+
layout={layout}
79+
label={label}
80+
error={error}
81+
helperText={helperText}
82+
warningText={warningText}
83+
required={required}
84+
fullWidth={fullWidth}
85+
ariaLabel={ariaLabel}
86+
borderConfig={borderConfig}
87+
borderRadiusConfig={borderRadiusConfig}
88+
labelTippyCustomizedConfig={labelTippyCustomizedConfig}
89+
labelTooltipConfig={labelTooltipConfig}
90+
>
91+
<div className="w-100">
92+
<AsyncSelect
93+
ref={selectRef}
94+
blurInputOnSelect
95+
onKeyDown={handleOnKeyDown}
96+
defaultOptions={defaultOptions}
97+
loadOptions={loadOptions}
98+
noOptionsMessage={noOptionsMessage}
99+
onChange={onChange}
100+
components={{
101+
IndicatorSeparator: null,
102+
LoadingIndicator: SelectPickerLoadingIndicator,
103+
DropdownIndicator: SelectPickerDropdownIndicator,
104+
Control: SelectPickerControl,
105+
Option: SelectPickerOption,
106+
MenuList: SelectPickerMenuList,
107+
ClearIndicator: SelectPickerClearIndicator,
108+
ValueContainer: renderValueContainer,
109+
// MultiValueLabel: renderMultiValueLabel,
110+
MultiValueRemove: SelectPickerMultiValueRemove,
111+
// GroupHeading: renderGroupHeading,
112+
// NoOptionsMessage: renderNoOptionsMessage,
113+
Input: SelectPickerInput,
114+
}}
115+
value={defaultOptions?.[0]}
116+
styles={selectStyles}
117+
// aria-describedby={getFormHelperTextElementId('app-selector')}
118+
/>
119+
</div>
120+
</FormFieldWrapper>
121+
)
122+
}

src/Shared/Components/SelectPicker/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export * from './SelectPickerTextArea.component'
1919
export { default as FilterSelectPicker } from './FilterSelectPicker'
2020
export * from './type'
2121
export { getSelectPickerOptionByValue } from './utils'
22+
export * from './AsyncSelectPicker.component'

src/Shared/Components/SelectPicker/type.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { CreatableProps } from 'react-select/creatable'
2424
import type {} from 'react-select/base'
2525
import { TooltipProps } from '@Common/Tooltip/types'
2626
import { ResizableTagTextAreaProps } from '@Common/CustomTagSelector'
27+
import { AsyncProps } from 'react-select/async'
2728
import { FormFieldWrapperProps } from '../FormFieldWrapper/types'
2829

2930
export interface SelectPickerOptionType<OptionValue = string | number> extends OptionType<OptionValue, ReactNode> {
@@ -314,3 +315,30 @@ export type SelectPickerTextAreaProps = Omit<
314315
| 'shouldRenderTextArea'
315316
> &
316317
Pick<ResizableTagTextAreaProps, 'maxHeight' | 'minHeight' | 'refVar' | 'dependentRefs'>
318+
319+
export interface AsyncSelectProps
320+
extends Partial<
321+
Pick<
322+
FormFieldWrapperProps,
323+
| 'error'
324+
| 'layout'
325+
| 'label'
326+
| 'helperText'
327+
| 'warningText'
328+
| 'required'
329+
| 'fullWidth'
330+
| 'borderConfig'
331+
| 'borderRadiusConfig'
332+
| 'labelTippyCustomizedConfig'
333+
| 'labelTooltipConfig'
334+
| 'ariaLabel'
335+
| 'inputId'
336+
>
337+
>,
338+
AsyncProps<SelectPickerOptionType, boolean, GroupBase<SelectPickerOptionType>>,
339+
Partial<Pick<SelectPickerProps, 'size' | 'menuSize' | 'variant' | 'shouldMenuAlignRight'>> {
340+
getIsOptionValid?: (option: SelectPickerOptionType) => boolean
341+
isGroupHeadingSelectable?: boolean
342+
// defaultOptions?: SelectPickerOptionType[]
343+
loadingOptions?: SelectPickerOptionType[]
344+
}

0 commit comments

Comments
 (0)