2020-02-11 14:01:35 +00:00
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
import React, { useContext } from 'react';
|
2020-05-18 16:25:13 +00:00
|
|
|
import { StyleSheet, Text } from 'react-native';
|
2020-02-11 14:01:35 +00:00
|
|
|
import {
|
|
|
|
uiKitMessage,
|
|
|
|
UiKitParserMessage,
|
|
|
|
uiKitModal,
|
|
|
|
UiKitParserModal,
|
|
|
|
BLOCK_CONTEXT
|
|
|
|
} from '@rocket.chat/ui-kit';
|
|
|
|
|
|
|
|
import Markdown from '../markdown';
|
|
|
|
import Button from '../Button';
|
|
|
|
import TextInput from '../TextInput';
|
|
|
|
|
2020-05-18 16:25:13 +00:00
|
|
|
import { useBlockContext, textParser } from './utils';
|
2020-02-11 14:01:35 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2020-05-18 16:25:13 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
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
|
2020-05-18 16:25:13 +00:00
|
|
|
},
|
|
|
|
text: {
|
|
|
|
fontSize: 16,
|
|
|
|
lineHeight: 22,
|
|
|
|
textAlignVertical: 'center',
|
|
|
|
...sharedStyles.textRegular
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const plainText = ({ text } = { text: '' }) => text;
|
|
|
|
|
|
|
|
class MessageParser extends UiKitParserMessage {
|
2021-07-27 02:37:02 +00:00
|
|
|
text({ text, type }: any = { text: '' }, context: any) {
|
|
|
|
const { theme }: any = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
if (type !== 'mrkdwn') {
|
2020-05-18 16:25:13 +00:00
|
|
|
return <Text style={[styles.text, { color: themes[theme].bodyText }]}>{text}</Text>;
|
2020-02-11 14:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const isContext = context === BLOCK_CONTEXT.CONTEXT;
|
|
|
|
return (
|
2021-07-27 02:37:02 +00:00
|
|
|
// @ts-ignore
|
2020-02-11 14:01:35 +00:00
|
|
|
<Markdown
|
|
|
|
msg={text}
|
|
|
|
theme={theme}
|
|
|
|
style={[isContext && { color: themes[theme].auxiliaryText }]}
|
|
|
|
preview={isContext}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
button(element: any, context: any) {
|
|
|
|
const { text, value, actionId, style } = element;
|
|
|
|
const [{ loading }, action]: any = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={actionId}
|
|
|
|
type={style}
|
2020-05-18 16:25:13 +00:00
|
|
|
title={textParser([text])}
|
2020-02-11 14:01:35 +00:00
|
|
|
loading={loading}
|
|
|
|
onPress={() => action({ value })}
|
|
|
|
style={styles.button}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
divider() {
|
2021-07-27 02:37:02 +00:00
|
|
|
const { theme }: any = useContext(ThemeContext);
|
|
|
|
// @ts-ignore
|
2020-02-11 14:01:35 +00:00
|
|
|
return <Divider theme={theme} />;
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
section(args: any) {
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return <Section {...args} theme={theme} parser={this} />;
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
actions(args: any) {
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return <Actions {...args} theme={theme} parser={this} />;
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
overflow(element: any, context: any) {
|
|
|
|
const [{ loading }, action]: any = useBlockContext(element, context);
|
|
|
|
const { theme }: any = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
return (
|
|
|
|
<Overflow
|
|
|
|
element={element}
|
|
|
|
context={context}
|
|
|
|
loading={loading}
|
|
|
|
action={action}
|
|
|
|
theme={theme}
|
|
|
|
parser={this}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
datePicker(element: any, context: any) {
|
|
|
|
const [{loading, value, error, language}, action]: any = useBlockContext(element, context);
|
|
|
|
const { theme }: any = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
return (
|
|
|
|
<DatePicker
|
|
|
|
element={element}
|
|
|
|
language={language}
|
|
|
|
theme={theme}
|
|
|
|
value={value}
|
|
|
|
action={action}
|
|
|
|
context={context}
|
|
|
|
loading={loading}
|
|
|
|
error={error}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
image(element: any, context: any) {
|
|
|
|
const { theme }: any = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
return <Image element={element} theme={theme} context={context} />;
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
context(args: any) {
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return <Context {...args} theme={theme} parser={this} />;
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
multiStaticSelect(element: any, context: any) {
|
|
|
|
const [{ loading, value }, action]: any = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return (
|
|
|
|
<MultiSelect
|
|
|
|
{...element}
|
|
|
|
theme={theme}
|
|
|
|
value={value}
|
|
|
|
onChange={action}
|
|
|
|
context={context}
|
|
|
|
loading={loading}
|
|
|
|
multiselect
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
staticSelect(element: any, context: any) {
|
|
|
|
const [{ loading, value }, action]: any = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
{...element}
|
|
|
|
theme={theme}
|
|
|
|
value={value}
|
|
|
|
onChange={action}
|
|
|
|
loading={loading}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
selectInput(element: any, context: any) {
|
|
|
|
const [{ loading, value }, action]: any = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
return (
|
|
|
|
<MultiSelect
|
|
|
|
{...element}
|
|
|
|
theme={theme}
|
|
|
|
value={value}
|
|
|
|
onChange={action}
|
|
|
|
context={context}
|
|
|
|
loading={loading}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-07-27 02:37:02 +00:00
|
|
|
}: any, context: any) {
|
|
|
|
const [{ error }]: any = useBlockContext({ ...element, appId, blockId }, context);
|
|
|
|
const { theme }: any = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
return (
|
|
|
|
<Input
|
|
|
|
parser={this}
|
|
|
|
element={{ ...element, appId, blockId }}
|
|
|
|
label={plainText(label)}
|
|
|
|
description={plainText(description)}
|
|
|
|
hint={plainText(hint)}
|
|
|
|
error={error}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
image(element: any, context: any) {
|
|
|
|
const { theme }: any = useContext(ThemeContext);
|
2020-02-11 14:01:35 +00:00
|
|
|
return <Image element={element} theme={theme} context={context} />;
|
|
|
|
}
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
plainInput(element: any, context: any) {
|
|
|
|
const [{ loading, value, error }, action]: any = useBlockContext(element, context);
|
2020-02-11 14:01:35 +00:00
|
|
|
const { theme } = useContext(ThemeContext);
|
|
|
|
const { multiline, actionId, placeholder } = element;
|
|
|
|
return (
|
2021-07-29 15:45:32 +00:00
|
|
|
// @ts-ignore
|
2020-02-11 14:01:35 +00:00
|
|
|
<TextInput
|
|
|
|
id={actionId}
|
|
|
|
placeholder={plainText(placeholder)}
|
|
|
|
onInput={action}
|
|
|
|
multiline={multiline}
|
|
|
|
loading={loading}
|
2021-07-27 02:37:02 +00:00
|
|
|
onChangeText={(text: any) => action({ value: text })}
|
2020-02-11 14:01:35 +00:00
|
|
|
inputStyle={multiline && styles.multiline}
|
|
|
|
containerStyle={styles.input}
|
|
|
|
value={value}
|
|
|
|
error={{ error }}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const messageParser = new MessageParser();
|
|
|
|
export const modalParser = new ModalParser();
|
|
|
|
|
|
|
|
export const UiKitMessage = uiKitMessage(messageParser);
|
|
|
|
export const UiKitModal = uiKitModal(modalParser);
|
|
|
|
|
2021-07-27 02:37:02 +00:00
|
|
|
export const UiKitComponent = ({ render, blocks }: any) => render(blocks);
|