Browse Source

Possible to change backgroundColor

master
Henning Hall 7 years ago
parent
commit
a8c670c2bd
9 changed files with 78 additions and 24 deletions
  1. +9
    -0
      android/src/main/java/com/henninghall/date_picker/DatePickerManager.java
  2. +3
    -0
      android/src/main/java/com/henninghall/date_picker/PickerView.java
  3. +26
    -0
      android/src/main/java/com/henninghall/date_picker/Style.java
  4. +3
    -1
      android/src/main/res/drawable/overlay.xml
  5. +0
    -3
      android/src/main/res/layout/datepicker_view.xml
  6. +18
    -6
      example/App.js
  7. +3
    -3
      example/examples.js
  8. +13
    -8
      example/examples/Advanced.js
  9. +3
    -3
      index.js

+ 9
- 0
android/src/main/java/com/henninghall/date_picker/DatePickerManager.java View File

@ -1,7 +1,10 @@
package com.henninghall.date_picker; package com.henninghall.date_picker;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View; import android.view.View;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder; import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.ThemedReactContext;
@ -59,6 +62,12 @@ public class DatePickerManager extends SimpleViewManager {
view.setMaximumDate(Utils.unixToDate(date)); view.setMaximumDate(Utils.unixToDate(date));
} }
@ReactProp(name = "userStyle")
public void setUserStyle(PickerView view, @Nullable ReadableMap style) {
view.style.set(style);
Log.v("style", "s");
}
@ReactProp(name = "minuteInterval") @ReactProp(name = "minuteInterval")
public void setMinuteInterval(PickerView view, @Nullable int interval) throws Exception { public void setMinuteInterval(PickerView view, @Nullable int interval) throws Exception {
if (interval < 0 || interval > 59) throw new Exception("Minute interval out of bounds"); if (interval < 0 || interval > 59) throw new Exception("Minute interval out of bounds");

+ 3
- 0
android/src/main/java/com/henninghall/date_picker/PickerView.java View File

@ -5,6 +5,7 @@ import android.view.View;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter; import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.henninghall.date_picker.wheelFunctions.AnimateToDate; import com.henninghall.date_picker.wheelFunctions.AnimateToDate;
@ -46,10 +47,12 @@ public class PickerView extends RelativeLayout {
public int minuteInterval = 1; public int minuteInterval = 1;
public Locale locale; public Locale locale;
public Mode mode; public Mode mode;
public Style style;
public PickerView() { public PickerView() {
super(DatePickerManager.context); super(DatePickerManager.context);
View rootView = inflate(getContext(), R.layout.datepicker_view, this); View rootView = inflate(getContext(), R.layout.datepicker_view, this);
this.style = new Style(this);
RelativeLayout wheelsWrapper = (RelativeLayout) rootView.findViewById(R.id.wheelsWrapper); RelativeLayout wheelsWrapper = (RelativeLayout) rootView.findViewById(R.id.wheelsWrapper);
wheelsWrapper.setWillNotDraw(false); wheelsWrapper.setWillNotDraw(false);

+ 26
- 0
android/src/main/java/com/henninghall/date_picker/Style.java View File

@ -0,0 +1,26 @@
package com.henninghall.date_picker;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.widget.ImageView;
import com.facebook.react.bridge.ReadableMap;
class Style {
private final GradientDrawable gradient;
public Style(PickerView pickerView) {
ImageView overlayTop = (ImageView) pickerView.findViewById(R.id.overlay_top);
this.gradient = (GradientDrawable) overlayTop.getDrawable();
}
public void set(ReadableMap style) {
String color = style.getString("backgroundColor");
if(color != null && color.length() == 7) {
int startColor = Color.parseColor("#FF"+ color.substring(1));
int endColor = Color.parseColor("#00" + color.substring(1));
gradient.setColors(new int[] {startColor, endColor});
}
}
}

+ 3
- 1
android/src/main/res/drawable/overlay.xml View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay_image"
>
<gradient <gradient
android:startColor="#FFFFFFFF" android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF" android:endColor="#00FFFFFF"

+ 0
- 3
android/src/main/res/layout/datepicker_view.xml View File

@ -20,7 +20,6 @@
android:id="@+id/day" android:id="@+id/day"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="160dp" android:layout_height="160dp"
android:background="#FFFFFF"
app:npv_ShowCount="5" app:npv_ShowCount="5"
app:npv_RespondChangeOnDetached="false" app:npv_RespondChangeOnDetached="false"
app:npv_TextSizeNormal="18sp" app:npv_TextSizeNormal="18sp"
@ -38,7 +37,6 @@
android:layout_height="160dp" android:layout_height="160dp"
android:layout_toEndOf="@+id/day" android:layout_toEndOf="@+id/day"
android:layout_toRightOf="@+id/day" android:layout_toRightOf="@+id/day"
android:background="#FFFFFF"
app:npv_DividerColor="#cccccc" app:npv_DividerColor="#cccccc"
app:npv_RespondChangeOnDetached="false" app:npv_RespondChangeOnDetached="false"
app:npv_ShowCount="5" app:npv_ShowCount="5"
@ -56,7 +54,6 @@
android:layout_height="160dp" android:layout_height="160dp"
android:layout_toEndOf="@+id/hour" android:layout_toEndOf="@+id/hour"
android:layout_toRightOf="@+id/hour" android:layout_toRightOf="@+id/hour"
android:background="#FFFFFF"
app:npv_DividerColor="#cccccc" app:npv_DividerColor="#cccccc"
app:npv_RespondChangeOnDetached="false" app:npv_RespondChangeOnDetached="false"
app:npv_ShowCount="5" app:npv_ShowCount="5"

+ 18
- 6
example/App.js View File

@ -8,20 +8,30 @@ import examples from './examples';
export default class App extends Component { export default class App extends Component {
state = { picker: undefined }
state = {
picker: undefined,
backgroundColor: undefined,
}
render() { render() {
return ( return (
<View style={styles.container}>
<ScrollView
style={[styles.container, { backgroundColor: this.state.backgroundColor }]}
contentContainerStyle={styles.content}>
<Text style={styles.header}>Examples</Text> <Text style={styles.header}>Examples</Text>
{!this.state.picker && this.renderButtons()} {!this.state.picker && this.renderButtons()}
{!!this.state.picker && this.renderBackButton()} {!!this.state.picker && this.renderBackButton()}
{!!this.state.picker && this.renderPicker()} {!!this.state.picker && this.renderPicker()}
</View>
</ScrollView>
); );
} }
renderPicker = () => examples[this.state.picker].component
setBackgroundColor = backgroundColor => this.setState({ backgroundColor })
renderPicker = () => {
const Picker = examples[this.state.picker].component;
return <Picker backgroundColor={this.state.color} setBackground={this.setBackgroundColor} />
}
renderButtons = () => renderButtons = () =>
Object.keys(examples) Object.keys(examples)
@ -52,10 +62,12 @@ export default class App extends Component {
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
alignItems: 'center',
backgroundColor: 'white',
backgroundColor: '#abcdef',
marginTop: 15, marginTop: 15,
}, },
content: {
alignItems: 'center',
},
text: { text: {
color: 'dodgerblue', color: 'dodgerblue',
fontSize: 16, fontSize: 16,

+ 3
- 3
example/examples.js View File

@ -7,14 +7,14 @@ import { EXAMPLE_KEYS } from './exampleKeys'
export default { export default {
[EXAMPLE_KEYS.MINIMAL]: { [EXAMPLE_KEYS.MINIMAL]: {
buttonTitle: 'Minimal', buttonTitle: 'Minimal',
component: <Minimal />
component: Minimal
}, },
[EXAMPLE_KEYS.ADVANCED]: { [EXAMPLE_KEYS.ADVANCED]: {
buttonTitle: 'Advanced', buttonTitle: 'Advanced',
component: <Advanced />
component: Advanced
}, },
[EXAMPLE_KEYS.TIME_MODE]: { [EXAMPLE_KEYS.TIME_MODE]: {
buttonTitle: 'Time mode', buttonTitle: 'Time mode',
component: <TimeMode />
component: TimeMode
} }
} }

+ 13
- 8
example/examples/Advanced.js View File

@ -5,9 +5,9 @@ import DeviceInfo from 'react-native-device-info';
import DatePicker from 'react-native-date-picker-x'; import DatePicker from 'react-native-date-picker-x';
import locales from '../locales'; import locales from '../locales';
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
Date.prototype.addHours = function (h) {
this.setTime(this.getTime() + (h * 60 * 60 * 1000));
return this;
} }
export default class App extends Component { export default class App extends Component {
@ -19,8 +19,8 @@ export default class App extends Component {
} }
render() { render() {
const result = locales.filter(createFilter(this.state.searchTerm)).slice(0, 5);
const result = locales.filter(createFilter(this.state.searchTerm)).slice(0, 5);
const { backgroundColor } = this.props;
return ( return (
<View style={styles.container}> <View style={styles.container}>
<DatePicker <DatePicker
@ -29,12 +29,13 @@ export default class App extends Component {
locale={this.state.locale} locale={this.state.locale}
minuteInterval={1} minuteInterval={1}
minimumDate={new Date()} minimumDate={new Date()}
maximumDate={(new Date()).addHours(24*5)}
maximumDate={(new Date()).addHours(24 * 5)}
style={this.style()}
/> />
<Text>Current locale: {this.state.locale}</Text> <Text>Current locale: {this.state.locale}</Text>
<Text>Current date: {this.state.chosenDate.toISOString()}</Text> <Text>Current date: {this.state.chosenDate.toISOString()}</Text>
<Text></Text>
<Text />
<Button title='Change date' <Button title='Change date'
onPress={() => this.setState({ onPress={() => this.setState({
@ -54,21 +55,25 @@ export default class App extends Component {
)) ))
} }
</ScrollView> </ScrollView>
<Text />
<Button title={'Change background color'} onPress={() => this.props.setBackground(randomColor())} />
</View> </View>
); );
} }
setDate = (newDate) => this.setState({ chosenDate: newDate }) setDate = (newDate) => this.setState({ chosenDate: newDate })
searchUpdated = (term) => this.setState({ searchTerm: term }) searchUpdated = (term) => this.setState({ searchTerm: term })
style = () => this.props.backgroundColor && ({ backgroundColor: this.props.backgroundColor })
} }
const randomColor = () => '#' + Math.floor(Math.random() * 16777215).toString(16);
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
backgroundColor: 'white',
marginTop: 15, marginTop: 15,
}, },
item: { item: {

+ 3
- 3
index.js View File

@ -17,22 +17,22 @@ const ios = Platform.OS === 'ios';
const NativeDatePicker = requireNativeComponent(`DatePickerManager`, DatePickerAndroid, { nativeOnly: { onChange: true } }); const NativeDatePicker = requireNativeComponent(`DatePickerManager`, DatePickerAndroid, { nativeOnly: { onChange: true } });
class DatePickerAndroid extends React.Component { class DatePickerAndroid extends React.Component {
static defaultProps = { static defaultProps = {
mode: 'datetime', mode: 'datetime',
minuteInterval: 1, minuteInterval: 1,
}; };
_onChange = e => this.props.onDateChange(new Date(parseInt(e.nativeEvent.date))); _onChange = e => this.props.onDateChange(new Date(parseInt(e.nativeEvent.date)));
_maximumDate = () => this.props.maximumDate && this.props.maximumDate.getTime(); _maximumDate = () => this.props.maximumDate && this.props.maximumDate.getTime();
_minimumDate = () => this.props.minimumDate && this.props.minimumDate.getTime(); _minimumDate = () => this.props.minimumDate && this.props.minimumDate.getTime();
render = () => ( render = () => (
<NativeDatePicker <NativeDatePicker
{...this.props } {...this.props }
date={this.props.date.getTime()}
minimumDate={this._minimumDate()} minimumDate={this._minimumDate()}
maximumDate={this._maximumDate()} maximumDate={this._maximumDate()}
date={this.props.date.getTime()}
onChange={this._onChange} onChange={this._onChange}
userStyle={this.props.style}
style={[styles.picker, this.props.style]} style={[styles.picker, this.props.style]}
/> />
) )

Loading…
Cancel
Save