-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build(storybook): enforce React strict mode * feat(fields): add MultiSelect
- Loading branch information
1 parent
8a85459
commit e1123b1
Showing
10 changed files
with
140 additions
and
52 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useCallback, useMemo } from 'react' | ||
import { TagPicker } from 'rsuite' | ||
import styled from 'styled-components' | ||
|
||
import type { Option } from '../types' | ||
import type { TagPickerProps } from 'rsuite' | ||
import type { Promisable } from 'type-fest' | ||
|
||
export type MultiSelectProps = Omit<TagPickerProps, 'as' | 'data' | 'defaultValue' | 'onChange' | 'value'> & { | ||
defaultValue?: string[] | ||
name: string | ||
onChange?: (nextValue: string[] | undefined) => Promisable<void> | ||
options: Option[] | ||
} | ||
export function MultiSelect({ | ||
onChange, | ||
options, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
searchable = false, | ||
...originalProps | ||
}: MultiSelectProps) { | ||
const key = useMemo( | ||
() => `${originalProps.name}-${JSON.stringify(originalProps.defaultValue)}`, | ||
[originalProps.defaultValue, originalProps.name] | ||
) | ||
|
||
const handleChange = useCallback( | ||
(nextValue: string[] | null) => { | ||
if (!onChange) { | ||
return | ||
} | ||
|
||
const normalizedNextValue = !nextValue || !nextValue.length ? undefined : nextValue | ||
|
||
onChange(normalizedNextValue) | ||
}, | ||
[onChange] | ||
) | ||
|
||
return <StyledTagPicker key={key} data={options} onChange={handleChange} searchable={searchable} {...originalProps} /> | ||
} | ||
|
||
const StyledTagPicker = styled(TagPicker)` | ||
cursor: pointer; | ||
width: 9.25rem; | ||
> .rs-picker-toggle { | ||
cursor: inherit; | ||
} | ||
` |
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
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useState } from 'react' | ||
|
||
import { MultiSelect } from '../../src' | ||
import { Output } from '../_components/Output' | ||
|
||
import type { MultiSelectProps } from '../../src' | ||
|
||
const args: MultiSelectProps = { | ||
defaultValue: undefined, | ||
placeholder: 'Pick some options', | ||
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' } | ||
] | ||
} | ||
|
||
export default { | ||
title: 'Fields/MultiSelect', | ||
component: MultiSelect, | ||
|
||
argTypes: { | ||
defaultValue: { | ||
control: 'inline-check', | ||
options: ['FIRST_OPTION', 'SECOND_OPTION', 'THIRD_OPTION', 'A_VERY_VERY_LONG_OPTION'] | ||
}, | ||
isMulti: { | ||
control: 'boolean' | ||
} | ||
}, | ||
|
||
args | ||
} | ||
|
||
export function _MultiSelect(props: MultiSelectProps) { | ||
const [outputValue, setOutputValue] = useState<string[] | undefined | '∅'>('∅') | ||
|
||
return ( | ||
<> | ||
<MultiSelect {...props} onChange={setOutputValue} /> | ||
|
||
{outputValue !== '∅' && <Output value={outputValue} />} | ||
</> | ||
) | ||
} |
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