Skip to content

Commit

Permalink
refactor(examples): improve compound components
Browse files Browse the repository at this point in the history
  • Loading branch information
jofaval committed Aug 18, 2023
1 parent 4fe9a76 commit a19d3c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All the log of changes on the project/repository

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## 2023-08-18

### Modified

- Improved typing and compound component examples

## 2023-08-08

### Added
Expand Down
75 changes: 40 additions & 35 deletions examples/compound-components/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type ParentProps = PropsWithChildren<{
}>;

type ParentType = FC<ParentProps> & {
NestedChild: FC;
// NestedChild: FC;
NestedChild: typeof NestedChild; // safer option
};

const Parent: ParentType = ({ children, subCode }) => {
Expand All @@ -45,54 +46,58 @@ const Parent: ParentType = ({ children, subCode }) => {
};
Parent.NestedChild = NestedChild;

type CompoundParentProps = PropsWithChildren<{
code: string;
type CompoundParentProps<TCode extends string> = PropsWithChildren<{
code: TCode;
}>;

const CompoundParent: FC<CompoundParentProps> & {
Parent: ParentType;
} = ({ code, children }) => {
const CompoundParent = <TCode extends string>({
code,
children,
}: CompoundParentProps<TCode>) => {
return <Provider value={{ code }}>{children}</Provider>;
};
CompoundParent.Parent = Parent;

const Group = Object.assign(CompoundParent, {
Parent,
});

function App() {
return (
<div className="app">
<CompoundParent code="first">
<CompoundParent.Parent subCode="top">
<CompoundParent.Parent.NestedChild />
<CompoundParent.Parent.NestedChild />
</CompoundParent.Parent>
<CompoundParent.Parent subCode="bottom">
<CompoundParent.Parent.NestedChild />
<CompoundParent.Parent.NestedChild />
</CompoundParent.Parent>
</CompoundParent>

<CompoundParent code="second">
<CompoundParent.Parent subCode="top">
<CompoundParent.Parent.NestedChild />
<CompoundParent.Parent.NestedChild />
</CompoundParent.Parent>
<CompoundParent.Parent subCode="bottom">
<CompoundParent.Parent.NestedChild />
<CompoundParent.Parent.NestedChild />
</CompoundParent.Parent>
</CompoundParent>
<Group code="first">
<Group.Parent subCode="top">
<Group.Parent.NestedChild />
<Group.Parent.NestedChild />
</Group.Parent>
<Group.Parent subCode="bottom">
<Group.Parent.NestedChild />
<Group.Parent.NestedChild />
</Group.Parent>
</Group>

<Group code="second">
<Group.Parent subCode="top">
<Group.Parent.NestedChild />
<Group.Parent.NestedChild />
</Group.Parent>
<Group.Parent subCode="bottom">
<Group.Parent.NestedChild />
<Group.Parent.NestedChild />
</Group.Parent>
</Group>

{/* ERROR, no parent nor compound parent */}
<CompoundParent.Parent.NestedChild />
<Group.Parent.NestedChild />

{/* ERROR, no compound parent */}
<CompoundParent.Parent subCode="willThrowError">
<CompoundParent.Parent.NestedChild />
</CompoundParent.Parent>
<Group.Parent subCode="willThrowError">
<Group.Parent.NestedChild />
</Group.Parent>

{/* ERROR, no parent */}
<CompoundParent code="willAlsoFail">
<CompoundParent.Parent.NestedChild />
</CompoundParent>
<Group code="willAlsoFail">
<Group.Parent.NestedChild />
</Group>
</div>
);
}
Expand Down

0 comments on commit a19d3c5

Please sign in to comment.