quick-picker
Version:
A plug and Play picker for React Native
151 lines (150 loc) • 7.19 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import * as React from 'react';
import { View, Animated, Platform, Text, Picker } from 'react-native';
import DateTimePicker from '@react-native-community/datetimepicker';
import Touchable from '@appandflow/touchable';
import pickerStore, { ANIMATION_DURATION } from './PickerStore';
var HEIGHT = 250;
var BORDERHEIGHT = 50;
var iOS_BLUE = 'rgb(0,122,255)';
// const ANDROID_PURPLE = '#6200EE';
var TOP_BACKGROUND_COLOR = '#F1F1F1';
var BACKGROUND_COLOR = '#E2E2E2';
var IosPicker = /** @class */ (function (_super) {
__extends(IosPicker, _super);
function IosPicker() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
isOpen: false,
deltaY: new Animated.Value(0),
value: pickerStore.pickerOptions.item
? pickerStore.pickerOptions.item.value
: pickerStore.pickerOptions.items
? pickerStore.pickerOptions.items[0]
: undefined,
};
_this._animateOpen = function () {
if (Platform.OS !== 'ios') {
return null;
}
Animated.timing(_this.state.deltaY, {
toValue: -HEIGHT,
duration: ANIMATION_DURATION,
useNativeDriver: pickerStore.pickerOptions.useNativeDriver || false,
}).start();
};
_this._animateClose = function () {
if (Platform.OS !== 'ios') {
return null;
}
Animated.timing(_this.state.deltaY, {
toValue: 0,
duration: ANIMATION_DURATION,
useNativeDriver: pickerStore.pickerOptions.useNativeDriver || false,
}).start();
};
_this._renderPickerBasedOnType = function () {
var _a;
var pickerOptions = pickerStore.pickerOptions;
var items = pickerOptions.items ? pickerOptions.items : [];
return (React.createElement(View, { style: [
{ backgroundColor: BACKGROUND_COLOR, height: HEIGHT - BORDERHEIGHT },
pickerOptions.disableTopRow ? { height: HEIGHT } : {},
] }, pickerOptions.pickerType === 'normal' ? (React.createElement(Picker, { selectedValue: pickerOptions.item ? (_a = pickerOptions.item) === null || _a === void 0 ? void 0 : _a.value : _this.state.value, onValueChange: function (_, itemIndex) {
// @ts-ignore
_this.props.onChange(items[itemIndex]);
if (!pickerOptions.item) {
_this.setState({ value: items[itemIndex].value });
}
_this.forceUpdate();
},
// @ts-ignore
pickerStyleType: pickerOptions.pickerStyleType, itemStyle: pickerOptions.itemStyle }, items.map(function (item, index) { return (
// @ts-ignore
React.createElement(Picker.Item, { key: item.value + "-" + index + "-pickeritem", label: item.label, value: item.value })); }))) : (React.createElement(DateTimePicker, { value: pickerOptions.date || new Date(),
// @ts-ignore
mode: pickerOptions.mode || 'time', onChange: function (_, date) { return _this.props.onChange(date); }, maximumDate: pickerOptions.maximumDate, minimumDate: pickerOptions.minimumDate, timeZoneOffsetInMinutes: pickerOptions.timeZoneOffsetInMinutes, locale: pickerOptions.locale,
// @ts-ignore
is24Hour: pickerOptions.is24Hour, minuteInterval: pickerOptions.minuteInterval, themeVariant: "light", display: Platform.OS === 'ios' ? 'spinner' : undefined }))));
};
return _this;
}
IosPicker.prototype.componentDidMount = function () {
this.props.getRef(this);
};
IosPicker.prototype.render = function () {
var pickerOptions = pickerStore.pickerOptions;
if (Platform.OS !== 'ios') {
return null;
}
var doneButtonText = pickerOptions.doneButtonText || 'Done';
return (React.createElement(Animated.View, { style: [
{
position: 'absolute',
bottom: 0 - HEIGHT,
right: 0,
left: 0,
height: HEIGHT,
borderTopWidth: 1,
borderColor: 'lightgray',
transform: [
{
translateY: this.state.deltaY,
},
],
},
] },
React.createElement(View, { style: {
height: pickerOptions.disableTopRow ? 0 : BORDERHEIGHT,
backgroundColor: TOP_BACKGROUND_COLOR,
} }, pickerOptions.topRow ? (pickerOptions.topRow) : (React.createElement(View, { style: [
{
height: BORDERHEIGHT,
paddingHorizontal: 17,
justifyContent: 'center',
alignItems: 'flex-end',
borderBottomWidth: 1,
borderColor: 'lightgray',
},
pickerOptions.disableTopRow ? { height: 0 } : {},
] },
React.createElement(Touchable, { feedback: "opacity", onPress: this.props.onPressDone },
React.createElement(Text, { style: [
{
fontWeight: '600',
fontSize: 20,
color: iOS_BLUE,
},
pickerOptions.doneButtonTextStyle
? __assign({}, pickerOptions.doneButtonTextStyle) : {},
] }, doneButtonText))))),
this._renderPickerBasedOnType()));
};
return IosPicker;
}(React.Component));
export default IosPicker;