Skip to content

Commit

Permalink
misc(multiple-combobox): add free solo mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ansmonjol committed Mar 4, 2024
1 parent e7b658e commit 35f4474
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
38 changes: 22 additions & 16 deletions src/components/form/MultipleComboBox/MultipleComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const MultipleComboBox = ({
freeSolo,
emptyText,
disableClearable = false,
forcePopupIcon = false,
hideTags = false,
renderGroupHeader,
virtualized = true,
limitTags,
Expand All @@ -55,13 +57,12 @@ export const MultipleComboBox = ({
<Container>
<Autocomplete
multiple
forcePopupIcon
forcePopupIcon={forcePopupIcon}
disableCloseOnSelect
disableClearable={disableClearable}
disabled={disabled}
limitTags={limitTags || DEFAULT_LIMIT_TAGS}
options={data}
sx={{ width: '100%' }}
renderInput={(params) => (
<TextInput
{...params}
Expand All @@ -76,22 +77,26 @@ export const MultipleComboBox = ({
)}
onChange={(_, newValue) => {
if (freeSolo) {
// On free solo mode, turn typed values into objects
onChange(
newValue.map((val) => {
if (typeof val === 'string') {
val = { value: val }
}
return val
}),
)
// On free solo mode, turn string typed values into objects
const formated = newValue.map((val) => {
if (typeof val === 'string') {
val = { value: val }
}
return val
})

onChange(formated)
} else {
onChange(newValue)
}
}}
value={value || []}
renderTags={(tagValues, getTagProps) =>
tagValues.map((option, index) => {
value={value || undefined}
renderTags={(tagValues, getTagProps) => {
if (hideTags) {
return null
}

return tagValues.map((option, index) => {
const tagOptions = getTagProps({ index })

return (
Expand All @@ -105,7 +110,7 @@ export const MultipleComboBox = ({
/>
)
})
}
}}
clearIcon={<Icon name="close-circle-filled" />}
popupIcon={<Icon name="chevron-up-down" />}
noOptionsText={emptyText ?? translate('text_623b3acb8ee4e000ba87d082')}
Expand Down Expand Up @@ -138,8 +143,9 @@ export const MultipleComboBox = ({

if (inputValue !== '' && !isExisting && freeSolo) {
filtered.push({
customValue: true,
value: inputValue,
label: `Add "${inputValue}"`,
label: `TODO: Click or press enter to create "${inputValue}"`,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ const StyledPopper = styled(Popper)<{
max-width: ${({ $minWidth }) => ($minWidth ? `${$minWidth}px` : 'initial')};
}
.MuiAutocomplete-paper {
border: 1px solid ${theme.palette.grey[200]};
padding: ${theme.spacing(2)} 0;
}
> *.multipleComboBox-popper--grouped .MuiAutocomplete-paper {
padding: ${theme.spacing(2)} 0;
.MuiAutocomplete-option {
margin: 0;
}
`
4 changes: 3 additions & 1 deletion src/components/form/MultipleComboBox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ interface BasicMultipleComboBoxProps
disabled?: boolean
freeSolo?: boolean
value?: MultipleComboBoxData[]
data: BasicMultipleComboBoxData[]
data?: BasicMultipleComboBoxData[]
sortValues?: boolean
forcePopupIcon?: boolean
hideTags?: boolean
emptyText?: string
virtualized?: boolean
limitTags?: number
Expand Down
41 changes: 39 additions & 2 deletions src/pages/__devOnly/DesignSystem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputAdornment } from '@mui/material'
import { InputAdornment, Stack } from '@mui/material'
import { useFormik } from 'formik'
import { useRef } from 'react'
import { generatePath } from 'react-router-dom'
Expand Down Expand Up @@ -39,7 +39,7 @@ import {
TextInputField,
} from '~/components/form'
import { AmountInputField } from '~/components/form/AmountInput'
import { MultipleComboBoxField } from '~/components/form/MultipleComboBox'
import { MultipleComboBox, MultipleComboBoxField } from '~/components/form/MultipleComboBox'
import { addToast, TToast } from '~/core/apolloClient'
import { ONLY_DEV_DESIGN_SYSTEM_ROUTE, ONLY_DEV_DESIGN_SYSTEM_TAB_ROUTE } from '~/core/router'
import { CurrencyEnum } from '~/generated/graphql'
Expand Down Expand Up @@ -102,6 +102,7 @@ const DesignSystem = () => {
inputNumber: undefined,
switch: true,
combobox: undefined,
multipleCombobox: [],
radio: false,
buttonSelector: undefined,
buttonSelector2: 'time',
Expand Down Expand Up @@ -1114,6 +1115,42 @@ const DesignSystem = () => {
placeholder="Placeholder"
formikProps={formikProps}
/>
<MultipleComboBoxField
freeSolo
name="multipleCombobox"
label="Multiple Free Solo No Data"
placeholder="Placeholder"
formikProps={formikProps}
/>
<Stack gap={1} direction="row" flexWrap="wrap">
{formikProps.values.multipleCombobox.map(
(value: { value: string }, index) => (
<Chip
key={index}
label={value.value}
onDelete={() => {
const newValues = formikProps.values.multipleCombobox.filter(
(v) => v !== value,
)

formikProps.setFieldValue('multipleCombobox', newValues)
}}
/>
),
)}
</Stack>
<MultipleComboBox
freeSolo
hideTags
disableClearable
data={[]}
onChange={(newValue) =>
formikProps.setFieldValue('multipleCombobox', newValue)
}
value={formikProps.values.multipleCombobox}
label="Multiple Free Solo No Data No Tags"
placeholder="Placeholder"
/>
</Block>

<GroupTitle variant="subhead">Radio</GroupTitle>
Expand Down

0 comments on commit 35f4474

Please sign in to comment.