Skip to content

Commit

Permalink
fix: only apply button text color fix to spinner (#685)
Browse files Browse the repository at this point in the history
* fix: only apply button text color fix to spinner

* fix: add back removed code

* chore: remove lambda
  • Loading branch information
vonovak authored Nov 3, 2022
1 parent 83a9a97 commit 3b1db5f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 74 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

indent_style = space
indent_size = 2

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,6 @@ Callback that is called when an error occurs inside the date picker native code

## Testing with Jest

If you're rendering the picker component (using the `@testing-library/react-native` or similar), you need to mock the native module:

```
"setupFiles": ["./node_modules/@react-native-community/datetimepicker/jest/setup.js"]
```

For examples of how you can write your tests, look [here](/test/userlandTestExamples.test.js).

## Migration from the older components
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.TypedValue;
import android.widget.Button;

Expand All @@ -18,6 +19,8 @@

import com.facebook.react.bridge.Promise;

import java.util.Locale;

public class Common {

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

@NonNull
public static DialogInterface.OnShowListener setButtonTextColor(@NonNull Context activityContext, final AlertDialog dialog) {
return presentedDialog -> {
int textColorPrimary = getDefaultDialogButtonTextColor(activityContext);
Button positiveButton = dialog.getButton(DatePickerDialog.BUTTON_POSITIVE);
if (positiveButton != null) {
positiveButton.setTextColor(textColorPrimary);
}
Button negativeButton = dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE);
if (negativeButton != null) {
negativeButton.setTextColor(textColorPrimary);
}
Button neutralButton = dialog.getButton(DatePickerDialog.BUTTON_NEUTRAL);
if (neutralButton != null) {
neutralButton.setTextColor(textColorPrimary);
}
};
return new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);

int textColorPrimary = getDefaultDialogButtonTextColor(activityContext);

if (positiveButton != null) {
positiveButton.setTextColor(textColorPrimary);
}
if (negativeButton != null) {
negativeButton.setTextColor(textColorPrimary);
}
if (neutralButton != null) {
neutralButton.setTextColor(textColorPrimary);
}
}
};
}

public static RNTimePickerDisplay getDisplayTime(Bundle args) {
RNTimePickerDisplay display = RNTimePickerDisplay.DEFAULT;
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
display = RNTimePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
}
return display;
}

public static RNDatePickerDisplay getDisplayDate(Bundle args) {
RNDatePickerDisplay display = RNDatePickerDisplay.DEFAULT;
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
display = RNDatePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
}
return display;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.reactcommunity.rndatetimepicker;

import static com.reactcommunity.rndatetimepicker.Common.getDisplayDate;
import static com.reactcommunity.rndatetimepicker.Common.setButtonTextColor;

import android.annotation.SuppressLint;
Expand Down Expand Up @@ -39,6 +40,7 @@ public class RNDatePickerDialogFragment extends DialogFragment {
@Nullable
private static OnClickListener mOnNeutralButtonActionListener;

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
Expand All @@ -62,33 +64,31 @@ DatePickerDialog getDialog(
final int month = date.month();
final int day = date.day();

RNDatePickerDisplay display = RNDatePickerDisplay.DEFAULT;
RNDatePickerDisplay display = getDisplayDate(args);

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

switch (display) {
case SPINNER:
return new RNDismissableDatePickerDialog(
activityContext,
R.style.SpinnerDatePickerDialog,
onDateSetListener,
year,
month,
day,
display
);
default:
return new RNDismissableDatePickerDialog(
activityContext,
onDateSetListener,
year,
month,
day,
display
);
if (display == RNDatePickerDisplay.SPINNER) {
return new RNDismissableDatePickerDialog(
activityContext,
R.style.SpinnerDatePickerDialog,
onDateSetListener,
year,
month,
day,
display
);
}
return new RNDismissableDatePickerDialog(
activityContext,
onDateSetListener,
year,
month,
day,
display
);
}

static DatePickerDialog createDialog(
Expand All @@ -100,18 +100,23 @@ static DatePickerDialog createDialog(

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

if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
}
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
}
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
if (args != null) {
if (args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
}
if (args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
}
if (args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
}
RNDatePickerDisplay display = getDisplayDate(args);
if (display == RNDatePickerDisplay.SPINNER) {
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));
}
}

final DatePicker datePicker = dialog.getDatePicker();
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));

Integer timeZoneOffsetInMilliseconds = getTimeZoneOffset(args);
if (timeZoneOffsetInMilliseconds != null) {
Expand Down Expand Up @@ -164,7 +169,7 @@ private static int getOffset(Calendar c, Integer timeZoneOffsetInMilliseconds) {
}

@Override
public void onDismiss(DialogInterface dialog) {
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
if (mOnDismissListener != null) {
mOnDismissListener.onDismiss(dialog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package com.reactcommunity.rndatetimepicker;

import static com.reactcommunity.rndatetimepicker.Common.getDisplayTime;
import static com.reactcommunity.rndatetimepicker.Common.setButtonTextColor;

import android.app.Dialog;
Expand All @@ -24,8 +25,6 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import java.util.Locale;

@SuppressWarnings("ValidFragment")
public class RNTimePickerDialogFragment extends DialogFragment {
private TimePickerDialog instance;
Expand Down Expand Up @@ -59,20 +58,16 @@ static TimePickerDialog getDialog(
final int hour = date.hour();
final int minute = date.minute();
boolean is24hour = DateFormat.is24HourFormat(activityContext);
if (args != null) {
is24hour = args.getBoolean(RNConstants.ARG_IS24HOUR, DateFormat.is24HourFormat(activityContext));
}

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

RNTimePickerDisplay display = RNTimePickerDisplay.DEFAULT;
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
display = RNTimePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
}

if (args != null) {
is24hour = args.getBoolean(RNConstants.ARG_IS24HOUR, DateFormat.is24HourFormat(activityContext));
}
RNTimePickerDisplay display = getDisplayTime(args);

if (display == RNTimePickerDisplay.SPINNER) {
return new RNDismissableTimePickerDialog(
Expand Down Expand Up @@ -103,16 +98,21 @@ static TimePickerDialog createDialog(

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

if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
}
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
}
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
if (args != null) {
if (args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
}
if (args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
}
if (args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
}
RNTimePickerDisplay display = getDisplayTime(args);
if (display == RNTimePickerDisplay.SPINNER) {
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));
}
}
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog));
return dialog;
}

Expand Down
1 change: 0 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ export const DATE_SET_ACTION = 'dateSetAction';
export const TIME_SET_ACTION = 'timeSetAction';
export const DISMISS_ACTION = 'dismissedAction';

export const NEUTRAL_BUTTON_LABEL = 'neutralButtonLabel';
export const NEUTRAL_BUTTON_ACTION = 'neutralButtonAction';
4 changes: 2 additions & 2 deletions src/datetimepicker.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import RNDateTimePicker from './picker';
import {dateToMilliseconds, sharedPropsValidation} from './utils';
import {
IOS_DISPLAY,
ANDROID_MODE,
EVENT_TYPE_SET,
EVENT_TYPE_DISMISSED,
IOS_MODE,
} from './constants';
import * as React from 'react';
import {Platform} from 'react-native';
Expand Down Expand Up @@ -56,7 +56,7 @@ export default function Picker({
accentColor,
themeVariant,
onChange,
mode = ANDROID_MODE.date,
mode = IOS_MODE.date,
display: providedDisplay = IOS_DISPLAY.default,
disabled = false,
}: IOSNativeProps): React.Node {
Expand Down

0 comments on commit 3b1db5f

Please # to comment.