Skip to content

Commit

Permalink
fix(fields): add fixedWidth prop to MultiSelect (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele authored Nov 29, 2022
1 parent 586a44c commit bea7a83
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
21 changes: 19 additions & 2 deletions src/fields/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import type { Promisable } from 'type-fest'

export type MultiSelectProps = Omit<TagPickerProps, 'as' | 'data' | 'defaultValue' | 'onChange' | 'value'> & {
defaultValue?: string[]
/** Width in REM */
fixedWidth?: number
name: string
onChange?: (nextValue: string[] | undefined) => Promisable<void>
options: Option[]
}
export function MultiSelect({
fixedWidth = 5,
onChange,
options,
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -37,11 +40,25 @@ export function MultiSelect({
[onChange]
)

return <StyledTagPicker key={key} data={options} onChange={handleChange} searchable={searchable} {...originalProps} />
return (
<StyledTagPicker
key={key}
data={options}
fixedWidth={fixedWidth}
onChange={handleChange}
searchable={searchable}
{...originalProps}
/>
)
}

const StyledTagPicker = styled(TagPicker)`
// TODO A width seems to be mandatory in rsuite which is a very dirty behavior.
// We should hack that.
const StyledTagPicker = styled(TagPicker)<{
fixedWidth: number
}>`
cursor: pointer;
width: ${p => p.fixedWidth}rem;
> .rs-picker-toggle {
cursor: inherit;
Expand Down
8 changes: 3 additions & 5 deletions stories/fields/MultiSelect.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import type { MultiSelectProps } from '../../src'

const args: MultiSelectProps = {
defaultValue: undefined,
placeholder: 'Pick some options',
fixedWidth: 10,
name: 'myMultiSelect',
options: [
{ label: 'First Option', value: 'FIRST_OPTION' },
{ label: 'Second Option', value: 'SECOND_OPTION' },
{ label: 'Third Option', value: 'THIRD_OPTION' },
{ label: 'A Very Very Long Option', value: 'A_VERY_VERY_LONG_OPTION' }
]
],
placeholder: 'Pick some options'
}

export default {
Expand All @@ -25,9 +26,6 @@ export default {
defaultValue: {
control: 'inline-check',
options: ['FIRST_OPTION', 'SECOND_OPTION', 'THIRD_OPTION', 'A_VERY_VERY_LONG_OPTION']
},
isMulti: {
control: 'boolean'
}
},

Expand Down
3 changes: 0 additions & 3 deletions stories/fields/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ export default {
defaultValue: {
control: 'inline-radio',
options: ['FIRST_OPTION', 'SECOND_OPTION', 'THIRD_OPTION', 'A_VERY_VERY_LONG_OPTION']
},
isMulti: {
control: 'boolean'
}
},

Expand Down
5 changes: 3 additions & 2 deletions stories/formiks/FormikMultiSelect.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { noop } from '../_utils/noop'
import type { FormikMultiSelectProps } from '../../src'

const args: FormikMultiSelectProps = {
placeholder: 'Pick some options',
fixedWidth: 10,
name: 'myMultiSelect',
options: [
{ label: 'First Option', value: 'FIRST_OPTION' },
{ label: 'Second Option', value: 'SECOND_OPTION' },
{ label: 'Third Option', value: 'THIRD_OPTION' },
{ label: 'A Very Very Long Option', value: 'A_VERY_VERY_LONG_OPTION' }
]
],
placeholder: 'Pick some options'
}

export default {
Expand Down

0 comments on commit bea7a83

Please sign in to comment.