-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 +1,27 @@ | ||
export const Select = () => <>select</> | ||
import { ReactElement } from 'react' | ||
|
||
import { SelectProvider } from './SelectContext' | ||
import { findElement } from './utils' | ||
|
||
export interface SelectProps { | ||
children: ReactElement[] | ||
value?: string | ||
placeholder?: string | ||
} | ||
|
||
export const Select = ({ children, placeholder }: SelectProps) => { | ||
const trigger = findElement('Trigger', children) | ||
|
||
const items = findElement('Items', children) | ||
|
||
return ( | ||
<SelectProvider | ||
data-spark-component="select" | ||
// value={value} | ||
placeholder={placeholder} | ||
items={items} | ||
> | ||
{trigger} | ||
</SelectProvider> | ||
) | ||
} |
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,39 @@ | ||
import { createContext, ReactElement, type ReactNode, useContext } from 'react' | ||
|
||
export interface SelectContextState { | ||
items: ReactElement | undefined | ||
placeholder?: string | undefined | ||
} | ||
|
||
const SelectContext = createContext<SelectContextState | null>(null) | ||
|
||
export const SelectProvider = ({ | ||
children, | ||
items, | ||
placeholder, | ||
}: { | ||
children: ReactNode | ||
items: ReactElement | undefined | ||
placeholder: string | undefined | ||
}) => { | ||
return ( | ||
<SelectContext.Provider | ||
value={{ | ||
items, | ||
placeholder, | ||
}} | ||
> | ||
{children} | ||
</SelectContext.Provider> | ||
) | ||
} | ||
|
||
export const useSelect = () => { | ||
const context = useContext(SelectContext) | ||
|
||
if (!context) { | ||
throw Error('useSelect must be used within a Select provider') | ||
} | ||
|
||
return context | ||
} |
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,6 @@ | ||
export const Item = ({ children }: { children: string }) => { | ||
return <option value={children}>{children}</option> | ||
} | ||
|
||
Item.id = 'Item' | ||
Item.displayName = 'Select.Item' |
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,17 @@ | ||
import { PropsWithChildren } from 'react' | ||
|
||
import { useSelect } from './SelectContext' | ||
|
||
export const Items = ({ children }: PropsWithChildren) => { | ||
const { placeholder } = useSelect() | ||
|
||
return ( | ||
<select className="absolute left-none top-none h-full w-full opacity-0"> | ||
{placeholder && <option value="">{placeholder}</option>} | ||
{children} | ||
</select> | ||
) | ||
} | ||
|
||
Items.id = 'Items' | ||
Items.displayName = 'Select.Items' |
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,4 @@ | ||
export const ItemsGroup = () => null | ||
|
||
ItemsGroup.id = 'ItemsGroup' | ||
ItemsGroup.displayName = 'Select.ItemsGroup' |
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,9 @@ | ||
import { Icon } from '@spark-ui/icon' | ||
import { ReactElement } from 'react' | ||
|
||
export const LeadingIcon = ({ children }: { children: ReactElement }) => { | ||
return <Icon>{children}</Icon> | ||
} | ||
|
||
LeadingIcon.id = 'LeadingIcon' | ||
LeadingIcon.displayName = 'Select.LeadingIcon' |
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,23 @@ | ||
import { Icon } from '@spark-ui/icon' | ||
import { ArrowHorizontalDown } from '@spark-ui/icons/dist/icons/ArrowHorizontalDown' | ||
import { ReactNode } from 'react' | ||
|
||
import { useSelect } from './SelectContext' | ||
|
||
export const Trigger = ({ children }: { children?: ReactNode }) => { | ||
const { items, placeholder } = useSelect() | ||
|
||
return ( | ||
<div className="relative flex h-sz-44 items-center gap-md rounded-md border-sm border-outline bg-surface px-lg focus-within:u-ring"> | ||
{children} | ||
{placeholder && <p className="grow">{placeholder}</p>} | ||
<Icon> | ||
<ArrowHorizontalDown /> | ||
</Icon> | ||
{items && items} | ||
</div> | ||
) | ||
} | ||
|
||
Trigger.id = 'Trigger' | ||
Trigger.displayName = 'Select.Trigger' |
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 +1,29 @@ | ||
export { Select } from './Select' | ||
import type { FC } from 'react' | ||
|
||
import { Select as Root, type SelectProps } from './Select' | ||
import { Item } from './SelectItem' | ||
import { Items } from './SelectItems' | ||
import { ItemsGroup } from './SelectItemsGroup' | ||
import { LeadingIcon } from './SelectLeadingIcon' | ||
import { Trigger } from './SelectTrigger' | ||
|
||
export const Select: FC<SelectProps> & { | ||
Trigger: typeof Trigger | ||
LeadingIcon: typeof LeadingIcon | ||
Items: typeof Items | ||
Item: typeof Item | ||
ItemsGroup: typeof ItemsGroup | ||
} = Object.assign(Root, { | ||
Trigger, | ||
LeadingIcon, | ||
Items, | ||
Item, | ||
ItemsGroup, | ||
}) | ||
|
||
Select.displayName = 'Select' | ||
Trigger.displayName = 'Select.Trigger' | ||
LeadingIcon.displayName = 'Select.LeadingIcon' | ||
Items.displayName = 'Select.Items' | ||
Item.displayName = 'Select.Item' | ||
ItemsGroup.displayName = 'Select.ItemsGroup' |
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,9 @@ | ||
import { type FC, type ReactElement } from 'react' | ||
|
||
const getElementId = (element?: ReactElement) => { | ||
return element ? (element.type as FC & { id?: string }).id : '' | ||
} | ||
|
||
export const findElement = (value: string, children: ReactElement[]) => { | ||
return children.find(child => value === getElementId(child) || '') | ||
} |