Skip to content

Commit 3b1db5f

Browse files
authored
fix: only apply button text color fix to spinner (#685)
* fix: only apply button text color fix to spinner * fix: add back removed code * chore: remove lambda
1 parent 83a9a97 commit 3b1db5f

File tree

7 files changed

+111
-74
lines changed

7 files changed

+111
-74
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
9+
indent_style = space
10+
indent_size = 2
11+
12+
end_of_line = lf
13+
charset = utf-8
14+
trim_trailing_whitespace = true
15+
insert_final_newline = true

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,6 @@ Callback that is called when an error occurs inside the date picker native code
496496

497497
## Testing with Jest
498498

499-
If you're rendering the picker component (using the `@testing-library/react-native` or similar), you need to mock the native module:
500-
501-
```
502-
"setupFiles": ["./node_modules/@react-native-community/datetimepicker/jest/setup.js"]
503-
```
504-
505499
For examples of how you can write your tests, look [here](/test/userlandTestExamples.test.js).
506500

507501
## Migration from the older components

android/src/main/java/com/reactcommunity/rndatetimepicker/Common.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Context;
66
import android.content.DialogInterface;
77
import android.content.res.Resources;
8+
import android.os.Bundle;
89
import android.util.TypedValue;
910
import android.widget.Button;
1011

@@ -18,6 +19,8 @@
1819

1920
import com.facebook.react.bridge.Promise;
2021

22+
import java.util.Locale;
23+
2124
public class Common {
2225

2326
public static void dismissDialog(FragmentActivity activity, String fragmentTag, Promise promise) {
@@ -54,20 +57,41 @@ public static int getDefaultDialogButtonTextColor(@NonNull Context activity) {
5457

5558
@NonNull
5659
public static DialogInterface.OnShowListener setButtonTextColor(@NonNull Context activityContext, final AlertDialog dialog) {
57-
return presentedDialog -> {
58-
int textColorPrimary = getDefaultDialogButtonTextColor(activityContext);
59-
Button positiveButton = dialog.getButton(DatePickerDialog.BUTTON_POSITIVE);
60-
if (positiveButton != null) {
61-
positiveButton.setTextColor(textColorPrimary);
62-
}
63-
Button negativeButton = dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE);
64-
if (negativeButton != null) {
65-
negativeButton.setTextColor(textColorPrimary);
66-
}
67-
Button neutralButton = dialog.getButton(DatePickerDialog.BUTTON_NEUTRAL);
68-
if (neutralButton != null) {
69-
neutralButton.setTextColor(textColorPrimary);
70-
}
71-
};
60+
return new DialogInterface.OnShowListener() {
61+
@Override
62+
public void onShow(DialogInterface dialogInterface) {
63+
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
64+
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
65+
Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
66+
67+
int textColorPrimary = getDefaultDialogButtonTextColor(activityContext);
68+
69+
if (positiveButton != null) {
70+
positiveButton.setTextColor(textColorPrimary);
71+
}
72+
if (negativeButton != null) {
73+
negativeButton.setTextColor(textColorPrimary);
74+
}
75+
if (neutralButton != null) {
76+
neutralButton.setTextColor(textColorPrimary);
77+
}
78+
}
79+
};
7280
}
81+
82+
public static RNTimePickerDisplay getDisplayTime(Bundle args) {
83+
RNTimePickerDisplay display = RNTimePickerDisplay.DEFAULT;
84+
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
85+
display = RNTimePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
86+
}
87+
return display;
88+
}
89+
90+
public static RNDatePickerDisplay getDisplayDate(Bundle args) {
91+
RNDatePickerDisplay display = RNDatePickerDisplay.DEFAULT;
92+
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
93+
display = RNDatePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
94+
}
95+
return display;
96+
}
7397
}

android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogFragment.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package com.reactcommunity.rndatetimepicker;
99

10+
import static com.reactcommunity.rndatetimepicker.Common.getDisplayDate;
1011
import static com.reactcommunity.rndatetimepicker.Common.setButtonTextColor;
1112

1213
import android.annotation.SuppressLint;
@@ -39,6 +40,7 @@ public class RNDatePickerDialogFragment extends DialogFragment {
3940
@Nullable
4041
private static OnClickListener mOnNeutralButtonActionListener;
4142

43+
@NonNull
4244
@Override
4345
public Dialog onCreateDialog(Bundle savedInstanceState) {
4446
Bundle args = getArguments();
@@ -62,33 +64,31 @@ DatePickerDialog getDialog(
6264
final int month = date.month();
6365
final int day = date.day();
6466

65-
RNDatePickerDisplay display = RNDatePickerDisplay.DEFAULT;
67+
RNDatePickerDisplay display = getDisplayDate(args);
6668

6769
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
6870
display = RNDatePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
6971
}
7072

71-
switch (display) {
72-
case SPINNER:
73-
return new RNDismissableDatePickerDialog(
74-
activityContext,
75-
R.style.SpinnerDatePickerDialog,
76-
onDateSetListener,
77-
year,
78-
month,
79-
day,
80-
display
81-
);
82-
default:
83-
return new RNDismissableDatePickerDialog(
84-
activityContext,
85-
onDateSetListener,
86-
year,
87-
month,
88-
day,
89-
display
90-
);
73+
if (display == RNDatePickerDisplay.SPINNER) {
74+
return new RNDismissableDatePickerDialog(
75+
activityContext,
76+
R.style.SpinnerDatePickerDialog,
77+
onDateSetListener,
78+
year,
79+
month,
80+
day,
81+
display
82+
);
9183
}
84+
return new RNDismissableDatePickerDialog(
85+
activityContext,
86+
onDateSetListener,
87+
year,
88+
month,
89+
day,
90+
display
91+
);
9292
}
9393

9494
static DatePickerDialog createDialog(
@@ -100,18 +100,23 @@ static DatePickerDialog createDialog(
100100

101101
DatePickerDialog dialog = getDialog(args, activityContext, onDateSetListener);
102102

103-
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
104-
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
105-
}
106-
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
107-
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
108-
}
109-
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
110-
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
103+
if (args != null) {
104+
if (args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
105+
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
106+
}
107+
if (args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
108+
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
109+
}
110+
if (args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
111+
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
112+
}
113+
RNDatePickerDisplay display = getDisplayDate(args);
114+
if (display == RNDatePickerDisplay.SPINNER) {
115+
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));
116+
}
111117
}
112118

