Skip to content

Commit

Permalink
support custom tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ZxMYS committed Jun 24, 2019
1 parent 776fddd commit fc652e4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ 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. 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. |
| `noWrapper` | `Boolean` | `false` | When it is `true`, `Twemoji` will not render a wrapping element (with `tag`) 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. |
| `tag` | `string` | `div` | The tag of the wrapping element. This option is ignored when `noWrapper` is `true`. |

### Run example

Expand Down
3 changes: 3 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export default class App extends React.Component {
<Twemoji noWrapper={true} options={{ className: 'twemoji' }}>
<p>😂😅</p><p>😍😉</p>
</Twemoji>
<Twemoji tag='p' options={{ className: 'twemoji' }}>
😂😅<br/>😍😉
</Twemoji>
</div>
);
}
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.1",
"version": "0.2.2",
"description": "A React wrapper for Twemoji",
"keyword": [
"react",
Expand Down
11 changes: 11 additions & 0 deletions src/Twemoji/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ import Twemoji from '..';
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>);
}

function renderTwemojiWithSpan() {
return TestUtils.renderIntoDocument(<Twemoji tag='span'><a>😉😊</a></Twemoji>);
}

suite('Twemoji', () => {
test('should parse emoji in children', () => {
const rendered = renderTwemoji();
const node = ReactDOM.findDOMNode(rendered); // eslint-disable-line react/no-find-dom-node
assert.equal(node.querySelectorAll('img').length, 2);
});

test('should render with custom tag when it\'s set', () => {
const rendered = renderTwemojiWithSpan();
const node = ReactDOM.findDOMNode(rendered); // eslint-disable-line react/no-find-dom-node
assert.equal(node.tagName, 'SPAN');
});

test('should parse again when children is updated', () => {
const node = document.createElement('div');
ReactDOM.render(<Twemoji>😐😑</Twemoji>, node);
Expand Down
11 changes: 8 additions & 3 deletions src/Twemoji/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export default class Twemoji extends React.Component {
static propTypes = {
children: PropTypes.node,
noWrapper: PropTypes.bool,
options: PropTypes.object
options: PropTypes.object,
tag: PropTypes.string
}

static defaultProps = {
tag: 'div'
}

constructor(props) {
Expand Down Expand Up @@ -43,7 +48,7 @@ export default class Twemoji extends React.Component {
}

render() {
const { children, noWrapper, ...other } = this.props;
const { children, noWrapper, tag, ...other } = this.props;
if (noWrapper) {
return (
<>
Expand All @@ -61,7 +66,7 @@ export default class Twemoji extends React.Component {
</>);
} else {
delete other.options;
return <div ref={this.rootRef} {...other}>{children}</div>;
return React.createElement(tag, { ref: this.rootRef, ...other }, children);
}
}
}

0 comments on commit fc652e4

Please sign in to comment.