-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from ynput/develop
Release: EnumDropdown
- Loading branch information
Showing
9 changed files
with
275 additions
and
16 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,124 @@ | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
import { EnumDropdown, EnumDropdownProps } from '.' | ||
import { useState } from 'react' | ||
|
||
const meta: Meta<typeof EnumDropdown> = { | ||
component: EnumDropdown, | ||
tags: ['autodocs'], | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof EnumDropdown> | ||
|
||
const Template = ({ value: initValue, ...props }: EnumDropdownProps) => { | ||
const [value, setValue] = useState(initValue) | ||
|
||
return <EnumDropdown value={value} {...props} onChange={(value) => setValue(value)} /> | ||
} | ||
|
||
const priorities: EnumDropdownProps['options'] = [ | ||
{ | ||
value: 'low', | ||
label: 'Low', | ||
icon: 'keyboard_double_arrow_down', | ||
color: '#9FA7B1', | ||
}, | ||
{ | ||
value: 'normal', | ||
label: 'Normal', | ||
color: '#9AC0E7', | ||
icon: 'check_indeterminate_small', | ||
}, | ||
{ | ||
value: 'high', | ||
label: 'High', | ||
color: '#FFAD66', | ||
icon: 'keyboard_arrow_up', | ||
}, | ||
{ | ||
value: 'urgent', | ||
label: 'Urgent', | ||
color: '#FF8585', | ||
icon: 'keyboard_double_arrow_up', | ||
}, | ||
] | ||
|
||
const initArgs: Story['args'] = { | ||
onClear: undefined, | ||
onClearNull: undefined, | ||
} | ||
|
||
const priorityValue = 'low' | ||
|
||
export const Priority: Story = { | ||
args: { | ||
...initArgs, | ||
value: [priorityValue], | ||
options: priorities, | ||
}, | ||
render: Template, | ||
} | ||
export const InverseColors: Story = { | ||
args: { | ||
...initArgs, | ||
value: [priorityValue], | ||
options: priorities, | ||
colorInverse: true, | ||
}, | ||
render: Template, | ||
} | ||
export const OnlyColors: Story = { | ||
args: { | ||
...initArgs, | ||
value: [priorityValue], | ||
options: priorities.map(({ color, value, label }) => ({ color, value, label })), | ||
}, | ||
render: Template, | ||
} | ||
export const OnlyIcons: Story = { | ||
args: { | ||
...initArgs, | ||
value: [priorityValue], | ||
options: priorities.map(({ icon, value, label }) => ({ icon, value, label })), | ||
}, | ||
render: Template, | ||
} | ||
export const OnlyLabels: Story = { | ||
args: { | ||
...initArgs, | ||
value: [priorityValue], | ||
options: priorities.map(({ value, label }) => ({ value, label })), | ||
}, | ||
render: Template, | ||
} | ||
export const MixedIconsAndColors: Story = { | ||
args: { | ||
...initArgs, | ||
value: [priorityValue], | ||
// some options have icons, some have colors, some have both, some have neither | ||
options: [ | ||
{ | ||
value: 'low', | ||
label: 'Low', | ||
icon: 'keyboard_double_arrow_down', | ||
color: '#9FA7B1', | ||
}, | ||
{ | ||
value: 'normal', | ||
label: 'Normal', | ||
color: '#9AC0E7', | ||
}, | ||
{ | ||
value: 'high', | ||
label: 'High', | ||
icon: 'keyboard_arrow_up', | ||
}, | ||
{ | ||
value: 'urgent', | ||
label: 'Urgent', | ||
}, | ||
], | ||
}, | ||
render: Template, | ||
} |
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,68 @@ | ||
import styled, { css } from 'styled-components' | ||
import { DefaultValueTemplate } from '../Dropdown' | ||
|
||
interface IconStyledProps { | ||
$color?: string | ||
} | ||
|
||
const getSelectedBg = ({ $color }: IconStyledProps) => { | ||
if ($color) | ||
return css` | ||
background: ${$color}; | ||
&:hover { | ||
filter: brightness(1.1); | ||
} | ||
.value-label, | ||
.icon { | ||
color: var(--md-sys-color-inverse-on-surface); | ||
} | ||
` | ||
else | ||
return css` | ||
background: var(--md-sys-color-primary-container); | ||
&:hover { | ||
background: var(--md-sys-color-primary-container-hover); | ||
} | ||
.value-label, | ||
.icon { | ||
color: var(--md-sys-color-on-primary-container); | ||
} | ||
` | ||
} | ||
|
||
export const StyledDefaultValueTemplate = styled(DefaultValueTemplate)<IconStyledProps>` | ||
padding-left: 0; | ||
&.inverse { | ||
${({ $color }) => getSelectedBg({ $color })} | ||
border-color: ${({ $color }) => $color && 'transparent'}; | ||
} | ||
` | ||
|
||
export const Option = styled.div<IconStyledProps>` | ||
display: flex; | ||
align-items: center; | ||
/* justify-content: center; */ | ||
gap: 8px; | ||
padding-left: 0.5rem; | ||
height: 32px; | ||
span:last-child { | ||
overflow: hidden; | ||
white-space: nowrap; | ||
text-overflow: ellipsis; | ||
} | ||
width: 100%; | ||
.value-label, | ||
.icon { | ||
color: ${({ $color }) => ($color ? $color : `var(--md-sys-color-on-surface)`)}; | ||
} | ||
&.selected { | ||
${({ $color }) => getSelectedBg({ $color })} | ||
} | ||
` |
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,60 @@ | ||
import { forwardRef, useMemo } from 'react' | ||
import * as Styled from './EnumDropdown.styled' | ||
import { DefaultValueTemplate, Dropdown, DropdownProps, DropdownRef } from '../Dropdown' | ||
import { Icon, IconType } from '../../Icon' | ||
import clsx from 'clsx' | ||
|
||
export interface EnumTemplateProps { | ||
option: EnumDropdownOption | null | undefined | ||
isSelected?: boolean | ||
} | ||
|
||
const EnumTemplate = ({ option, isSelected }: EnumTemplateProps) => { | ||
const { value, label, icon, color } = option || {} | ||
return ( | ||
<Styled.Option className={clsx({ selected: isSelected })} id={value} $color={color}> | ||
{icon && <Icon icon={icon} />} | ||
<span className="value-label">{label}</span> | ||
</Styled.Option> | ||
) | ||
} | ||
|
||
export type EnumDropdownOption = { | ||
value: string | ||
label: string | ||
icon?: IconType | ||
color?: string | ||
} | ||
|
||
export interface EnumDropdownProps | ||
extends Omit<DropdownProps, 'options' | 'valueTemplate' | 'itemTemplate' | 'ref'> { | ||
options: EnumDropdownOption[] | ||
colorInverse?: boolean | ||
} | ||
|
||
export const EnumDropdown = forwardRef<DropdownRef, EnumDropdownProps>( | ||
({ colorInverse, ...props }, ref) => { | ||
return ( | ||
<Dropdown | ||
ref={ref} | ||
valueTemplate={(v, s, o) => { | ||
const option = props.options.find((op) => op.value === v[0]) | ||
return ( | ||
<Styled.StyledDefaultValueTemplate | ||
isOpen={o} | ||
{...props} | ||
$color={option?.color} | ||
className={clsx({ inverse: colorInverse })} | ||
> | ||
<EnumTemplate option={option} /> | ||
</Styled.StyledDefaultValueTemplate> | ||
) | ||
}} | ||
itemTemplate={(option, isSelected) => ( | ||
<EnumTemplate option={option} isSelected={isSelected} /> | ||
)} | ||
{...props} | ||
/> | ||
) | ||
}, | ||
) |
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 @@ | ||
export * from './EnumDropdown' |
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