-
Notifications
You must be signed in to change notification settings - Fork 25
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 #5 from functional-ui/option-group
Option Group
- Loading branch information
Showing
8 changed files
with
192 additions
and
0 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,5 @@ | ||
{ | ||
"main": "../dist/fui-option-group/index.js", | ||
"module": "../dist/fui-option-group/index.mjs", | ||
"types": "../dist/fui-option-group/index.d.ts" | ||
} |
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,43 @@ | ||
.fui-option-group .fui-option-group-menu-option::after { | ||
border-radius: var(--fui-radius-sm); | ||
width: calc(100% + var(--fui-space-lg)); | ||
left: 50%; | ||
transform: translateX(-50%); | ||
} | ||
|
||
.fui-option-group { | ||
border-radius: var(--fui-radius-lg); | ||
border: 1px solid var(--fui-color-divider-soft); | ||
display: flex; | ||
padding: var(--fui-space-lg) var(--fui-space-xlg); | ||
flex-direction: column; | ||
align-items: flex-start; | ||
background: var(--fui-color-background-base); | ||
} | ||
|
||
.fui-option-group-menu-option { | ||
display: flex; | ||
height: 38px; | ||
gap: var(--fui-space-sm); | ||
align-items: center; | ||
align-self: stretch; | ||
font: var(--running-small-bold); | ||
position: relative; | ||
} | ||
|
||
.fui-option-group-menu-group { | ||
display: flex; | ||
flex-direction: column; | ||
min-height: 38px; | ||
align-self: stretch; | ||
margin-bottom: var(--fui-space-md); | ||
} | ||
|
||
.fui-option-group-menu-group:last-child { | ||
margin-bottom: 0; | ||
} | ||
|
||
.fui-option-group-menu-group-label { | ||
font: var(--running-small-bold); | ||
color: var(--fui-color-foreground-softest); | ||
} |
64 changes: 64 additions & 0 deletions
64
src/components/fui-option-group/fui-option-group.stories.tsx
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,64 @@ | ||
import React from 'react'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import FuiIconPlaceholder16X16 from '../../icons/fui-icon-placeholder-16x16'; | ||
import { FuiOptionGroup } from './fui-option-group'; | ||
|
||
const meta = { | ||
title: ' Components/OptionGroup', | ||
component: FuiOptionGroup, | ||
tags: ['autodocs'], | ||
parameters: { | ||
design: { | ||
type: 'figma', | ||
url: 'https://www.figma.com/file/zHutj6e9DcPngHZTDtAL1u/Functional-UI-Kit?type=design&node-id=2574-19579&mode=design&t=jq0JgMhh6dwhuYIm-4' | ||
} | ||
}, | ||
argTypes: { | ||
options: { | ||
name: '🔗 options', | ||
control: { | ||
disable: true | ||
} | ||
}, | ||
className: { | ||
control: { | ||
disable: true | ||
} | ||
}, | ||
} | ||
} satisfies Meta<typeof FuiOptionGroup>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
name: 'Basic', | ||
args: { | ||
options: [ | ||
{ | ||
label: 'Group Title', options: [ | ||
{ label: 'Option', value: '1' }, | ||
{ label: 'Option', value: '2' }, | ||
{ label: 'Option', value: '3' }, | ||
] | ||
} | ||
] | ||
} | ||
}; | ||
|
||
export const CustomOptions: Story = { | ||
parameters: { | ||
docs: { | ||
description: { | ||
story: 'You can customize your options by passing in a `prefix` (on single select) or `suffix` (on single or multi select).' | ||
} | ||
} | ||
}, | ||
args: { | ||
options: [ | ||
{ label: 'Option 1', value: '1', prefix: <FuiIconPlaceholder16X16 />, suffix: <FuiIconPlaceholder16X16 /> }, | ||
{ label: 'Option 2', value: '2', prefix: <FuiIconPlaceholder16X16 />, suffix: <FuiIconPlaceholder16X16 /> }, | ||
{ label: 'Option 3', value: '3', prefix: <FuiIconPlaceholder16X16 />, suffix: <FuiIconPlaceholder16X16 /> } | ||
] | ||
} | ||
}; |
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,72 @@ | ||
import React from 'react'; | ||
import { prefix } from '../prefix'; | ||
import classNames from 'classnames'; | ||
|
||
const compPrefix = `${prefix}-option-group`; | ||
|
||
export interface Option { | ||
label: string | ||
value: string | ||
prefix?: JSX.Element | ||
suffix?: JSX.Element | ||
} | ||
|
||
export interface OptionGroup { | ||
label: string | ||
options: Option[] | ||
} | ||
|
||
export interface FuiOptionGroupProps { | ||
options: (Option | OptionGroup)[] | ||
className?: string | ||
onSelect?: (value: string) => void | ||
} | ||
|
||
const isOptionGroup = (option: Option | OptionGroup): option is OptionGroup => { | ||
return (option as OptionGroup).options !== undefined; | ||
}; | ||
|
||
export const FuiOptionGroup = ({ | ||
options, | ||
className, | ||
onSelect, | ||
}: FuiOptionGroupProps) => { | ||
const classnames = classNames(compPrefix, className); | ||
|
||
const renderMenuOption = (option: Option, index: number) => { | ||
const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => { | ||
if (e.key === ' ') { | ||
e.preventDefault(); | ||
onSelect?.(option.value); | ||
} | ||
}; | ||
|
||
return ( | ||
<div tabIndex={0} onKeyDown={onKeyDown} key={index} className={`${compPrefix}-menu-option ${prefix}-interactable`} onClick={() => { onSelect?.(option.value); }}> | ||
{option.prefix} | ||
<div className={`${compPrefix}-menu-option-label`}>{option.label}</div> | ||
{option.suffix} | ||
</div> | ||
); | ||
}; | ||
|
||
const renderOptions = (options: (Option | OptionGroup)[]) => { | ||
return options.map((option, index) => { | ||
if (isOptionGroup(option)) { | ||
return ( | ||
<div key={index} className={`${compPrefix}-menu-group`}> | ||
<div className={`${compPrefix}-menu-group-label`}>{option.label}</div> | ||
{option.options.map(renderMenuOption)} | ||
</div> | ||
); | ||
} | ||
return renderMenuOption(option, index); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className={classnames}> | ||
{renderOptions(options)} | ||
</div> | ||
); | ||
}; |
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