|
| 1 | +import ExpensiMark from 'expensify-common/lib/ExpensiMark'; |
| 2 | +import React, {useState} from 'react'; |
| 3 | +import lodashGet from 'lodash/get'; |
| 4 | +import {View, Image} from 'react-native'; |
| 5 | +import Composer from '../components/Composer'; |
| 6 | +import RenderHTML from '../components/RenderHTML'; |
| 7 | +import Text from '../components/Text'; |
| 8 | +import styles from '../styles/styles'; |
| 9 | +import themeColors from '../styles/themes/default'; |
| 10 | +import * as StyleUtils from '../styles/StyleUtils'; |
| 11 | +import CONST from '../CONST'; |
| 12 | + |
| 13 | +/** |
| 14 | + * We use the Component Story Format for writing stories. Follow the docs here: |
| 15 | + * |
| 16 | + * https://storybook.js.org/docs/react/writing-stories/introduction#component-story-format |
| 17 | + */ |
| 18 | +const story = { |
| 19 | + title: 'Components/Composer', |
| 20 | + component: Composer, |
| 21 | +}; |
| 22 | + |
| 23 | +const parser = new ExpensiMark(); |
| 24 | + |
| 25 | +const Default = (args) => { |
| 26 | + const [pastedFile, setPastedFile] = useState(null); |
| 27 | + const [comment, setComment] = useState(args.defaultValue); |
| 28 | + const renderedHTML = parser.replace(comment); |
| 29 | + const [droppingFile, setDroppingFile] = useState(false); |
| 30 | + const [isComposerDroppingTarget, setIsComposerDroppingTarget] = useState(false); |
| 31 | + |
| 32 | + return ( |
| 33 | + <View> |
| 34 | + <View style={[styles.border, styles.p4, droppingFile && isComposerDroppingTarget && styles.borderColorFocus]}> |
| 35 | + <Composer |
| 36 | + // eslint-disable-next-line react/jsx-props-no-spreading |
| 37 | + {...args} |
| 38 | + multiline |
| 39 | + textAlignVertical="top" |
| 40 | + onChangeText={setComment} |
| 41 | + onDragOver={(e, isOriginComposer) => { |
| 42 | + setIsComposerDroppingTarget(isOriginComposer); |
| 43 | + setDroppingFile(true); |
| 44 | + }} |
| 45 | + onDragLeave={() => { |
| 46 | + setIsComposerDroppingTarget(false); |
| 47 | + setDroppingFile(false); |
| 48 | + }} |
| 49 | + onDrop={(e) => { |
| 50 | + e.preventDefault(); |
| 51 | + |
| 52 | + const file = lodashGet(e, ['dataTransfer', 'files', 0]); |
| 53 | + if (!file) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + setPastedFile(file); |
| 58 | + }} |
| 59 | + onPasteFile={setPastedFile} |
| 60 | + style={[styles.textInputCompose, styles.w100]} |
| 61 | + /> |
| 62 | + </View> |
| 63 | + <View style={[styles.flexRow, styles.mv5, styles.flexWrap, styles.w100]}> |
| 64 | + <View |
| 65 | + style={[ |
| 66 | + styles.border, |
| 67 | + styles.noLeftBorderRadius, |
| 68 | + styles.noRightBorderRadius, |
| 69 | + styles.p5, |
| 70 | + styles.flex1, |
| 71 | + droppingFile && !isComposerDroppingTarget && styles.borderColorFocus, |
| 72 | + ]} |
| 73 | + nativeID={CONST.REPORT.DROP_NATIVE_ID} |
| 74 | + > |
| 75 | + <Text style={[styles.mb2, styles.formLabel]}>Entered Comment (Drop Enabled)</Text> |
| 76 | + <Text>{comment}</Text> |
| 77 | + </View> |
| 78 | + <View |
| 79 | + style={[ |
| 80 | + styles.p5, |
| 81 | + styles.borderBottom, |
| 82 | + styles.borderRight, |
| 83 | + styles.borderTop, |
| 84 | + styles.flex1, |
| 85 | + ]} |
| 86 | + > |
| 87 | + <Text style={[styles.mb2, styles.formLabel]}>Rendered Comment</Text> |
| 88 | + {Boolean(renderedHTML) && <RenderHTML html={renderedHTML} />} |
| 89 | + {pastedFile && ( |
| 90 | + <View style={styles.mv3}> |
| 91 | + <Image |
| 92 | + source={{uri: URL.createObjectURL(pastedFile)}} |
| 93 | + resizeMode="contain" |
| 94 | + style={StyleUtils.getWidthAndHeightStyle(250, 250)} |
| 95 | + /> |
| 96 | + </View> |
| 97 | + )} |
| 98 | + </View> |
| 99 | + </View> |
| 100 | + </View> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +Default.args = { |
| 105 | + autoFocus: true, |
| 106 | + placeholder: 'Compose Text Here', |
| 107 | + placeholderTextColor: themeColors.placeholderText, |
| 108 | + defaultValue: `Composer can do the following: |
| 109 | +
|
| 110 | + * It can contain MD e.g. *bold* _italic_ |
| 111 | + * Supports Pasted Images via Ctrl+V |
| 112 | + * Supports Drag N Drop for files.`, |
| 113 | + isDisabled: false, |
| 114 | + maxLines: 16, |
| 115 | +}; |
| 116 | + |
| 117 | +export default story; |
| 118 | +export { |
| 119 | + Default, |
| 120 | +}; |
0 commit comments