Skip to content

Commit 25f33fc

Browse files
raphaelporCanedo
authored andcommitted
Add support for Text Props (#37)
* Add support for RN Text props * Add textProps to sample * Update documentation * Add tests for text props
1 parent d4655c2 commit 25f33fc

11 files changed

+72
-11
lines changed

docs/GetStarted.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,32 @@ type contentState: {
4343

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

46+
## Props
47+
48+
- **contentState**: the Draft.js model with `blocks` and `entityMap`.
49+
```js
50+
// Flow type for contentState
51+
type contentState: {
52+
blocks: Array<Object>,
53+
entityMap: Object,
54+
};
55+
```
56+
57+
- **customStyles**: Object with your custom styles. ([Documentation](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/CustomStyles.md)).
58+
59+
- **atomicHandler**: Function to handle atomic types ([Documentation](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/AtomicTypes.md)).
60+
61+
- **navigate** (optional): Function to be called when `onPress` is fired.
62+
63+
- **orderedListSeparator** (optional): String to be rendered after each number of the ordered lists.
64+
65+
- **customBlockHandler** (optional): Default element to be rendered when some type is not knew.
66+
67+
- **depthMargin** (optional): Margin left for each level of depth lists.
68+
69+
- **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)).
70+
4671
## Next
4772

4873
1. **[Custom Styles](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/CustomStyles.md)**.
49-
2. Atomic Types (To do).
74+
2. **[Atomic Types](https://github.com/globocom/react-native-draftjs-render/blob/master/docs/AtomicTypes.md)**.

sample/__tests__/getBlocks.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ describe('return specific component based on type', () => {
248248
expect(customBlockHandler.mock.calls[0][1].customBlockHandler).toBe(customBlockHandler);
249249
expect(result[0]).toBe(myCustomComponent);
250250
});
251+
252+
test('pass text props to <Text />', () => {
253+
const contentState = {
254+
blocks: [
255+
{ type: 'blockquote', text: 'My text' },
256+
{ type: 'unstyled', text: 'My text' },
257+
],
258+
};
259+
const result = getBlocks({ contentState, textProps: { selectable: true } });
260+
expect(result[0].props.children[1].props.textProps.selectable).toBe(true);
261+
expect(result[1].props.children[1].props.textProps.selectable).toBe(true);
262+
});
251263
});
252264

