Skip to content

Commit a788c7e

Browse files
committed
feat: add support for count chip
1 parent 85a149c commit a788c7e

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ReactSelect, { ControlProps, MenuProps } from 'react-select'
1+
import ReactSelect, { ControlProps, MenuProps, ValueContainerProps } from 'react-select'
22
import { ReactElement, useCallback, useMemo } from 'react'
33
import { ReactComponent as ErrorIcon } from '@Icons/ic-warning.svg'
44
import { ReactComponent as ICInfoFilledOverride } from '@Icons/ic-info-filled-override.svg'
@@ -13,6 +13,7 @@ import {
1313
SelectPickerLoadingIndicator,
1414
SelectPickerMenu,
1515
SelectPickerOption,
16+
SelectPickerValueContainer,
1617
} from './common'
1718
import { SelectPickerOptionType, SelectPickerProps } from './type'
1819

@@ -26,6 +27,7 @@ const SelectPicker = ({
2627
showSelectedOptionIcon = true,
2728
size = ComponentSizeType.medium,
2829
disabledTippyContent,
30+
showSelectedOptionsCount = false,
2931
...props
3032
}: SelectPickerProps) => {
3133
const { inputId, required, isDisabled } = props
@@ -53,6 +55,13 @@ const SelectPicker = ({
5355
[],
5456
)
5557

58+
const renderValueContainer = useCallback(
59+
(valueContainerProps: ValueContainerProps<SelectPickerOptionType>) => (
60+
<SelectPickerValueContainer {...valueContainerProps} showSelectedOptionsCount={showSelectedOptionsCount} />
61+
),
62+
[showSelectedOptionsCount],
63+
)
64+
5665
const renderDisabledTippy = (children: ReactElement) => (
5766
<Tippy content={disabledTippyContent} placement="top" className="default-tt" arrow={false}>
5867
{children}
@@ -92,6 +101,7 @@ const SelectPicker = ({
92101
Option: SelectPickerOption,
93102
Menu: renderMenu,
94103
ClearIndicator: SelectPickerClearIndicator,
104+
ValueContainer: renderValueContainer,
95105
}}
96106
styles={selectStyles}
97107
menuPlacement="auto"

src/Shared/Components/SelectPicker/common.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
OptionProps,
66
ClearIndicatorProps,
77
MenuProps,
8+
ValueContainerProps,
89
} from 'react-select'
910
import { Progressing } from '@Common/Progressing'
1011
import { ReactComponent as ICCaretDown } from '@Icons/ic-caret-down.svg'
@@ -51,6 +52,27 @@ export const SelectPickerControl = ({
5152
)
5253
}
5354

55+
export const SelectPickerValueContainer = ({
56+
showSelectedOptionsCount,
57+
...props
58+
}: ValueContainerProps<SelectPickerOptionType> & Pick<SelectPickerProps, 'showSelectedOptionsCount'>) => {
59+
const { getValue } = props
60+
const selectedOptionsLength = (getValue() ?? []).length
61+
62+
return (
63+
<div className="flex left dc__gap-8 flex-grow-1">
64+
<div className="flex left">
65+
<components.ValueContainer {...props} />
66+
</div>
67+
{showSelectedOptionsCount && selectedOptionsLength > 0 && (
68+
<div className="bcb-5 dc__border-radius-4-imp pl-5 pr-5 cn-0 fs-12 fw-6 lh-18 dc__truncate dc__no-shrink">
69+
{selectedOptionsLength}
70+
</div>
71+
)}
72+
</div>
73+
)
74+
}
75+
5476
export const SelectPickerLoadingIndicator = () => <Progressing />
5577

5678
export const SelectPickerOption = (props: OptionProps<SelectPickerOptionType>) => {

src/Shared/Components/SelectPicker/type.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ export interface SelectPickerProps
5252
* Content to be shown in a tippy when disabled
5353
*/
5454
disabledTippyContent?: ReactNode
55+
/**
56+
* If true, the selected options count is shown in a chip inside ValueContainer
57+
*
58+
* @default 'false'
59+
*/
60+
showSelectedOptionsCount?: boolean
5561
}

src/Shared/Components/SelectPicker/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const getCommonSelectStyle = ({ error, size }: Pick<SelectPickerProps, 'e
3333
padding: '5px 8px',
3434
gap: '8px',
3535
opacity: state.isDisabled ? 0.5 : 1,
36+
flexWrap: 'nowrap',
3637

3738
'&:hover': {
3839
borderColor: state.isDisabled ? 'var(--N200)' : 'var(--N300)',
@@ -107,6 +108,7 @@ export const getCommonSelectStyle = ({ error, size }: Pick<SelectPickerProps, 'e
107108
size: '13px',
108109
fontWeight: 400,
109110
lineHeight: '20px',
111+
overflow: 'hidden',
110112
}),
111113
placeholder: (base) => ({
112114
...base,

0 commit comments

Comments
 (0)