2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-17 19:21:20 +03:00

feat(react): allow pass custom options to react component

This commit is contained in:
Nikolay Kostyurin
2018-10-25 16:39:49 +02:00
parent 6d37793001
commit 77b30f3d44
3 changed files with 10 additions and 8 deletions
+2 -2
View File
@@ -88,7 +88,7 @@ import {render} from 'react-dom'
import bbobReactRender from '@bbob/react/es/render' import bbobReactRender from '@bbob/react/es/render'
import presetReact from '@bbob/preset-react' import presetReact from '@bbob/preset-react'
console.log(render(<span>{bbobReactRender(`[i]Text[/i]`, presetReact())}</span>)); // <span><span style="font-style: italic;">Text</span></span> console.log(render(<span>{bbobReactRender(`[i]Text[/i]`, presetReact(), { onlyAllowTags: ['i'] })}</span>)); // <span><span style="font-style: italic;">Text</span></span>
``` ```
### Presets <a name="basic"></a> ### Presets <a name="basic"></a>
@@ -165,7 +165,7 @@ import BBCode from '@bbob/react/es/Component'
import reactPreset from '@bbob/preset-react/es' import reactPreset from '@bbob/preset-react/es'
const MyComponent = () => ( const MyComponent = () => (
<BBCode plugins={[reactPreset()]}> <BBCode plugins={[reactPreset()]} options={{ onlyAllowTags: ['i'] }}>
[quote]Text[/quote] [quote]Text[/quote]
</BBCode> </BBCode>
) )
+4 -3
View File
@@ -2,11 +2,11 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { render } from './render'; import { render } from './render';
const content = (children, plugins) => React.Children.map(children, child => const content = (children, plugins, options) => React.Children.map(children, child =>
(typeof child === 'string' ? render(child, plugins) : child)); (typeof child === 'string' ? render(child, plugins, options) : child));
const Component = props => const Component = props =>
React.createElement(props.container, {}, content(props.children, props.plugins)); React.createElement(props.container, {}, content(props.children, props.plugins, props.options));
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
Component.propTypes = { Component.propTypes = {
@@ -19,6 +19,7 @@ if (process.env.NODE_ENV !== 'production') {
Component.defaultProps = { Component.defaultProps = {
container: 'span', container: 'span',
plugins: [], plugins: [],
options: {},
}; };
export default Component; export default Component;
+4 -3
View File
@@ -4,8 +4,9 @@ import * as html from '@bbob/html';
import { isTagNode, isStringNode } from '@bbob/plugin-helper'; import { isTagNode, isStringNode } from '@bbob/plugin-helper';
const toAST = (source, plugins) => core(plugins) const toAST = (source, plugins, options) => core(plugins)
.process(source, { .process(source, {
...options,
render: input => html.render(input, { stripTags: true }), render: input => html.render(input, { stripTags: true }),
}).tree; }).tree;
@@ -32,8 +33,8 @@ function renderToReactNodes(nodes) {
return els; return els;
} }
function render(source, plugins) { function render(source, plugins, options) {
return renderToReactNodes(toAST(source, plugins)); return renderToReactNodes(toAST(source, plugins, options));
} }
export { render }; export { render };