/* eslint-disable class-methods-use-this */
import React, { useContext } from 'react';
import { StyleSheet, Text } from 'react-native';
import {
uiKitMessage,
UiKitParserMessage,
uiKitModal,
UiKitParserModal,
BLOCK_CONTEXT
} from '@rocket.chat/ui-kit';
import Markdown from '../markdown';
import Button from '../Button';
import TextInput from '../TextInput';
import { useBlockContext, textParser } from './utils';
import { themes } from '../../constants/colors';
import sharedStyles from '../../views/Styles';
import { Divider } from './Divider';
import { Section } from './Section';
import { Actions } from './Actions';
import { Image } from './Image';
import { Select } from './Select';
import { Context } from './Context';
import { MultiSelect } from './MultiSelect';
import { Input } from './Input';
import { DatePicker } from './DatePicker';
import { Overflow } from './Overflow';
import { ThemeContext } from '../../theme';
const styles = StyleSheet.create({
input: {
marginBottom: 0
},
multiline: {
height: 130
button: {
marginBottom: 16
text: {
fontSize: 16,
lineHeight: 22,
textAlignVertical: 'center',
...sharedStyles.textRegular
}
});
const plainText = ({ text } = { text: '' }) => text;
class MessageParser extends UiKitParserMessage {
text({ text, type } = { text: '' }, context) {
const { theme } = useContext(ThemeContext);
if (type !== 'mrkdwn') {
return <Text style={[styles.text, { color: themes[theme].bodyText }]}>{text}</Text>;
const isContext = context === BLOCK_CONTEXT.CONTEXT;
return (
<Markdown
msg={text}
theme={theme}
style={[isContext && { color: themes[theme].auxiliaryText }]}
preview={isContext}
/>
);
button(element, context) {
const {
text, value, actionId, style
} = element;
const [{ loading }, action] = useBlockContext(element, context);
<Button
key={actionId}
type={style}
title={textParser([text])}
loading={loading}
onPress={() => action({ value })}
style={styles.button}
divider() {
return <Divider theme={theme} />;
section(args) {
return <Section {...args} theme={theme} parser={this} />;
actions(args) {
return <Actions {...args} theme={theme} parser={this} />;
overflow(element, context) {
<Overflow
element={element}
context={context}
action={action}
parser={this}
datePicker(element, context) {
const [{
loading, value, error, language
}, action] = useBlockContext(element, context);
<DatePicker
language={language}
value={value}
error={error}
image(element, context) {
return <Image element={element} theme={theme} context={context} />;
context(args) {
return <Context {...args} theme={theme} parser={this} />;
multiStaticSelect(element, context) {
const [{ loading, value }, action] = useBlockContext(element, context);
<MultiSelect
{...element}
onChange={action}
multiselect
staticSelect(element, context) {
<Select
selectInput(element, context) {
class ModalParser extends UiKitParserModal {
constructor() {
super();
Object.getOwnPropertyNames(MessageParser.prototype).forEach((method) => {
ModalParser.prototype[method] = ModalParser.prototype[method] || MessageParser.prototype[method];
input({
element, blockId, appId, label, description, hint
}, context) {
const [{ error }] = useBlockContext({ ...element, appId, blockId }, context);
<Input
element={{ ...element, appId, blockId }}
label={plainText(label)}
description={plainText(description)}
hint={plainText(hint)}
plainInput(element, context) {
const [{ loading, value, error }, action] = useBlockContext(element, context);
const { multiline, actionId, placeholder } = element;
<TextInput
id={actionId}
placeholder={plainText(placeholder)}
onInput={action}
multiline={multiline}
onChangeText={text => action({ value: text })}
inputStyle={multiline && styles.multiline}
containerStyle={styles.input}
error={{ error }}
export const messageParser = new MessageParser();
export const modalParser = new ModalParser();
export const UiKitMessage = uiKitMessage(messageParser);
export const UiKitModal = uiKitModal(modalParser);
export const UiKitComponent = ({ render, blocks }) => render(blocks);