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

Update index.js #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
97 changes: 57 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import styled from 'styled-components';
import {
View,
Text,
Expand All @@ -9,30 +8,48 @@ import {
} from 'react-native';
import PropTypes from 'prop-types';

const Container = styled.View`
height: ${props => props.wrapperHeight};
flex: 1;
overflow: hidden;
align-self: center;
width: ${props => props.wrapperWidth};
background-color: ${props => props.wrapperBackground};
`;
export const HighLightView = styled.View`
position: absolute;
top: ${props => (props.wrapperHeight - props.itemHeight) / 2};
height: ${props => props.itemHeight};
width: ${props => props.highlightWidth};
border-top-color: ${props => props.highlightColor};
border-bottom-color: ${props => props.highlightColor};
border-top-width: ${props => props.highlightBorderWidth}px;
border-bottom-width: ${props => props.highlightBorderWidth}px;
`;
export const SelectedItem = styled.View`
height: 30px;
justify-content: center;
align-items: center;
height: ${props => props.itemHeight};
`;
const Container = props => {
const style = {
height: props.wrapperHeight,
flex: 1,
overflow: 'hidden',
alignSelf: 'center',
width: props.wrapperWidth,
backgroundColor: props.wrapperBackground
}
return (
<View style={style}>
{props.children}
</View>
)
};

export const HighLightView = props => {
return <View style={{
position: 'absolute',
top: (props.wrapperHeight - props.itemHeight) / 2,
height: props.itemHeight,
width: props.highlightWidth,
borderTopColor: props.highlightColor,
borderBottomColor: props.highlightColor,
borderTopWidth: props.highlightBorderWidth,
borderBottomWidth: props.highlightBorderWidth
}} />
};

export const SelectedItem = props => {
const style = {
height: props.itemHeight,
justifyContent: 'center',
alignItems: 'center',
}
return (
<View style={style}>
{props.children}
</View>
)
};

const deviceWidth = Dimensions.get('window').width;
export default class ScrollPicker extends React.Component {
constructor() {
Expand All @@ -47,7 +64,7 @@ export default class ScrollPicker extends React.Component {
}

componentDidMount() {
if (this.props.selectedIndex) {
if (this.props.selectedIndex !== null) {
this.scrollToIndex(this.props.selectedIndex);
}
}
Expand All @@ -59,15 +76,15 @@ export default class ScrollPicker extends React.Component {
}

render() {
const {header, footer} = this.renderPlaceHolder();
const { header, footer } = this.renderPlaceHolder();
return (
<Container wrapperHeight={this.props.wrapperHeight} wrapperWidth={this.props.wrapperWidth}
wrapperBackground={this.props.wrapperBackground}>
wrapperBackground={this.props.wrapperBackground}>
<HighLightView highlightColor={this.props.highlightColor}
highlightWidth={this.props.highlightWidth}
wrapperHeight={this.props.wrapperHeight}
itemHeight={this.props.itemHeight}
highlightBorderWidth={this.props.highlightBorderWidth}/>
highlightWidth={this.props.highlightWidth}
wrapperHeight={this.props.wrapperHeight}
itemHeight={this.props.itemHeight}
highlightBorderWidth={this.props.highlightBorderWidth} />
<ScrollView
ref={(sview) => {
this.sview = sview;
Expand All @@ -90,9 +107,9 @@ export default class ScrollPicker extends React.Component {

renderPlaceHolder() {
const height = (this.props.wrapperHeight - this.props.itemHeight) / 2;
const header = <View style={{height, flex: 1}}></View>;
const footer = <View style={{height, flex: 1}}></View>;
return {header, footer};
const header = <View style={{ height, flex: 1 }}></View>;
const footer = <View style={{ height, flex: 1 }}></View>;
return { header, footer };
}

renderItem(data, index) {
Expand Down Expand Up @@ -120,7 +137,7 @@ export default class ScrollPicker extends React.Component {
this.isScrollTo = true;
}
if (this.sview) {
this.sview.scrollTo({y: verticalElem});
this.sview.scrollTo({ y: verticalElem });
}
}
if (this.state.selectedIndex === selectedIndex) {
Expand Down Expand Up @@ -192,7 +209,7 @@ export default class ScrollPicker extends React.Component {
const y = this.props.itemHeight * ind;
setTimeout(() => {
if (this.sview) {
this.sview.scrollTo({y});
this.sview.scrollTo({ y });
}
}, 0);
}
Expand Down Expand Up @@ -228,6 +245,6 @@ ScrollPicker.defaultProps = {
},
onScrollEndDrag: () => {
},
itemTextStyle: {fontSize: 20, lineHeight: 26, textAlign: 'center', color: '#B4B4B4'},
activeItemTextStyle: {fontSize: 20, lineHeight: 26, textAlign: 'center', color: '#222121'}
};
itemTextStyle: { fontSize: 20, lineHeight: 26, textAlign: 'center', color: '#B4B4B4' },
activeItemTextStyle: { fontSize: 20, lineHeight: 26, textAlign: 'center', color: '#222121' }
};