113119
final DatePicker datePicker = dialog.getDatePicker();
114-
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));
115120

116121
Integer timeZoneOffsetInMilliseconds = getTimeZoneOffset(args);
117122
if (timeZoneOffsetInMilliseconds != null) {
@@ -164,7 +169,7 @@ private static int getOffset(Calendar c, Integer timeZoneOffsetInMilliseconds) {
164169
}
165170

166171
@Override
167-
public void onDismiss(DialogInterface dialog) {
172+
public void onDismiss(@NonNull DialogInterface dialog) {
168173
super.onDismiss(dialog);
169174
if (mOnDismissListener != null) {
170175
mOnDismissListener.onDismiss(dialog);

android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogFragment.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package com.reactcommunity.rndatetimepicker;
1010

11+
import static com.reactcommunity.rndatetimepicker.Common.getDisplayTime;
1112
import static com.reactcommunity.rndatetimepicker.Common.setButtonTextColor;
1213

1314
import android.app.Dialog;
@@ -24,8 +25,6 @@
2425
import androidx.annotation.Nullable;
2526
import androidx.fragment.app.DialogFragment;
2627

27-
import java.util.Locale;
28-
2928
@SuppressWarnings("ValidFragment")
3029
public class RNTimePickerDialogFragment extends DialogFragment {
3130
private TimePickerDialog instance;
@@ -59,20 +58,16 @@ static TimePickerDialog getDialog(
5958
final int hour = date.hour();
6059
final int minute = date.minute();
6160
boolean is24hour = DateFormat.is24HourFormat(activityContext);
61+
if (args != null) {
62+
is24hour = args.getBoolean(RNConstants.ARG_IS24HOUR, DateFormat.is24HourFormat(activityContext));
63+
}
6264

6365
int minuteInterval = RNConstants.DEFAULT_TIME_PICKER_INTERVAL;
6466
if (args != null && MinuteIntervalSnappableTimePickerDialog.isValidMinuteInterval(args.getInt(RNConstants.ARG_INTERVAL))) {
6567
minuteInterval = args.getInt(RNConstants.ARG_INTERVAL);
6668
}
6769

68-
RNTimePickerDisplay display = RNTimePickerDisplay.DEFAULT;
69-
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
70-
display = RNTimePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
71-
}
72-
73-
if (args != null) {
74-
is24hour = args.getBoolean(RNConstants.ARG_IS24HOUR, DateFormat.is24HourFormat(activityContext));
75-
}
70+
RNTimePickerDisplay display = getDisplayTime(args);
7671

7772
if (display == RNTimePickerDisplay.SPINNER) {
7873
return new RNDismissableTimePickerDialog(
@@ -103,16 +98,21 @@ static TimePickerDialog createDialog(
10398

10499
TimePickerDialog dialog = getDialog(args, activityContext, onTimeSetListener);
105100

106-
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
107-
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
108-
}
109-
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
110-
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
111-
}
112-
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
113-
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
101+
if (args != null) {
102+
if (args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
103+
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
104+
}
105+
if (args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
106+
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
107+
}
108+
if (args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
109+
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
110+
}
111+
RNTimePickerDisplay display = getDisplayTime(args);
112+
if (display == RNTimePickerDisplay.SPINNER) {
113+
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));
114+
}
114115
}
115-
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));
116116
return dialog;
117117
}
118118

src/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,4 @@ export const DATE_SET_ACTION = 'dateSetAction';
5757
export const TIME_SET_ACTION = 'timeSetAction';
5858
export const DISMISS_ACTION = 'dismissedAction';
5959

60-
export const NEUTRAL_BUTTON_LABEL = 'neutralButtonLabel';
6160
export const NEUTRAL_BUTTON_ACTION = 'neutralButtonAction';

src/datetimepicker.ios.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import RNDateTimePicker from './picker';
1313
import {dateToMilliseconds, sharedPropsValidation} from './utils';
1414
import {
1515
IOS_DISPLAY,
16-
ANDROID_MODE,
1716
EVENT_TYPE_SET,
1817
EVENT_TYPE_DISMISSED,
18+
IOS_MODE,
1919
} from './constants';
2020
import * as React from 'react';
2121
import {Platform} from 'react-native';
@@ -56,7 +56,7 @@ export default function Picker({
5656
accentColor,
5757
themeVariant,
5858
onChange,
59-
mode = ANDROID_MODE.date,
59+
mode = IOS_MODE.date,
6060
display: providedDisplay = IOS_DISPLAY.default,
6161
disabled = false,
6262
}: IOSNativeProps): React.Node {

0 commit comments

Comments
 (0)