Skip to content

Commit

Permalink
feat: resolve id prop from component JSON root
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Oct 17, 2018
1 parent 72bb8d7 commit 214c256
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const Example = () => {
which would look up the `Button` component at index `0` in the `components` object, resolving to:

```jsx
<Button>Hello, World!</Button>
<Button id={0}>Hello, World!</Button>
```

For `react-from-json` we use JSON, so we would write this:
Expand All @@ -105,6 +105,8 @@ For `react-from-json` we use JSON, so we would write this:
}
```

> The `id` here is set by the `componentIndex`, since we didn't specify one in the JSON. See <a href="#a-note-on-ids">this comment on IDs</a> for more information.
#### Example

Here's the same example as above, instead using a `<ComponentLookup />` for `entry.props.patty`, and providing a separate `components` object.
Expand Down Expand Up @@ -155,7 +157,11 @@ const Example = () => {
};
```

## With TypeScript
### A note on ids

`react-from-json` will map `id` from the root of your component JSON to the React component's `id` prop. Likewise, if you specify `id` under `props`, it will use this. If you use the `<ComponentLookup />` component, `react-from-json` will use the array index as `id` unless another `id` is specified. **Your `id` will always take priority.**

### With TypeScript

`react-from-json` supports generic types for use with TypeScript.

Expand Down
4 changes: 3 additions & 1 deletion src/__helpers__/burger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export const Bun = ({ variant }: { variant: string }) => (
);

export const Patty = ({
id,
size,
ingredient
}: {
id: number;
size: string;
ingredient: React.ReactNode;
}) => (
<div>
<div id={`${id}`}>
{`${size} patty`}
{ingredient}
</div>
Expand Down
16 changes: 12 additions & 4 deletions src/__tests__/__snapshots__/index.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ exports[`ReactFromJSON to render a flat entry with a components attribute 1`] =
cheese
</div>
<div>
<div>
<div
id="0"
>
large patty
<div>
impossible
</div>
</div>
<div>
<div
id="1"
>
large patty
<div>
beef
Expand All @@ -43,13 +47,17 @@ exports[`ReactFromJSON to render a recursive entry 1`] = `
cheese
</div>
<div>
<div>
<div
id="undefined"
>
large patty
<div>
impossible
</div>
</div>
<div>
<div
id="undefined"
>
large patty
<div>
beef
Expand Down
8 changes: 7 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ class ReactFromJSON<

const component = components[componentType][componentIndex];

return this.renderComponent(component);
return this.renderComponent({
...component,
props: {
id: component.id || componentIndex, // Map id to component props if specified on root. Otherwise, use index.
...component.props
}
});
};

resolveProp = (prop: any): any => {
Expand Down

0 comments on commit 214c256

Please sign in to comment.