-
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.
- Loading branch information
Showing
10 changed files
with
666 additions
and
10 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,53 @@ | ||
import * as React from 'react' | ||
|
||
import { createHost, createSlot } from '../rfc' | ||
|
||
const Divider = (props: React.ComponentPropsWithoutRef<'hr'>) => ( | ||
<hr {...props} /> | ||
) | ||
|
||
const SelectItem = createSlot('li') | ||
const SelectDivider = createSlot(Divider) | ||
|
||
export const Select = (props: React.ComponentPropsWithoutRef<'ul'>) => { | ||
const [selected, setSelected] = React.useState<React.ReactElement>() | ||
const indexRef = React.useRef(0) | ||
|
||
return ( | ||
<div> | ||
<div>Selected: {selected?.props.value ?? ''}</div> | ||
{createHost(props.children, (slots) => { | ||
indexRef.current = 0 | ||
return ( | ||
<ul {...props}> | ||
{slots.map((slot) => { | ||
if (slot.type === SelectItem) { | ||
const itemProps = slot.props | ||
|
||
return ( | ||
<li | ||
key={slot.key} | ||
ref={slot.ref} | ||
{...itemProps} | ||
{...{ | ||
'data-index': indexRef.current++, | ||
'aria-selected': slot === selected, | ||
onClick: () => { | ||
setSelected(slot) | ||
}, | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
return slot | ||
})} | ||
</ul> | ||
) | ||
})} | ||
</div> | ||
) | ||
} | ||
|
||
Select.Item = SelectItem | ||
Select.Divider = SelectDivider |
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,69 @@ | ||
import * as React from 'react' | ||
|
||
import { createHost, createSlot } from '../simple' | ||
|
||
const Description = (props: React.ComponentPropsWithoutRef<'span'>) => ( | ||
<span {...props} /> | ||
) | ||
|
||
const FieldLabel = createSlot('label') | ||
const FieldInput = createSlot('input') | ||
const FieldDescription = createSlot(Description) | ||
const FieldIcon = createSlot('span') | ||
|
||
// const createFill = <P extends keyof typeof SlotComponents>(name: P) => { | ||
// const FillComponent = React.forwardRef((props, ref) => { | ||
// const Slots = useSlots() | ||
// const [originalProps] = React.useState(() => Slots.getProps(name)) | ||
|
||
// React.useEffect(() => Slots.update(name, { ...props, ref })) | ||
// React.useEffect( | ||
// () => () => { | ||
// originalProps ? Slots.update(name, originalProps) : Slots.unmount(name) | ||
// }, | ||
// [Slots, originalProps] | ||
// ) | ||
|
||
// return null | ||
// }) as unknown as typeof SlotComponents[P] | ||
// return FillComponent | ||
// } | ||
|
||
type FieldProps = React.ComponentPropsWithoutRef<'div'> | ||
|
||
export const Field = (props: FieldProps) => { | ||
const id = ':r0:' | ||
const descriptionId = ':r1:' | ||
|
||
return createHost(props.children, (Slots) => { | ||
const labelProps = Slots.getProps(FieldLabel) | ||
const inputProps = Slots.getProps(FieldInput) | ||
const descriptionProps = Slots.getProps(FieldDescription) | ||
const iconProps = Slots.getProps(FieldIcon) | ||
return ( | ||
<div {...props}> | ||
{labelProps && <label {...labelProps} htmlFor={inputProps?.id || id} />} | ||
{inputProps && ( | ||
<input | ||
id={id} | ||
aria-describedby={descriptionProps ? descriptionId : undefined} | ||
{...inputProps} | ||
/> | ||
)} | ||
{(iconProps || descriptionProps) && ( | ||
<div> | ||
{Slots.get(FieldIcon)} | ||
{descriptionProps && ( | ||
<span {...descriptionProps} id={descriptionId} /> | ||
)} | ||
</div> | ||
)} | ||
</div> | ||
) | ||
}) | ||
} | ||
|
||
Field.Label = FieldLabel | ||
Field.Input = FieldInput | ||
Field.Description = FieldDescription | ||
Field.Icon = FieldIcon |
Oops, something went wrong.