-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add categories and dividers for dropdowns (#2398)
- Loading branch information
1 parent
db40fdb
commit 2521b32
Showing
6 changed files
with
156 additions
and
59 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
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 |
---|---|---|
@@ -1,56 +1,109 @@ | ||
import { Select } from '@chakra-ui/react'; | ||
import { ChangeEvent, memo, useEffect } from 'react'; | ||
import { DropDownInput, InputSchemaValue } from '../../../../common/common-types'; | ||
import { DropDownInput, DropdownGroup, InputSchemaValue } from '../../../../common/common-types'; | ||
|
||
export interface DropDownProps { | ||
value: InputSchemaValue | undefined; | ||
onChange: (value: InputSchemaValue) => void; | ||
reset: () => void; | ||
isDisabled?: boolean; | ||
options: DropDownInput['options']; | ||
groups?: readonly DropdownGroup[]; | ||
} | ||
|
||
export const DropDown = memo(({ value, onChange, reset, isDisabled, options }: DropDownProps) => { | ||
// reset invalid values to default | ||
useEffect(() => { | ||
if (value === undefined || options.every((o) => o.value !== value)) { | ||
reset(); | ||
} | ||
}, [value, reset, options]); | ||
|
||
let selection = options.findIndex((o) => o.value === value); | ||
if (selection === -1) selection = 0; | ||
|
||
const handleChange = (event: ChangeEvent<HTMLSelectElement>) => { | ||
const selectedIndex = Number(event.target.value); | ||
const selectedValue = options[selectedIndex]?.value as InputSchemaValue | undefined; | ||
if (selectedValue === undefined) { | ||
reset(); | ||
} else { | ||
onChange(selectedValue); | ||
} | ||
}; | ||
|
||
return ( | ||
<Select | ||
borderRadius="lg" | ||
className="nodrag" | ||
disabled={isDisabled} | ||
draggable={false} | ||
size="sm" | ||
style={{ contain: 'size' }} | ||
value={selection} | ||
onChange={handleChange} | ||
> | ||
{options.map(({ option }, index) => ( | ||
export const DropDown = memo( | ||
({ value, onChange, reset, isDisabled, options, groups }: DropDownProps) => { | ||
// reset invalid values to default | ||
useEffect(() => { | ||
if (value === undefined || options.every((o) => o.value !== value)) { | ||
reset(); | ||
} | ||
}, [value, reset, options]); | ||
|
||
let selection = options.findIndex((o) => o.value === value); | ||
if (selection === -1) selection = 0; | ||
|
||
const handleChange = (event: ChangeEvent<HTMLSelectElement>) => { | ||
const selectedIndex = Number(event.target.value); | ||
const selectedValue = options[selectedIndex]?.value as InputSchemaValue | undefined; | ||
if (selectedValue === undefined) { | ||
reset(); | ||
} else { | ||
onChange(selectedValue); | ||
} | ||
}; | ||
|
||
const optionElements: JSX.Element[] = []; | ||
// eslint-disable-next-line @typescript-eslint/no-shadow | ||
for (const [o, index] of options.map((o, i) => [o, i] as const)) { | ||
const group = groups?.find((c) => c.startAt === o.value); | ||
if (group) { | ||
const pseudoValue = `--category-${index}-${group.label ?? ''}`; | ||
|
||
if (group.label) { | ||
optionElements.push( | ||
<option | ||
disabled | ||
key={optionElements.length} | ||
style={{ fontSize: '30%' }} | ||
value={`--pad-${pseudoValue}`} | ||
> | ||
{' '} | ||
</option>, | ||
<option | ||
disabled | ||
key={optionElements.length + 1} | ||
style={{ | ||
fontSize: '110%', | ||
fontWeight: 'bold', | ||
textAlign: 'center', | ||
}} | ||
value={pseudoValue} | ||
> | ||
{group.label} | ||
</option> | ||
); | ||
} else { | ||
optionElements.push( | ||
<option | ||
disabled | ||
key={optionElements.length} | ||
style={{ | ||
fontSize: '85%', | ||
color: 'rgba(128 128 128 / 70%)', | ||
}} | ||
value={`--hr-${pseudoValue}`} | ||
> | ||
{'\u23AF'.repeat(30)} | ||
</option> | ||
); | ||
} | ||
} | ||
|
||
optionElements.push( | ||
<option | ||
key={option} | ||
key={optionElements.length} | ||
style={{ fontSize: '120%' }} | ||
value={index} | ||
> | ||
{option} | ||
{o.option} | ||
</option> | ||
))} | ||
</Select> | ||
); | ||
}); | ||
); | ||
} | ||
|
||
return ( | ||
<Select | ||
borderRadius="lg" | ||
className="nodrag" | ||
disabled={isDisabled} | ||
draggable={false} | ||
size="sm" | ||
style={{ contain: 'size' }} | ||
value={selection} | ||
onChange={handleChange} | ||
> | ||
{optionElements} | ||
</Select> | ||
); | ||
} | ||
); |