Skip to content

Commit

Permalink
Host -> HostSlots
Browse files Browse the repository at this point in the history
  • Loading branch information
nihgwu committed Aug 15, 2022
1 parent 084dca4 commit 62c51d6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/__fixtures__/RFCField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as React from 'react'

import { createHost, createSlot, getSlot, getLastSlot, getSlots } from '../rfc'
import {
createHost,
createSlot,
getSlot,
getLastSlot,
getSlots,
getSlotProps,
} from '../rfc'

const Description = (props: React.ComponentPropsWithoutRef<'span'>) => (
<span {...props} />
Expand All @@ -16,19 +23,23 @@ type FieldProps = React.ComponentPropsWithoutRef<'div'>
export const Field = (props: FieldProps) => {
return createHost(props.children, (slots) => {
const labelSlot = getSlot(slots, FieldLabel)
const lastLabelSlot = getLastSlot(slots, FieldLabel)
const lastLabelProps = getSlotProps(getLastSlot(slots, FieldLabel))
const inputSlot = getSlot(slots, FieldInput)
const lastInputSlot = getLastSlot(slots, FieldInput)
const lastDescriptionSlot = getLastSlot(slots, FieldDescription)
const lastInputProps = getSlotProps(getLastSlot(slots, FieldInput))
const descriptionSlot = getSlot(slots, FieldDescription)
const lastDescriptionProps = getSlotProps(
getLastSlot(slots, FieldDescription)
)
const icons = getSlots(slots, FieldIcon)
return (
<div {...props}>
{lastLabelSlot && <label {...lastLabelSlot.props} />}
{lastInputSlot && <input {...lastInputSlot.props} />}
{lastDescriptionSlot}
{lastLabelProps && <label {...lastLabelProps} />}
{lastInputProps && <input {...lastInputProps} />}
{lastDescriptionProps && <Description {...lastDescriptionProps} />}
{icons}
{labelSlot}
{inputSlot}
{descriptionSlot}
</div>
)
})
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rfc.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,6 @@ test('dev warning', () => {
)
expect(warn).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledWith(
'Unwrapped children found in "Host", either wrap them in subcomponents or remove'
'Unwrapped children found in "HostSlots", either wrap them in slots or remove'
)
})
2 changes: 1 addition & 1 deletion src/__tests__/simple.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ test('dev warning', () => {
)
expect(warn).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledWith(
'Unwrapped children found in "Host", either wrap them in subcomponents or remove'
'Unwrapped children found in "HostSlots", either wrap them in slots or remove'
)
})

Expand Down
6 changes: 3 additions & 3 deletions src/rfc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const createIdGenerator = (prefix: string) => {

const genSlotId = createIdGenerator('s')

export const Host = ({
export const HostSlots = ({
children,
callback,
}: {
Expand All @@ -42,7 +42,7 @@ export const Host = ({
<SlotsContext.Provider value={Slots}>
<ScanProvider>
{process.env.NODE_ENV === 'development' ? (
<DevChildren name={'Host'}>{children}</DevChildren>
<DevChildren name="HostSlots">{children}</DevChildren>
) : (
children
)}
Expand All @@ -54,7 +54,7 @@ export const Host = ({
}

export const createHost = (children: React.ReactNode, callback: Callback) => {
return <Host children={children} callback={callback} />
return <HostSlots children={children} callback={callback} />
}

export const createSlot = <T extends React.ElementType>(Fallback?: T) => {
Expand Down
10 changes: 10 additions & 0 deletions src/rfc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ export const getLastSlot = <T extends React.ElementType>(
}
return undefined
}

export const getSlotProps = <T extends SlotElement>(slotElement?: T) => {
if (!slotElement) return undefined

type Props = React.ComponentPropsWithRef<
T extends SlotElement<infer P> ? P : never
>
const { key, ref, props } = slotElement
return { ...props, key, ref } as Props
}
6 changes: 3 additions & 3 deletions src/simple/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Template = ({ children }: { children: () => ReturnType<Callback> }) => {
return children()
}

export const Host = ({
export const HostSlots = ({
children,
callback,
}: {
Expand All @@ -30,7 +30,7 @@ export const Host = ({
<>
<SlotsContext.Provider value={Slots}>
{process.env.NODE_ENV === 'development' ? (
<DevChildren name={'Host'}>{children}</DevChildren>
<DevChildren name="HostSlots">{children}</DevChildren>
) : (
children
)}
Expand All @@ -41,7 +41,7 @@ export const Host = ({
}

export const createHost = (children: React.ReactNode, callback: Callback) => {
return <Host children={children} callback={callback} />
return <HostSlots children={children} callback={callback} />
}

export const createSlot = <T extends React.ElementType>(Fallback?: T) => {
Expand Down

0 comments on commit 62c51d6

Please sign in to comment.