253265
describe('ViewAfterList', () => {

sample/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ export default function App(): any {
113113
customStyles,
114114
atomicHandler,
115115
depthMargin: 32,
116+
textProps: {
117+
selectable: true,
118+
},
116119
};
117120
const blocks = getRNDraftJSBlocks(params);
118121

src/components/BlockQuote.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414

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

17-
import type { BlockQuotePropsType } from './defaultProps';
17+
import type { BlockQuotePropsType } from './types';
1818

1919
const styles = StyleSheet.create({
2020
blockquoteContainer: {

src/components/DraftJsText.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Text } from 'react-native';
1212
import loadAttributes from '../loadAttributes';
1313

1414
import defaultStyles from './defaultStyles';
15-
import type { DraftJsTextPropsType } from './defaultProps';
15+
import type { DraftJsTextPropsType } from './types';
1616

1717
const DraftJsText = (props: DraftJsTextPropsType): any => {
1818
let textElements = props.text;
@@ -25,6 +25,7 @@ const DraftJsText = (props: DraftJsTextPropsType): any => {
2525
entityRanges: props.entityRanges,
2626
entityMap: props.entityMap,
2727
navigate: props.navigate,
28+
textProps: props.textProps,
2829
});
2930

3031
const customStyle = props.customStyles ? props.customStyles[props.type] : undefined;
@@ -33,6 +34,7 @@ const DraftJsText = (props: DraftJsTextPropsType): any => {
3334
return (
3435
<Text
3536
style={[defaultStyles[props.type], textAlignStyle, customStyle]}
37+
{...props.textProps}
3638
>{textElements}
3739
</Text>);
3840
}

src/components/OrderedListItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515

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

18-
import type { OrderedListItemPropsType } from './defaultProps';
18+
import type { OrderedListItemPropsType } from './types';
1919

2020
const styles = StyleSheet.create({
2121
orderedListItemContainer: {

src/components/TextStyled.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
StyleSheet,
1313
} from 'react-native';
1414

15-
import type { TextStyledPropsType } from './defaultProps';
15+
import type { TextStyledPropsType } from './types';
1616

1717
const styles = StyleSheet.flatten({
1818
bold: {

src/components/UnorderedListItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414

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

17-
import type { UnorderedListItemPropsType } from './defaultProps';
17+
import type { UnorderedListItemPropsType } from './types';
1818

1919
const styles = StyleSheet.create({
2020
unorderedListItemContainer: {

src/components/defaultProps.js renamed to src/components/types.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type BlockQuotePropsType = {
1414
inlineStyles: Array<Object>,
1515
entityRanges: Array<Object>,
1616
entityMap: Object,
17+
textProps: ?Object,
1718
};
1819

1920
export type DraftJsTextPropsType = {
@@ -25,6 +26,7 @@ export type DraftJsTextPropsType = {
2526
entityRanges: Array<Object>,
2627
entityMap: Object,
2728
navigate?: Function,
29+
textProps: ?Object,
2830
};
2931

3032
export type OrderedListItemPropsType = {
@@ -38,7 +40,8 @@ export type OrderedListItemPropsType = {
3840
counter: number,
3941
separator?: string,
4042
depth: number,
41-
defaultMarginLeft: number
43+
defaultMarginLeft: number,
44+
textProps: ?Object,
4245
};
4346

4447
export type UnorderedListItemPropsType = {
@@ -50,7 +53,8 @@ export type UnorderedListItemPropsType = {
5053
entityRanges: Array<Object>,
5154
entityMap: Object,
5255
depth: number,
53-
defaultMarginLeft: number
56+
defaultMarginLeft: number,
57+
textProps: ?Object,
5458
};
5559

5660
export type TextStyledPropsType = {

src/getBlocks.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type ParamsType = {
2626
orderedListSeparator?: string,
2727
customBlockHandler?: (Object, ParamsType) => any,
2828
depthMargin?: number,
29+
textProps: ?Object,
2930
};
3031

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

47+
const textProps = params.textProps || {};
48+
4649
if (!contentState.blocks) {
4750
return null;
4851
}
@@ -121,6 +124,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
121124
entityMap={contentState.entityMap}
122125
customStyles={customStyles}
123126
navigate={navigate}
127+
textProps={textProps}
124128
/>
125129
</View>
126130
);
@@ -153,6 +157,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
153157
entityMap={contentState.entityMap}
154158
customStyles={customStyles}
155159
navigate={navigate}
160+
textProps={textProps}
156161
/>
157162
</View>
158163
);
@@ -170,6 +175,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
170175
customStyles={customStyles}
171176
navigate={navigate}
172177
defaultMarginLeft={depthMargin}
178+
textProps={textProps}
173179
/>
174180
</View>
175181
);
@@ -208,6 +214,7 @@ const getBlocks = (params: ParamsType): ?Array<React$Element<*>> => {
208214
customStyles={customStyles}
209215
navigate={navigate}
210216
defaultMarginLeft={depthMargin}
217+
textProps={textProps}
211218
/>
212219
</View>
213220
);

src/loadAttributes.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ParamsType = {
3232
entityRanges: Array<Object>,
3333
entityMap: Object,
3434
navigate?: Function,
35+
textProps: ?Object,
3536
};
3637

3738
const loadAttributes = (params: ParamsType): any => {
@@ -42,6 +43,7 @@ const loadAttributes = (params: ParamsType): any => {
4243
entityRanges,
4344
entityMap,
4445
navigate,
46+
textProps,
4547
} = params;
4648

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

5658
if (attrs[0].offset > 0) {
57-
elementList.push(<Text key={generateKey()}>{substring(text, 0, attrs[0].offset)}</Text>);
59+
const element = (
60+
<Text key={generateKey()} {...textProps}>
61+
{substring(text, 0, attrs[0].offset)}
62+
</Text>
63+
);
64+
elementList.push(element);
5865
}
5966

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

6673
if (subText.length) {
67-
elementList.push(<Text key={generateKey()}>{subText}</Text>);
74+
elementList.push(<Text key={generateKey()} {...textProps}>{subText}</Text>);
6875
}
6976
}
7077

@@ -74,6 +81,7 @@ const loadAttributes = (params: ParamsType): any => {
7481
type: itemType,
7582
text: substring(text, item.offset, item.offset + item.length),
7683
customStyles,
84+
textProps,
7785
});
7886

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

9199
if (subText.length) {
92-
elementList.push(<Text key={generateKey()}>{subText}</Text>);
100+
elementList.push(<Text key={generateKey()} {...textProps}>{subText}</Text>);
93101
}
94102
} else {
95103
elementList.push(text);

0 commit comments

Comments
 (0)