Skip to content

Commit 20f2bfc

Browse files
Martín BigioMorgan Pretty
authored andcommitted
Add support for drawableLeft and drawablePadding props
Summary: Add support inlining images into text inputs. For now this is only available on Android. Reviewed By: andreicoman11 Differential Revision: D3470805 fbshipit-source-id: 14db05ec4d5af549bf314b903654314f39bf73ea
1 parent c6addce commit 20f2bfc

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

Examples/UIExplorer/TextInputExample.android.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,19 +412,19 @@ exports.examples = [
412412
render: function() {
413413
return (
414414
<View>
415-
<TextInput
415+
<TextInput
416416
style={[styles.singleLine, {fontFamily: 'sans-serif'}]}
417417
placeholder="Custom fonts like Sans-Serif are supported"
418418
/>
419-
<TextInput
419+
<TextInput
420420
style={[styles.singleLine, {fontFamily: 'sans-serif', fontWeight: 'bold'}]}
421421
placeholder="Sans-Serif bold"
422422
/>
423-
<TextInput
423+
<TextInput
424424
style={[styles.singleLine, {fontFamily: 'sans-serif', fontStyle: 'italic'}]}
425425
placeholder="Sans-Serif italic"
426426
/>
427-
<TextInput
427+
<TextInput
428428
style={[styles.singleLine, {fontFamily: 'serif'}]}
429429
placeholder="Serif"
430430
/>
@@ -570,4 +570,28 @@ exports.examples = [
570570
return <View>{examples}{types}</View>;
571571
}
572572
},
573+
{
574+
title: 'Inline Images',
575+
render: function() {
576+
return (
577+
<View>
578+
<TextInput
579+
inlineImageLeft="ic_menu_black_24dp"
580+
placeholder="This has drawableLeft set"
581+
style={styles.singleLine}
582+
/>
583+
<TextInput
584+
inlineImageLeft="ic_menu_black_24dp"
585+
inlineImagePadding={30}
586+
placeholder="This has drawableLeft and drawablePadding set"
587+
style={styles.singleLine}
588+
/>
589+
<TextInput
590+
placeholder="This does not have drawable props set"
591+
style={styles.singleLine}
592+
/>
593+
</View>
594+
);
595+
}
596+
},
573597
];

Libraries/Components/TextInput/TextInput.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,18 @@ const TextInput = React.createClass({
419419
* @platform android
420420
*/
421421
underlineColorAndroid: ColorPropType,
422+
423+
/**
424+
* If defined, the provided image resource will be rendered on the left.
425+
* @platform android
426+
*/
427+
inlineImageLeft: PropTypes.string,
428+
429+
/**
430+
* Padding between the inline image, if any, and the text input itself.
431+
* @platform android
432+
*/
433+
inlineImagePadding: PropTypes.number,
422434
},
423435

424436
/**
@@ -638,6 +650,8 @@ const TextInput = React.createClass({
638650
selectionColor={this.props.selectionColor}
639651
text={this._getText()}
640652
underlineColorAndroid={this.props.underlineColorAndroid}
653+
inlineImageLeft={this.props.inlineImageLeft}
654+
inlineImagePadding={this.props.inlineImagePadding}
641655
children={children}
642656
editable={this.props.editable}
643657
selectTextOnFocus={this.props.selectTextOnFocus}

ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ android_library(
1212
react_native_target('java/com/facebook/react/modules/core:core'),
1313
react_native_target('java/com/facebook/react/uimanager/annotations:annotations'),
1414
react_native_target('java/com/facebook/react/uimanager:uimanager'),
15+
react_native_target('java/com/facebook/react/views/imagehelper:imagehelper'),
1516
react_native_target('java/com/facebook/react/views/text:text'),
1617
react_native_target('java/com/facebook/react/views/view:view'),
1718
],

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.facebook.react.uimanager.ViewProps;
4444
import com.facebook.react.uimanager.annotations.ReactProp;
4545
import com.facebook.react.uimanager.events.EventDispatcher;
46+
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
4647
import com.facebook.react.views.text.DefaultStyleValuesUtil;
4748
import com.facebook.react.views.text.ReactTextUpdate;
4849
import com.facebook.react.views.text.TextInlineImageSpan;
@@ -323,6 +324,17 @@ public void setTextAlignVertical(ReactEditText view, @Nullable String textAlignV
323324
}
324325
}
325326

327+
@ReactProp(name = "inlineImageLeft")
328+
public void setInlineImageLeft(ReactEditText view, @Nullable String resource) {
329+
int id = ResourceDrawableIdHelper.getInstance().getResourceDrawableId(view.getContext(), resource);
330+
view.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
331+
}
332+
333+
@ReactProp(name = "inlineImagePadding")
334+
public void setInlineImagePadding(ReactEditText view, int padding) {
335+
view.setCompoundDrawablePadding(padding);
336+
}
337+
326338
@ReactProp(name = "editable", defaultBoolean = true)
327339
public void setEditable(ReactEditText view, boolean editable) {
328340
view.setEnabled(editable);

0 commit comments

Comments
 (0)