UNPKG

@botonic/react

Version:

Build Chatbots using React

42 lines 1.61 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { INPUT } from '@botonic/core'; import React, { Children } from 'react'; import { mapObjectNonBooleanValues } from '../util/react'; import { serializeMarkdown, toMarkdownChildren } from './markdown'; import { Message } from './message'; const serializeText = children => { children = Array.isArray(children) ? children : [children]; const text = children .filter(e => !e.type) .map(e => { if (Array.isArray(e)) return serializeText(e); else return String(e); }) .join(''); return text; }; const serialize = textProps => { if (!textProps.markdown) return { text: serializeText(textProps.children), }; return { text: serializeMarkdown(textProps.children) }; }; /** * * @param {TextProps} props * @returns {JSX.Element} */ export const Text = props => { const defaultTextProps = { markdown: props.markdown === undefined ? true : props.markdown, }; const textProps = mapObjectNonBooleanValues(Object.assign(Object.assign(Object.assign({}, props), defaultTextProps), { children: Children.toArray(props.children) })); if (!textProps.markdown) return (_jsx(Message, Object.assign({ json: serialize(textProps) }, textProps, { type: INPUT.TEXT }, { children: textProps.children }))); return (_jsx(Message, Object.assign({ json: serialize(textProps) }, textProps, { type: INPUT.TEXT }, { children: toMarkdownChildren(textProps.children) }))); }; Text.serialize = serialize; //# sourceMappingURL=text.js.map