Skip to content

Commit

Permalink
imply asChild given children
Browse files Browse the repository at this point in the history
  • Loading branch information
steppy452 committed Jul 26, 2024
1 parent aeec720 commit fe383a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/SessionsList/NewSessionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import { SessionsContext } from '@/SessionsContext';
import { Slot } from '@radix-ui/react-slot';

interface NewSessionButtonProps extends PropsWithChildren {
asChild?: boolean;
newSessionText?: string;
}

export const NewSessionButton: FC<NewSessionButtonProps> = ({
children,
asChild,
newSessionText = 'New Session'
}) => {
const { theme, createSession } = useContext(SessionsContext);
const Comp = asChild ? Slot : Button;
const Comp = children ? Slot : Button;
return (
<Comp
fullWidth
Expand All @@ -23,7 +21,7 @@ export const NewSessionButton: FC<NewSessionButtonProps> = ({
className={cn(theme.sessions.create)}
onClick={createSession}
>
{asChild ? children : newSessionText}
{children || newSessionText}
</Comp>
);
};
6 changes: 2 additions & 4 deletions src/SessionsList/SessionListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SessionsContext } from '@/SessionsContext';
import { Slot } from '@radix-ui/react-slot';

interface SessionListItemProps {
asChild?: boolean;
children?: ReactNode;

/**
Expand All @@ -22,13 +21,12 @@ interface SessionListItemProps {

export const SessionListItem: FC<SessionListItemProps> = ({
children,
asChild,
session,
deleteIcon = <TrashIcon />
}) => {
const { activeSessionId, selectSession, deleteSession, theme } =
useContext(SessionsContext);
const Comp = asChild ? Slot : ListItem;
const Comp = children ? Slot : ListItem;
return (
<Comp
dense
Expand All @@ -54,7 +52,7 @@ export const SessionListItem: FC<SessionListItemProps> = ({
</>
}
>
{asChild ? children : <Ellipsis value={session.title} limit={100} />}
{children || <Ellipsis value={session.title} limit={100} />}
</Comp>
);
};
21 changes: 14 additions & 7 deletions stories/Demos.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {
SessionMessageProps,
SessionInput
} from '../src';
import { Card, Divider, Input } from 'reablocks';
import { Card, Divider, IconButton, Input, ListItem } from 'reablocks';
import { subDays, subMinutes, subHours } from 'date-fns';
import { groupSessionsByDate } from '@/utils';

export default {
title: 'Demos',
Expand Down Expand Up @@ -97,7 +96,9 @@ export const Console = () => {
{groups =>
groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading} key={heading}>
{sessions.map(s => <SessionListItem key={s.id} session={s} />)}
{sessions.map(s => (
<SessionListItem key={s.id} session={s} />
))}
</SessionsGroup>
))
}
Expand Down Expand Up @@ -140,15 +141,19 @@ export const CustomComponents = () => {
borderRadius: 5
}}
>
<Sessions sessions={fakeSessions}>
<Sessions sessions={fakeSessions} onDeleteSession={deleteSession}>
<SessionsList>
<NewSessionButton />
<NewSessionButton>
<button className="text-blue-500">New Session</button>
</NewSessionButton>
<Divider />
<SessionGroups>
{groups =>
groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading} key={heading}>
{sessions.map(s => <SessionListItem key={s.id} session={s} />)}
{sessions.map(s => (
<SessionListItem key={s.id} session={s} />
))}
</SessionsGroup>
))
}
Expand Down Expand Up @@ -230,7 +235,9 @@ export const Embeds = () => {
{groups =>
groups.map(({ heading, sessions }) => (
<SessionsGroup heading={heading} key={heading}>
{sessions.map(s => <SessionListItem key={s.id} session={s} />)}
{sessions.map(s => (
<SessionListItem key={s.id} session={s} />
))}
</SessionsGroup>
))
}
Expand Down

0 comments on commit fe383a6

Please sign in to comment.