RichText Editor for React-Native
npm install react-native-cn-richtext-editor
npm i react-native-cn-richtext-editor
`
#### Install using yarn:
`
yarn add react-native-cn-richtext-editor
`
$3
Here is a simple overview of our components usage.
` jsx
import React, { Component } from 'react';
import { View, StyleSheet, Keyboard
, TouchableWithoutFeedback, Text
, KeyboardAvoidingView } from 'react-native';
import CNRichTextEditor , { CNToolbar, getInitialObject , getDefaultStyles } from "react-native-cn-richtext-editor";
const defaultStyles = getDefaultStyles();
class App extends Component {
constructor(props) {
super(props);
this.state = {
selectedTag : 'body',
selectedStyles : [],
value: [getInitialObject()]
};
this.editor = null;
}
onStyleKeyPress = (toolType) => {
this.editor.applyToolbar(toolType);
}
onSelectedTagChanged = (tag) => {
this.setState({
selectedTag: tag
})
}
onSelectedStyleChanged = (styles) => {
this.setState({
selectedStyles: styles,
})
}
onValueChanged = (value) => {
this.setState({
value: value
});
}
render() {
return (
behavior="padding"
enabled
keyboardVerticalOffset={0}
style={{
flex: 1,
paddingTop: 20,
backgroundColor:'#eee',
flexDirection: 'column',
justifyContent: 'flex-end',
}}
>
ref={input => this.editor = input}
onSelectedTagChanged={this.onSelectedTagChanged}
onSelectedStyleChanged={this.onSelectedStyleChanged}
value={this.state.value}
style={{ backgroundColor : '#fff'}}
styleList={defaultStyles}
onValueChanged={this.onValueChanged}
/>
minHeight: 35
}}>
style={{
height: 35,
}}
iconSetContainerStyle={{
flexGrow: 1,
justifyContent: 'space-evenly',
alignItems: 'center',
}}
size={30}
iconSet={[
{
type: 'tool',
iconArray: [{
toolTypeText: 'image',
iconComponent:
image
}]
},
{
type: 'tool',
iconArray: [{
toolTypeText: 'bold',
buttonTypes: 'style',
iconComponent:
bold
}]
},
{
type: 'seperator'
},
{
type: 'tool',
iconArray: [
{
toolTypeText: 'body',
buttonTypes: 'tag',
iconComponent:
body
},
]
},
{
type: 'tool',
iconArray: [
{
toolTypeText: 'ul',
buttonTypes: 'tag',
iconComponent:
ul
}
]
},
{
type: 'tool',
iconArray: [
{
toolTypeText: 'ol',
buttonTypes: 'tag',
iconComponent:
ol
}
]
},
]}
selectedTag={this.state.selectedTag}
selectedStyles={this.state.selectedStyles}
onStyleKeyPress={this.onStyleKeyPress}
/>
);
}
}
var styles = StyleSheet.create({
main: {
flex: 1,
marginTop: 10,
paddingLeft: 30,
paddingRight: 30,
paddingBottom: 1,
alignItems: 'stretch',
},
toolbarButton: {
fontSize: 20,
width: 28,
height: 28,
textAlign: 'center'
},
italicButton: {
fontStyle: 'italic'
},
boldButton: {
fontWeight: 'bold'
},
underlineButton: {
textDecorationLine: 'underline'
},
lineThroughButton: {
textDecorationLine: 'line-through'
},
});
export default App;
`
More Advanced TextEditor
You need to put more effort :) to use more advanced features of CNRichTextEditor such as:
- Image Uploading
- Highlighting Text
- Change Text Color
Actually we did not implement 'Toolbar buttons and menus' and 'Image Uploading Process' because it totally depends on using expo or pure react-native and also what other packages you prefer to use.
To see an example of how to implement more advanced feature of this editor please check this Link.
Also be noticed that this example is writen with expo and required 'react-native-popup-menu' package.
API
$3
#### Props
| Name | Description | Required |
| ------ | ----------- | ---- |
| onSelectedTagChanged | this event triggers when selected tag of editor is changed. | No |
| onSelectedStyleChanged | this event triggers when selected style of editor is changed. | No |
| onValueChanged | this event triggers when value of editor is changed. | No |
| onRemoveImage | this event triggers when an image is removed. Callback param in the form { url, id }. | No |
| value | an array object which keeps value of the editor | Yes |
| styleList | an object consist of styles name and values (use getDefaultStyles function) | Yes |
| ImageComponent | a React component (class or functional) which will be used to render images. Will be passed style and source props. | No |
| style | Styles applied to the outermost component. | No |
| textInputStyle | TextInput style | No |
| contentContainerStyle | Styles applied to the scrollview content. | No |
| onFocus | Callback that is called when one of text inputs are focused. | No |
| onBlur | Callback that is called when one of text inputs are blurred. | No |
| placeholder | The string that will be rendered before text input has been entered. | No |
| textInputProps | An object containing additional props to be passed to the TextInput component| No |
#### Instance methods
| Name | Params | Description |
| ------ | ---- | ----------- |
| applyToolbar | toolType | Apply the given transformation to selected text. |
| insertImage | uri, id?, height?, width? | Insert the provided image where cursor is positionned. |
| focus | | Focus to the last TextInput` |