Skip to content

Commit

Permalink
handle text child with noWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ZxMYS committed Jun 24, 2019
1 parent 704f8c2 commit 776fddd
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ It outputs following HTML to DOM,
| Name | Type | Default | Description|
|:---- |:---- |:---- |:----|
| `options` | `Object` | `{}` | `twemoji.parse` options |
| `noWrapper` | `Boolean` | `false` | When it is `true`, Twemoji will not render a wrapping `div` to contain children |
| `noWrapper` | `Boolean` | `false` | When it is `true`, `Twemoji` will not render a wrapping `div` to contain children. Note that since `twemoji.parse` needs an DOM element reference, any direct pure text child of `Twemoji` is not parsed when `noWrapper` is `true`. E.g. `foo` in `<Twemoji noWrapper={true}>foo<p>bar</p></Twmoji>` is not parsed. |

### Run example

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-twemoji",
"version": "0.2.0",
"version": "0.2.1",
"description": "A React wrapper for Twemoji",
"keyword": [
"react",
Expand Down
2 changes: 1 addition & 1 deletion src/Twemoji/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function renderTwemoji() {
return TestUtils.renderIntoDocument(<Twemoji><div>πŸ˜‰<a>😊</a></div></Twemoji>);
}
function renderTwemojiWithNoWrapper() {
return TestUtils.renderIntoDocument(<div><Twemoji noWrapper={true}><p>πŸ˜‰<a>😊</a></p></Twemoji></div>);
return TestUtils.renderIntoDocument(<div><Twemoji noWrapper={true}>πŸ˜‰<p>πŸ˜‰<a>😊</a></p></Twemoji></div>);
}

suite('Twemoji', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/Twemoji/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export default class Twemoji extends React.Component {
<>
{
React.Children.map(children, (c, i) => {
if (typeof c === 'string') {
// eslint-disable-next-line no-console
console.warn(`Twemoji can't parse string child when noWrapper is set. Skipping child "${c}"`);
return c;
}
this.childrenRefs[i] = this.childrenRefs[i] || React.createRef();
return React.cloneElement(c, { ref: this.childrenRefs[i] });
})
Expand Down

0 comments on commit 776fddd

Please sign in to comment.