Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 8385760

Browse files
committed
update example project with a toolbar
1 parent 4dee4c2 commit 8385760

File tree

1 file changed

+98
-10
lines changed
  • ReactNativeDraftjsExample

1 file changed

+98
-10
lines changed

ReactNativeDraftjsExample/App.js

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,121 @@
66
* @flow
77
*/
88

9-
import React, { Fragment } from "react";
9+
import React, { useState } from "react";
1010
import {
1111
SafeAreaView,
1212
StyleSheet,
13-
ScrollView,
13+
TouchableOpacity,
1414
View,
15-
Text,
16-
StatusBar
15+
Text
1716
} from "react-native";
1817
import KeyboardSpacer from "react-native-keyboard-spacer";
1918
import RNDraftView from "react-native-draftjs";
2019

20+
const ControlButton = ({ text, action, isActive }) => {
21+
return (
22+
<TouchableOpacity
23+
style={[
24+
styles.controlButtonContainer,
25+
isActive ? { backgroundColor: "gold" } : {}
26+
]}
27+
onPress={action}
28+
>
29+
<Text>{text}</Text>
30+
</TouchableOpacity>
31+
);
32+
};
33+
34+
const EditorToolBar = ({
35+
activeStyles,
36+
blockType,
37+
toggleStyle,
38+
toggleBlockType
39+
}) => {
40+
return (
41+
<View style={styles.toolbarContainer}>
42+
<ControlButton
43+
text={"B"}
44+
isActive={activeStyles.includes("BOLD")}
45+
action={() => toggleStyle("BOLD")}
46+
/>
47+
<ControlButton
48+
text={"I"}
49+
isActive={activeStyles.includes("ITALIC")}
50+
action={() => toggleStyle("ITALIC")}
51+
/>
52+
<ControlButton
53+
text={"H"}
54+
isActive={blockType === "header-one"}
55+
action={() => toggleBlockType("header-one")}
56+
/>
57+
<ControlButton
58+
text={"ul"}
59+
isActive={blockType === "unordered-list-item"}
60+
action={() => toggleBlockType("unordered-list-item")}
61+
/>
62+
<ControlButton
63+
text={"ol"}
64+
isActive={blockType === "ordered-list-item"}
65+
action={() => toggleBlockType("ordered-list-item")}
66+
/>
67+
</View>
68+
);
69+
};
70+
2171
const App = () => {
72+
const _draftRef = React.createRef();
73+
const [activeStyles, setActiveStyles] = useState([]);
74+
const [blockType, setActiveBlockType] = useState("unstyled");
75+
76+
const editorLoaded = () => {
77+
_draftRef.current && _draftRef.current.focus();
78+
};
79+
80+
const toggleStyle = style => {
81+
_draftRef.current && _draftRef.current.setStyle(style);
82+
};
83+
84+
const toggleBlockType = blockType => {
85+
_draftRef.current && _draftRef.current.setBlockType(blockType);
86+
};
87+
2288
return (
23-
<Fragment>
24-
<View style={styles.containerStyle}>
25-
<RNDraftView />
26-
<KeyboardSpacer />
27-
</View>
28-
</Fragment>
89+
<SafeAreaView style={styles.containerStyle}>
90+
<RNDraftView
91+
onEditorReady={editorLoaded}
92+
style={{ flex: 1 }}
93+
placeholder={"Some placeholder Text..."}
94+
ref={_draftRef}
95+
onStyleChanged={setActiveStyles}
96+
onBlockTypeChanged={setActiveBlockType}
97+
/>
98+
<EditorToolBar
99+
activeStyles={activeStyles}
100+
blockType={blockType}
101+
toggleStyle={toggleStyle}
102+
toggleBlockType={toggleBlockType}
103+
/>
104+
<KeyboardSpacer />
105+
</SafeAreaView>
29106
);
30107
};
31108

32109
const styles = StyleSheet.create({
33110
containerStyle: {
34111
flex: 1,
35112
marginTop: 36
113+
},
114+
toolbarContainer: {
115+
height: 56,
116+
flexDirection: "row",
117+
backgroundColor: "silver",
118+
alignItems: "center",
119+
justifyContent: "space-around"
120+
},
121+
controlButtonContainer: {
122+
padding: 8,
123+
borderRadius: 2
36124
}
37125
});
38126

0 commit comments

Comments
 (0)