Skip to content

Commit 157f509

Browse files
committed
fix: hide the create new option if value already exists
1 parent 71c3162 commit 157f509

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
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": "0.2.2-beta-10",
3+
"version": "0.2.2-beta-12",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
209209
fullWidth = false,
210210
...props
211211
}: SelectPickerProps<OptionValue, IsMulti>) => {
212-
const { inputId, required, isDisabled, controlShouldRenderValue = true, value } = props
212+
const { inputId, required, isDisabled, controlShouldRenderValue = true, value, options } = props
213213
const {
214214
isCreatable = false,
215215
isGroupHeadingSelectable = false,
@@ -240,11 +240,17 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
240240
[error, selectSize, menuSize, variant, isGroupHeadingSelectable, shouldMenuAlignRight],
241241
)
242242

243-
// Used to show the create new option for creatable select
244-
const isValidNewOption = (inputValue: string) =>
245-
isCreatable &&
246-
!!inputValue?.trim() &&
247-
!getSelectPickerOptionByValue(value as SelectPickerOptionType<OptionValue>[], inputValue.trim(), null)
243+
// Used to show the create new option for creatable select and the option(s) doesn't have the input value
244+
const isValidNewOption = (inputValue: string) => {
245+
const trimmedInput = inputValue?.trim()
246+
247+
return (
248+
isCreatable &&
249+
!!trimmedInput &&
250+
!getSelectPickerOptionByValue(value as SelectPickerOptionType<OptionValue>[], trimmedInput, null) &&
251+
!getSelectPickerOptionByValue(options, trimmedInput, null)
252+
)
253+
}
248254

249255
const renderControl = useCallback(
250256
(controlProps: ControlProps<SelectPickerOptionType<OptionValue>>) => (

src/Shared/Components/SelectPicker/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ export const getSelectPickerOptionByValue = <OptionValue>(
341341
value: string | number,
342342
defaultOption: SelectPickerOptionType<OptionValue> = { label: '', value: '' as unknown as OptionValue },
343343
): SelectPickerOptionType<OptionValue> => {
344-
const foundOption = (optionsList ?? []).reduce(
344+
if (!Array.isArray(optionsList)) {
345+
return defaultOption
346+
}
347+
348+
const foundOption = optionsList.reduce(
345349
(acc, curr) => {
346350
if (!acc.notFound) return acc
347351

0 commit comments

Comments
 (0)