-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3161cfc
commit 2dfb42a
Showing
4 changed files
with
131 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,83 @@ | ||
import { Tooltip, useMediaQuery, Select } from '@neo4j-ndl/react'; | ||
import { OptionType, ReusableDropdownProps } from '../types'; | ||
import { memo, useMemo } from 'react'; | ||
import { capitalize, capitalizeWithUnderscore } from '../utils/Utils'; | ||
import { prodllms } from '../utils/Constants'; | ||
const DropdownComponent: React.FC<ReusableDropdownProps> = ({ | ||
import { useMediaQuery, Select, Tooltip } from '@neo4j-ndl/react'; // Added Tooltip import | ||
import { memo } from 'react'; | ||
import { capitalizeWithUnderscore } from '../utils/Utils'; | ||
import { DropdownComponentProps, OptionType } from '../types'; | ||
|
||
const DropdownComponent: React.FC<DropdownComponentProps> = ({ | ||
options, | ||
placeholder, | ||
defaultValue, | ||
onSelect, | ||
onChange, | ||
children, | ||
view, | ||
isDisabled, | ||
value, | ||
label, | ||
helpText, | ||
size | ||
size, | ||
mapOptions, | ||
customTooltip, | ||
}) => { | ||
const isProdEnv = process.env.VITE_ENV === 'PROD'; | ||
const isLargeDesktop = useMediaQuery(`(min-width:1440px )`); | ||
const handleChange = (selectedOption: OptionType | null | void) => { | ||
onSelect(selectedOption); | ||
if (view === 'ContentView') { | ||
const existingModel = localStorage.getItem('selectedModel'); | ||
if (existingModel != selectedOption?.value) { | ||
localStorage.setItem('selectedModel', selectedOption?.value ?? ''); | ||
} | ||
} | ||
}; | ||
const allOptions = useMemo(() => options, [options]); | ||
const isLargeDesktop = useMediaQuery('(min-width:1440px)'); | ||
const dropdownOptions = options.map((option) => { | ||
const mappedOption = mapOptions ? mapOptions(option) : option; | ||
const labelText = | ||
typeof mappedOption.label === 'string' | ||
? capitalizeWithUnderscore(mappedOption.label) | ||
: mappedOption.label; | ||
const tooltipContent = customTooltip?.(mappedOption); | ||
return { | ||
label: tooltipContent ? ( | ||
<Tooltip type="simple" placement={isLargeDesktop ? 'left' : 'right'}> | ||
<Tooltip.Trigger> | ||
<span>{labelText}</span> | ||
</Tooltip.Trigger> | ||
<Tooltip.Content>{tooltipContent}</Tooltip.Content> | ||
</Tooltip> | ||
) : ( | ||
<span>{labelText}</span> | ||
), | ||
value: mappedOption.value, | ||
isDisabled: mappedOption.isDisabled || false, | ||
}; | ||
}); | ||
return ( | ||
<> | ||
<div className={view === 'ContentView' ? 'w-[150px]' : ''}> | ||
<Select | ||
type='select' | ||
label={label} | ||
helpText={<div className='!w-max'>{helpText}</div>} | ||
selectProps={{ | ||
onChange: handleChange, | ||
// @ts-ignore | ||
options: allOptions?.map((option) => { | ||
const label = typeof option === 'string' ? capitalizeWithUnderscore(option) : capitalize(option.label); | ||
const value = typeof option === 'string' ? option : option.value; | ||
const isModelSupported = !isProdEnv || prodllms?.includes(value); | ||
return { | ||
label: !isModelSupported && view === 'ContentView' ? ( | ||
<Tooltip type='simple' placement={isLargeDesktop ? 'left' : 'right'}> | ||
<Tooltip.Trigger> | ||
<span>{label}</span> | ||
</Tooltip.Trigger> | ||
<Tooltip.Content>Available In Development Version</Tooltip.Content> | ||
</Tooltip> | ||
) : ( | ||
<span>{label}</span> | ||
), | ||
value, | ||
isDisabled: view === 'ContentView' ? !isModelSupported : isDisabled | ||
}; | ||
}), | ||
placeholder: placeholder || 'Select an option', | ||
defaultValue: defaultValue | ||
? { label: capitalizeWithUnderscore(defaultValue), value: defaultValue } | ||
: undefined, | ||
menuPlacement: 'auto', | ||
isDisabled: isDisabled, | ||
value: value, | ||
}} | ||
size={size} | ||
isFluid | ||
htmlAttributes={{ | ||
'aria-label': 'A selection dropdown', | ||
}} | ||
/> | ||
{children} | ||
</div> | ||
</> | ||
<div className={view === 'ContentView' ? 'w-[150px]' : ''}> | ||
<Select | ||
type='select' | ||
label={label} | ||
helpText={<div className='!w-max'>{helpText}</div>} | ||
selectProps={{ | ||
onChange: (selectedOption) => { | ||
if (selectedOption && typeof selectedOption === 'object' && 'value' in selectedOption) { | ||
onChange(selectedOption as OptionType); | ||
} else { | ||
onChange(null); | ||
} | ||
}, | ||
options: dropdownOptions, | ||
placeholder: placeholder || 'Select an option', | ||
defaultValue: defaultValue | ||
? { | ||
label: | ||
typeof defaultValue.label === 'string' | ||
? capitalizeWithUnderscore(defaultValue.label) | ||
: String(defaultValue.label), | ||
value: defaultValue.value, | ||
} | ||
: undefined, | ||
menuPlacement: 'auto', | ||
isDisabled, | ||
value, | ||
}} | ||
size={size} | ||
isFluid | ||
htmlAttributes={{ | ||
'aria-label': 'A selection dropdown', | ||
}} | ||
/> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
export default memo(DropdownComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters