Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion docs/GetStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,32 @@ type contentState: {

See our [`sample`](https://github.com/globocom/react-native-draftjs-render/tree/master/sample) folder for more details.

## Props

- **contentState**: the Draft.js model with `blocks` and `entityMap`.
```js
// Flow type for contentState
type contentState: {
blocks: Array<Object>,
entityMap: Object,
};
```

- **customStyles**: Object with your custom styles. ([Documentation](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/CustomStyles.md)).

- **atomicHandler**: Function to handle atomic types ([Documentation](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/AtomicTypes.md)).

- **navigate** (optional): Function to be called when `onPress` is fired.

- **orderedListSeparator** (optional): String to be rendered after each number of the ordered lists.

- **customBlockHandler** (optional): Default element to be rendered when some type is not knew.

- **depthMargin** (optional): Margin left for each level of depth lists.

- **textProps** (optional): Object containing all the Text props supported by React Native. (See [Text Props](https://facebook.github.io/react-native/docs/text.html#props)).

## Next

1. **[Custom Styles](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/CustomStyles.md)**.
2. Atomic Types (To do).
2. **[Atomic Types](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/AtomicTypes.md)**.
12 changes: 12 additions & 0 deletions sample/__tests__/getBlocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ describe('return specific component based on type', () => {
expect(customBlockHandler.mock.calls[0][1].customBlockHandler).toBe(customBlockHandler);
expect(result[0]).toBe(myCustomComponent);
});

test('pass text props to <Text />', () => {
const contentState = {
blocks: [
{ type: 'blockquote', text: 'My text' },
{ type: 'unstyled', text: 'My text' },
],
};
const result = getBlocks({ contentState, textProps: { selectable: true } });
expect(result[0].props.children[1].props.textProps.selectable).toBe(true);
expect(result[1].props.children[1].props.textProps.selectable).toBe(true);
});
});

describe('ViewAfterList', () => {
Expand Down
3 changes: 3 additions & 0 deletions sample/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export default function App(): any {
customStyles,
atomicHandler,
depthMargin: 32,
textProps: {
selectable: true,
},
};
const blocks = getRNDraftJSBlocks(params);

Expand Down
2 changes: 1 addition & 1 deletion src/components/BlockQuote.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

import DraftJsText from '../components/DraftJsText';

import type { BlockQuotePropsType } from './defaultProps';
import type { BlockQuotePropsType } from './types';

const styles = StyleSheet.create({
blockquoteContainer: {
Expand Down
4 changes: 3 additions & 1 deletion src/components/DraftJsText.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Text } from 'react-native';
import loadAttributes from '../loadAttributes';

import defaultStyles from './defaultStyles';
import type { DraftJsTextPropsType } from './defaultProps';
import type { DraftJsTextPropsType } from './types';

const DraftJsText = (props: DraftJsTextPropsType): any => {
let textElements = props.text;
Expand All @@ -25,6 +25,7 @@ const DraftJsText = (props: DraftJsTextPropsType): any => {
entityRanges: props.entityRanges,
entityMap: props.entityMap,
navigate: props.navigate,
textProps: props.textProps,
});

const customStyle = props.customStyles ? props.customStyles[props.type] : undefined;
Expand All @@ -33,6 +34,7 @@ const DraftJsText = (props: DraftJsTextPropsType): any => {
return (
<Text
style={[defaultStyles[props.type], textAlignStyle, customStyle]}
{...props.textProps}
>{textElements}
</Text>);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/OrderedListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import DraftJsText from '../components/DraftJsText';

import type { OrderedListItemPropsType } from './defaultProps';
import type { OrderedListItemPropsType } from './types';

const styles = StyleSheet.create({
orderedListItemContainer: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
StyleSheet,
} from 'react-native';

import type { TextStyledPropsType } from './defaultProps';
import type { TextStyledPropsType } from './types';

const styles = StyleSheet.flatten({
bold: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/UnorderedListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

import DraftJsText from '../components/DraftJsText';

import type { UnorderedListItemPropsType } from './defaultProps';
import type { UnorderedListItemPropsType } from './types';

const styles = StyleSheet.create({
unorderedListItemContainer: {
Expand Down
8 changes: 6 additions & 2 deletions src/components/defaultProps.js → src/components/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type BlockQuotePropsType = {
inlineStyles: Array<Object>,
entityRanges: Array<Object>,
entityMap: Object,
textProps: ?Object,
};

export type DraftJsTextPropsType = {
Expand All @@ -25,6 +26,7 @@ export type DraftJsTextPropsType = {
entityRanges: Array<Object>,
entityMap: Object,
navigate?: Function,
textProps: ?Object,
};

export type OrderedListItemPropsType = {
Expand All @@ -38,7 +40,8 @@ export type OrderedListItemPropsType = {
counter: number,
separator?: string,
depth: number,
defaultMarginLeft: number
defaultMarginLeft: number,
textProps: ?Object,
};

export type UnorderedListItemPropsType = {
Expand All @@ -50,7 +53,8 @@ export type UnorderedListItemPropsType = {
entityRanges: Array<Object>,
entityMap: Object,
depth: number,
defaultMarginLeft: number
defaultMarginLeft: number,
textProps: ?Object,
};

export type TextStyledPropsType = {
Expand Down
7 changes: 7 additions & 0 deletions src/getBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ParamsType = {
orderedListSeparator?: string,
customBlockHandler?: (Object, ParamsType) => any,
depthMargin?: number,
textProps: ?Object,
};

export const ViewAfterList = (props: Object): React$Element<*> => (
Expand All @@ -43,6 +44,8 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
atomicHandler,
} = params;

const textProps = params.textProps || {};

if (!contentState.blocks) {
return null;
}
Expand Down Expand Up @@ -121,6 +124,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
entityMap={contentState.entityMap}
customStyles={customStyles}
navigate={navigate}
textProps={textProps}
/>
</View>
);
Expand Down Expand Up @@ -153,6 +157,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
entityMap={contentState.entityMap}
customStyles={customStyles}
navigate={navigate}
textProps={textProps}
/>
</View>
);
Expand All @@ -170,6 +175,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
customStyles={customStyles}
navigate={navigate}
defaultMarginLeft={depthMargin}
textProps={textProps}
/>
</View>
);
Expand Down Expand Up @@ -208,6 +214,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
customStyles={customStyles}
navigate={navigate}
defaultMarginLeft={depthMargin}
textProps={textProps}
/>
</View>
);
Expand Down
14 changes: 11 additions & 3 deletions src/loadAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ParamsType = {
entityRanges: Array<Object>,
entityMap: Object,
navigate?: Function,
textProps: ?Object,
};

const loadAttributes = (params: ParamsType): any => {
Expand All @@ -42,6 +43,7 @@ const loadAttributes = (params: ParamsType): any => {
entityRanges,
entityMap,
navigate,
textProps,
} = params;

const defaultNavigationFn = (url: string) => { Linking.openURL(url); };
Expand All @@ -54,7 +56,12 @@ const loadAttributes = (params: ParamsType): any => {
const attrs = flatAttributesList(attributes);

if (attrs[0].offset > 0) {
elementList.push(<Text key={generateKey()}>{substring(text, 0, attrs[0].offset)}</Text>);
const element = (
<Text key={generateKey()} {...textProps}>
{substring(text, 0, attrs[0].offset)}
</Text>
);
elementList.push(element);
}

attrs.forEach((item: Object, index: number) => {
Expand All @@ -64,7 +71,7 @@ const loadAttributes = (params: ParamsType): any => {
const subText = substring(text, offset, item.offset);

if (subText.length) {
elementList.push(<Text key={generateKey()}>{subText}</Text>);
elementList.push(<Text key={generateKey()} {...textProps}>{subText}</Text>);
}
}

Expand All @@ -74,6 +81,7 @@ const loadAttributes = (params: ParamsType): any => {
type: itemType,
text: substring(text, item.offset, item.offset + item.length),
customStyles,
textProps,
});

const itemOnPress = getItemOnPress(item, entityMap, navigateFunction);
Expand All @@ -89,7 +97,7 @@ const loadAttributes = (params: ParamsType): any => {
const subText = substring(text, offset, length(text));

if (subText.length) {
elementList.push(<Text key={generateKey()}>{subText}</Text>);
elementList.push(<Text key={generateKey()} {...textProps}>{subText}</Text>);
}
} else {
elementList.push(text);
Expand Down