Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Add t.ReactChildren to pre-defined types #19

Closed
jednano opened this issue Dec 2, 2015 · 5 comments
Closed

Add t.ReactChildren to pre-defined types #19

jednano opened this issue Dec 2, 2015 · 5 comments

Comments

@jednano
Copy link
Contributor

jednano commented Dec 2, 2015

In lieu of #18 it seems useful to include t.ReactChildren as a pre-defined type. Would you be willing to accept a PR with this inclusion?

@jednano
Copy link
Contributor Author

jednano commented Dec 2, 2015

Also, I think it may be necessary to include t.ReactNode here, for cases when the children are a single TextNode. For example:

<FooComponent>
    Hello, World!
</FooComponent>

So the final type would be:

export const ReactChildren = t.union([
    t.Nil,
    t.ReactNode,
    t.ReactElement,
    t.list(t.ReactElement)
]);

How does that sound?

@gcanti
Copy link
Owner

gcanti commented Dec 3, 2015

Hi,
Sorry for the delay.

Isn't ReactNode enough for your use case?

import React from 'react';
import ReactDOM from 'react-dom';
import {t, props, ReactNode} from 'tcomb-react'

@props({
  children: t.maybe(ReactNode)
})
class A extends React.Component {
  render() {
    return <div>hello</div>;
  }
}

const mountNode = document.querySelector('#app')

ReactDOM.render(<A />, mountNode); // => ok
ReactDOM.render(<A><div/></A>, mountNode); // => ok
ReactDOM.render(<A><div/><div/></A>, mountNode); // => ok
ReactDOM.render(<A>world</A>, mountNode); // => ok
ReactDOM.render(<A>{null}</A>, mountNode); // => ok
ReactDOM.render(<A><div>{null}</div></A>, mountNode); // => ok
ReactDOM.render(<A><div/>{null}</A>, mountNode); // => KO!

@jednano
Copy link
Contributor Author

jednano commented Dec 3, 2015

That appears to be working for now. I'll reopen if it presents itself again. Thanks!

@jednano jednano closed this as completed Dec 3, 2015
@jednano
Copy link
Contributor Author

jednano commented Dec 17, 2015

@gcanti it looks like this fails when you return false and I presume undefined in children. For example:

<Foo>
    {isBar && (
        <div>baz</div>
    )}
</Foo>

If isBar is false then t.maybe(t.ReactNode) fails with:

Invalid value false supplied to ReactNode.

However, if I don't use tcomb here then React gracefully displays nothing for false values.

I think ReactChildren might be better described as:

const ReactChild = t.union([
  t.ReactNode,
  t.Boolean,
  t.Nil
]);

const ReactChildren = t.maybe(
  t.union([
    ReactChild,
    t.list(ReactChild)
  ])
);

What do you think?

@gcanti
Copy link
Owner

gcanti commented Dec 17, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants