Skip to content

Commit c3ecb49

Browse files
committed
Update unsafe components
* Have to use createElement with unsafeCoerce due to purescript-contrib/purescript-react#151 * Change createNoChild to use createLeafElement
1 parent 4cee453 commit c3ecb49

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

src/ReactNative/Unsafe/Components.purs

+50-38
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module ReactNative.Unsafe.Components (
3535
, webViewU
3636
) where
3737

38-
import React (ReactClass, ReactElement, createElement)
38+
import React (Children, ReactClass, ReactElement, createElement, createLeafElement)
39+
import Unsafe.Coerce (unsafeCoerce)
3940

4041
foreign import activityIndicatorClass :: forall props. ReactClass props
4142
foreign import buttonClass :: forall props. ReactClass props
@@ -72,141 +73,152 @@ foreign import viewClass :: forall props. ReactClass props
7273
foreign import viewPagerAndroidClass :: forall props. ReactClass props
7374
foreign import webViewClass :: forall props. ReactClass props
7475

75-
createNoChild :: forall props. ReactClass props -> props -> ReactElement
76-
createNoChild c p = createElement c p []
77-
7876
-- | Create a [Text](https://facebook.github.io/react-native/docs/text.html) component unsafely
7977
textU :: forall props. props -> Array ReactElement -> ReactElement
80-
textU = createElement textClass
78+
textU = createElementU textClass
8179

8280
-- | Create a [View](https://facebook.github.io/react-native/docs/view.html) component unsafely
8381
viewU :: forall props. props -> Array ReactElement -> ReactElement
84-
viewU = createElement viewClass
82+
viewU = createElementU viewClass
8583

8684
-- | Create a [SafeAreaView](https://facebook.github.io/react-native/docs/safeareaview.html) component unsafely
8785
safeAreaViewU :: forall props. props -> Array ReactElement -> ReactElement
88-
safeAreaViewU = createElement viewClass
86+
safeAreaViewU = createElementU viewClass
8987

9088
-- | Create an [Image](https://facebook.github.io/react-native/docs/image.html) component unsafely
9189
imageU :: forall props. props -> Array ReactElement -> ReactElement
92-
imageU = createElement imageClass
90+
imageU = createElementU imageClass
9391

9492
-- | Create a [ListView](https://facebook.github.io/react-native/docs/listview.html) component unsafely
9593
listViewU :: forall props. props -> ReactElement
96-
listViewU = createNoChild listViewClass
94+
listViewU = createLeafElementU listViewClass
9795

9896
-- | Create a [ToolbarAndroid](https://facebook.github.io/react-native/docs/toolbarandroid.html) component unsafely
9997
toolbarAndroidU :: forall props. props -> Array ReactElement -> ReactElement
100-
toolbarAndroidU = createElement toolbarAndroidClass
98+
toolbarAndroidU = createElementU toolbarAndroidClass
10199

102100
-- | Create an [ActivityIndicator](https://facebook.github.io/react-native/docs/activityindicator.html) component unsafely
103101
activityIndicatorU :: forall props. props -> ReactElement
104-
activityIndicatorU = createNoChild activityIndicatorClass
102+
activityIndicatorU = createLeafElementU activityIndicatorClass
105103

106104
-- | Create a [TouchableNativeFeedback](https://facebook.github.io/react-native/docs/touchablenativefeedback.html) component unsafely
107105
touchableNativeFeedbackU :: forall props. props -> ReactElement -> ReactElement
108-
touchableNativeFeedbackU p c = createElement touchableNativeFeedbackClass p [c]
106+
touchableNativeFeedbackU p c = createElementU touchableNativeFeedbackClass p [c]
109107

110108
-- | Create a [TouchableWithoutFeedback](https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html) component unsafely
111109
touchableWithoutFeedbackU :: forall props. props -> ReactElement -> ReactElement
112-
touchableWithoutFeedbackU p c = createElement touchableWithoutFeedbackClass p [c]
110+
touchableWithoutFeedbackU p c = createElementU touchableWithoutFeedbackClass p [c]
113111

114112
-- | Create a [TouchableHighlight](http://facebook.github.io/react-native/docs/touchablehighlight.html#touchablehighlight) component unsafely
115113
touchableHighlightU :: forall props. props -> ReactElement -> ReactElement
116-
touchableHighlightU p c = createElement touchableHighlightClass p [c]
114+
touchableHighlightU p c = createElementU touchableHighlightClass p [c]
117115

118116
-- | Create a [TouchableOpacity](https://facebook.github.io/react-native/docs/touchableopacity.html) component unsafely
119117
touchableOpacityU :: forall props. props -> ReactElement -> ReactElement
120-
touchableOpacityU p c = createElement touchableOpacityClass p [c]
118+
touchableOpacityU p c = createElementU touchableOpacityClass p [c]
121119

122120
-- | Create a [ScrollView](https://facebook.github.io/react-native/docs/scrollview.html) component unsafely
123121
scrollViewU :: forall props. props -> Array ReactElement -> ReactElement
124-
scrollViewU = createElement scrollViewClass
122+
scrollViewU = createElementU scrollViewClass
125123

126124
-- | Create an [RefreshControl](https://facebook.github.io/react-native/docs/refreshcontrol.html) component unsafely
127125
refreshControlU :: forall props. props -> ReactElement
128-
refreshControlU = createNoChild refreshControlClass
126+
refreshControlU = createLeafElementU refreshControlClass
129127

130128
-- | Create a [Switch](https://facebook.github.io/react-native/docs/switch.html) component unsafely
131129
switchU :: forall props. props -> ReactElement
132-
switchU = createNoChild switchClass
130+
switchU = createLeafElementU switchClass
133131

134132
-- | Create a [DrawerLayoutAndroid](https://facebook.github.io/react-native/docs/drawerlayoutandroid.html) component unsafely
135133
drawerLayoutAndroidU :: forall props. props -> Array ReactElement -> ReactElement
136-
drawerLayoutAndroidU = createElement drawerLayoutAndroidClass
134+
drawerLayoutAndroidU = createElementU drawerLayoutAndroidClass
137135

138136
-- | Create a [Button](https://facebook.github.io/react-native/docs/button.html) component unsafely
139137
buttonU :: forall props. props -> ReactElement
140-
buttonU = createNoChild buttonClass
138+
buttonU = createLeafElementU buttonClass
141139

142140
-- | Create a [Picker](https://facebook.github.io/react-native/docs/picker.html) component unsafely
143141
pickerU :: forall props. props -> Array ReactElement -> ReactElement
144-
pickerU = createElement pickerClass
142+
pickerU = createElementU pickerClass
145143

146144
-- | Create a [Picker.Item](https://facebook.github.io/react-native/docs/picker.html) component unsafely
147145
pickerItemU :: forall props. props -> ReactElement
148-
pickerItemU = createNoChild pickerItemClass
146+
pickerItemU = createLeafElementU pickerItemClass
149147

150148
-- | Create a [Slider](https://facebook.github.io/react-native/docs/activityindicator.html) component unsafely
151149
sliderU :: forall props. props -> ReactElement
152-
sliderU = createNoChild sliderClass
150+
sliderU = createLeafElementU sliderClass
153151

154152
-- | Create a [NavigatorIOS](https://facebook.github.io/react-native/docs/navigatorios.html) component unsafely
155153
navigatorIOSU :: forall props. props -> ReactElement
156-
navigatorIOSU = createNoChild navigatorIOSClass
154+
navigatorIOSU = createLeafElementU navigatorIOSClass
157155

158156
-- | Create a [TextInput](https://facebook.github.io/react-native/docs/textinput.html) component unsafely
159157
textInputU :: forall props. props -> ReactElement
160-
textInputU = createNoChild textInputClass
158+
textInputU = createLeafElementU textInputClass
161159

162160
-- | Create a [DatePickerIOS](https://facebook.github.io/react-native/docs/datepickerios.html) component unsafely
163161
datePickerIOSU :: forall props. props -> ReactElement
164-
datePickerIOSU = createNoChild datePickerIOSClass
162+
datePickerIOSU = createLeafElementU datePickerIOSClass
165163

166164
-- | Create a [KeyboardAvoidingView](https://facebook.github.io/react-native/docs/keyboardavoidingview.html) component unsafely
167165
keyboardAvoidingViewU :: forall props. props -> Array ReactElement -> ReactElement
168-
keyboardAvoidingViewU = createElement keyboardAvoidingViewClass
166+
keyboardAvoidingViewU = createElementU keyboardAvoidingViewClass
169167

170168
-- | Create a [MapView](https://facebook.github.io/react-native/docs/mapview.html) component unsafely
171169
mapViewU :: forall props. props -> Array ReactElement -> ReactElement
172-
mapViewU = createElement mapViewClass
170+
mapViewU = createElementU mapViewClass
173171

174172
-- | Create a [Modal](https://facebook.github.io/react-native/docs/modal.html) component unsafely
175173
modalU :: forall props. props -> Array ReactElement -> ReactElement
176-
modalU = createElement modalClass
174+
modalU = createElementU modalClass
177175

178176
-- | Create a [ProgressBarAndroid](https://facebook.github.io/react-native/docs/progressbarandroid.html) component unsafely
179177
progressBarAndroidU :: forall props. props -> ReactElement
180-
progressBarAndroidU = createNoChild progressBarAndroidClass
178+
progressBarAndroidU = createLeafElementU progressBarAndroidClass
181179

182180
-- | Create a [ProgressViewIOS](https://facebook.github.io/react-native/docs/progressviewios.html) component unsafely
183181
progressViewIOSU :: forall props. props -> ReactElement
184-
progressViewIOSU = createNoChild progressViewIOSClass
182+
progressViewIOSU = createLeafElementU progressViewIOSClass
185183

186184
-- | Create a [SegmentedControlIOS](https://facebook.github.io/react-native/docs/segmentedcontrolios.html) component unsafely
187185
segmentedControlIOSU :: forall props. props -> ReactElement
188-
segmentedControlIOSU = createNoChild segmentedControlIOSClass
186+
segmentedControlIOSU = createLeafElementU segmentedControlIOSClass
189187

190188
-- | Create a [StatusBar](https://facebook.github.io/react-native/docs/statusbar.html) component unsafely
191189
statusBarU :: forall props. props -> ReactElement
192-
statusBarU = createNoChild statusBarClass
190+
statusBarU = createLeafElementU statusBarClass
193191

194192
-- | Create a [SnapshotViewIOS](https://facebook.github.io/react-native/docs/snapshotviewios.html) component unsafely
195193
snapshotViewIOSU :: forall props. props -> ReactElement
196-
snapshotViewIOSU = createNoChild snapshotViewIOSClass
194+
snapshotViewIOSU = createLeafElementU snapshotViewIOSClass
197195

198196
-- | Create a [TabBarIOS](https://facebook.github.io/react-native/docs/tabbarios.html) component unsafely
199197
tabBarIOSU :: forall props. props -> Array ReactElement -> ReactElement
200-
tabBarIOSU = createElement tabBarIOSClass
198+
tabBarIOSU = createElementU tabBarIOSClass
201199

202200
-- | Create a [TabBarIOS.Item](https://facebook.github.io/react-native/docs/tabbarios-item.html) component unsafely
203201
tabBarIOSItemU :: forall props. props -> Array ReactElement -> ReactElement
204-
tabBarIOSItemU = createElement tabBarIOSItemClass
202+
tabBarIOSItemU = createElementU tabBarIOSItemClass
205203

206204
-- | Create a [ViewPagerAndroid](https://facebook.github.io/react-native/docs/viewpagerandroid.html) component unsafely
207205
viewPagerAndroidU :: forall props. props -> Array ReactElement -> ReactElement
208-
viewPagerAndroidU = createElement viewPagerAndroidClass
206+
viewPagerAndroidU = createElementU viewPagerAndroidClass
209207

210208
-- | Create a [WebView](https://facebook.github.io/react-native/docs/webview.html) component unsafely
211209
webViewU :: forall props. props -> ReactElement
212-
webViewU = createNoChild webViewClass
210+
webViewU = createLeafElementU webViewClass
211+
212+
-- Unsafe createElement
213+
214+
createElementU :: forall props. ReactClass props -> props -> Array ReactElement -> ReactElement
215+
createElementU c p =
216+
createElement
217+
(unsafeCoerce c :: ReactClass { children :: Children })
218+
(unsafeCoerce p :: {})
219+
220+
createLeafElementU :: forall props. ReactClass props -> props -> ReactElement
221+
createLeafElementU c p =
222+
createLeafElement
223+
(unsafeCoerce c :: ReactClass {})
224+
(unsafeCoerce p :: {})

0 commit comments

Comments
 (0)