Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing max numberOfLines #171

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Shows a Snackbar, dismissing any existing Snackbar first. Accepts an object with
|-----|-----------|----------------|-------------|
| `text` | `string` | Required. | The message to show. |
| `duration` | See below | `Snackbar.LENGTH_SHORT` | How long to display the Snackbar. |
| `numberOfLines` | `number` | `2` | The max number of text lines to allow before ellipsizing. |
| `textColor` | `string` or `style` | `'white'` | The color of the message text. |
| `backgroundColor` | `string` or `style` | `undefined` (dark gray) | The background color for the whole Snackbar. |
| `fontFamily` | `string` | `undefined` | [Android only] The basename of a `.ttf` font from `assets/fonts/` (see [setup guide](https://github.com/facebook/react-native/issues/25852) and [example app](/example), remember to `react-native link` after). |
Expand All @@ -79,8 +80,6 @@ Where `duration` can be one of the following (timing may vary based on device):
- `Snackbar.LENGTH_LONG` (about three seconds)
- `Snackbar.LENGTH_INDEFINITE` (stays on screen until dismissed, replaced, or action button is tapped)

Note: the `text` will ellipsize after 2 lines of text on most platforms. See [#110](https://github.com/cooperka/react-native-snackbar/issues/110) if you need to display more lines.

The optional `action` object can contain the following options:

| Key | Data type | Default value? | Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void dismiss() {
private void displaySnackbar(View view, ReadableMap options, final Callback callback) {
String text = getOptionValue(options, "text", "");
int duration = getOptionValue(options, "duration", Snackbar.LENGTH_SHORT);
int numberOfLines = getOptionValue(options, "numberOfLines", 2);
int textColor = getOptionValue(options, "textColor", Color.WHITE);
boolean rtl = getOptionValue(options, "rtl", false);
String fontFamily = getOptionValue(options, "fontFamily", null);
Expand All @@ -125,6 +126,9 @@ private void displaySnackbar(View view, ReadableMap options, final Callback call
}
View snackbarView = snackbar.getView();

TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setMaxLines(numberOfLines);

if (rtl && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
snackbarView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
snackbarView.setTextDirection(View.TEXT_DIRECTION_RTL);
Expand Down
10 changes: 10 additions & 0 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Example extends Component {
<Text style={styles.button}>Simple Snackbar - two lines</Text>
</TouchableOpacity>

<TouchableOpacity
onPress={() => Snackbar.show({
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
duration: Snackbar.LENGTH_LONG,
numberOfLines: 5,
})}
>
<Text style={styles.button}>Simple Snackbar - extra lines</Text>
</TouchableOpacity>

<TouchableOpacity
onPress={() => Snackbar.show({
text: 'Please agree to this.',
Expand Down
7 changes: 7 additions & 0 deletions ios/RNSnackBarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ - (void)setTextColor:(UIColor *)textColor {
textLabel.textColor = textColor;
}

- (void)setNumberOfLines:(int *)numberOfLines {
textLabel.numberOfLines = numberOfLines;
}

- (void)setActionText:(NSString *)actionText {
[actionButton setTitle:actionText forState:UIControlStateNormal];
}
Expand Down Expand Up @@ -246,6 +250,9 @@ - (void)show {
return;
}

NSNumber *numberOfLines = _pendingOptions[@"numberOfLines"];
self.numberOfLines = [RCTConvert int:numberOfLines] ? [RCTConvert int:numberOfLines] : 2;

id backgroundColor = _pendingOptions[@"backgroundColor"];
self.backgroundColor = backgroundColor ? [RCTConvert UIColor:backgroundColor]
: [UIColor colorWithRed:0.196078F
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Array [
Object {
"action": undefined,
"backgroundColor": undefined,
"numberOfLines": undefined,
"text": undefined,
"textColor": undefined,
},
Expand All @@ -24,6 +25,7 @@ Array [
"textColor": 4278222848,
},
"backgroundColor": undefined,
"numberOfLines": undefined,
"text": "Hello world",
"textColor": 4278190335,
},
Expand All @@ -43,6 +45,7 @@ Array [
},
"backgroundColor": 4294901760,
"duration": 0,
"numberOfLines": undefined,
"text": "Hello world",
"textColor": 4278190335,
},
Expand Down
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export interface SnackBarOptions {
*/
text: string;

/**
* Number of text lines to display on the snackbar.
* Default 2 lines are shown.
*/
numberOfLines: number;

/**
* Length of time the Snackbar stays on screen.
* Must be one of Snackbar.LENGTH_SHORT, Snackbar.LENGTH_LONG, or Snackbar.LENGTH_INDEFINITE.
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ type SnackBarOptions = {
*/
text: string,

/**
* Number of text lines to display on the snackbar.
* Default 2 lines are shown.
*/
numberOfLines: number,

/**
* Length of time the Snackbar stays on screen.
* Must be one of Snackbar.LENGTH_SHORT, Snackbar.LENGTH_LONG, or Snackbar.LENGTH_INDEFINITE.
Expand Down Expand Up @@ -101,6 +107,7 @@ const SnackBar: ISnackBar = {
warnDeprecation(options, 'color', 'textColor');

const text = options.text || options.title;
const { numberOfLines } = options;
// eslint-disable-next-line no-param-reassign
delete options.title;
const textColorRaw = options.textColor || options.color;
Expand All @@ -125,6 +132,7 @@ const SnackBar: ISnackBar = {
...options,
text,
textColor,
numberOfLines,
backgroundColor,
action: options.action ? {
...action,
Expand Down