Skip to content

Commit

Permalink
feat(Confirm): add Confirm component
Browse files Browse the repository at this point in the history
  • Loading branch information
lulutia committed Mar 16, 2017
1 parent 6c27e8f commit 1882fd5
Show file tree
Hide file tree
Showing 17 changed files with 564 additions and 101 deletions.
1 change: 0 additions & 1 deletion components/Button/ButtonStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export let ButtonType = StyleSheet.create({

export let ButtonOuter = StyleSheet.create({
btn: {
position: 'absolute',
alignItems: 'center',
justifyContent: 'center'
},
Expand Down
78 changes: 55 additions & 23 deletions components/Button/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,142 +13,174 @@ class button extends Component {
}
render() {
return (
<View style={{marginLeft: 20, marginTop: 20}}>
<View style={
{flex: 1, marginLeft: 10, backgroundColor: '#F5FCFF'}
}>
<View style={
{flex: 1, flexDirection: 'row', alignItems: 'center'}
}>
<Button
type="surface"
size="small"
theme="orange"
selfStyle={{marginRight: 10}}
loadingTitle="正在加载"
isLoading={true}
onPress={this._onPressButton}>Small</Button>
<Button
selfStyle={{marginLeft: 120}}
type="ghost"
size="small"
theme="blue"
selfStyle={{marginRight: 10}}
isLoading={true}
onPress={this._onPressButton}>Default</Button>
</View>

<View style={
{flex: 1, flexDirection: 'row', alignItems: 'center'}
}>
<Button
selfStyle={{marginTop: 60}}
theme="#BA55D3"
loadingTitle="正在加载"
selfStyle={{marginRight: 10}}
isLoading={true}
disableColor="#00C5CD"
onPress={this._onPressButton}>Default</Button>
<Button
selfStyle={{marginLeft: 120, marginTop: 60}}
theme="#BA55D3"
loadingTitle="正在加载"
selfStyle={{marginRight: 10}}
isLoading={true}
disableColor="#00C5CD"
loadingColor="rgba(221,106,167,0.8)"
onPress={this._onPressButton}>Default</Button>
<Button
selfStyle={{marginLeft: 240, marginTop: 60}}
theme="#BA55D3"
selfStyle={{marginRight: 10}}
isLoading={true}
onPress={this._onPressButton}>Default</Button>
</View>
<View style={
{flex: 1, flexDirection: 'row', alignItems: 'center'}
}>
<Button
selfStyle={{marginTop: 120}}
theme="rgba(221,106,167,0.8)"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>default-btn</Button>
<Button
selfStyle={{marginTop: 120, marginLeft: 110}}
theme="rgba(221,106,167,0.8)"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}><Text style={{marginRight: 20}}>node</Text>string</Button>
<Button
selfStyle={{marginTop: 120, marginLeft: 240}}
theme="rgba(221,106,167,0.8)"
isLoading={true}
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}><Text style={{marginRight: 20}}>test</Text>test</Button>
</View>
<View style={
{flex: 1, flexDirection: 'row', alignItems: 'center'}
}>
<Button
selfStyle={{marginTop: 180}}
theme="#00C5CD"
size="small"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>small</Button>
<Button
selfStyle={{marginTop: 230}}
theme="#00C5CD"
size="default"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 280}}
theme="#00C5CD"
size="large"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>large</Button>
</View>
<View style={
{flex: 1, flexDirection: 'row', alignItems: 'center'}
}>
<Button
selfStyle={{marginTop: 350}}
theme="blue"
size="default"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 350, marginLeft: 80}}
theme="red"
size="default"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 350, marginLeft: 160}}
theme="orange"
size="default"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 350, marginLeft: 240}}
theme="gray"
size="default"
selfStyle={{marginRight: 10}}
onPress={this._onPressButton}>default</Button>
</View>
<View style={
{flex: 1, flexDirection: 'row', alignItems: 'center'}
}>
<Button
selfStyle={{marginTop: 400}}
theme="blue"
size="default"
selfStyle={{marginRight: 10}}
disabled={true}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 400, marginLeft: 80}}
theme="red"
size="default"
selfStyle={{marginRight: 10}}
disabled={true}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 400, marginLeft: 160}}
theme="orange"
size="default"
type="ghost"
selfStyle={{marginRight: 10}}
disabled={true}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 400, marginLeft: 240}}
theme="gray"
size="default"
type="ghost"
selfStyle={{marginRight: 10}}
disabled={true}
onPress={this._onPressButton}>default</Button>
</View>
<View style={
{flex: 1, flexDirection: 'row', alignItems: 'center'}
}>
<Button
selfStyle={{marginTop: 450}}
theme="blue"
size="default"
selfStyle={{marginRight: 10}}
active={true}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 450, marginLeft: 80}}
theme="red"
size="default"
selfStyle={{marginRight: 10}}
active={true}
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 450, marginLeft: 160}}
theme="orange"
size="default"
selfStyle={{marginRight: 10}}
active={true}
type="ghost"
onPress={this._onPressButton}>default</Button>
<Button
selfStyle={{marginTop: 450, marginLeft: 240}}
theme="gray"
size="default"
selfStyle={{marginRight: 10}}
active={true}
type="ghost"
onPress={this._onPressButton}>default</Button>
</View>
</View>
);
}
}
Expand Down
9 changes: 9 additions & 0 deletions components/Confirm/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
example/
__tests__/
.*
components/
coverage/
index.android.js
index.ios.js
android/
ios/
148 changes: 148 additions & 0 deletions components/Confirm/Confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import React, {Component} from 'react';
import {
Text,
View,
Modal
} from 'react-native';
import Style from './ConfirmStyle';
import Button from 'react-native-buttons';
import Input from 'react-native-input-xg';

class Confirm extends Component {

// 构造
constructor(props) {
super(props);
// 初始状态
this.state = {
modalVisible: false,
title: null,
text: null,
content: null,
comment: null,
commentRequired: false,
disableCancel: false,
confirmText: '确定',
cancelText: '取消'
};

this.open = this.open.bind(this);
this.onCancel = this.onCancel.bind(this);
this.onConfirm = this.onConfirm.bind(this);
}

open(options) {
const {
title = null, text = null, content = null, comment = null,
commentRequired = false, disableCancel = false, confirmText = '确定', cancelText = '取消'
} = options;

this._cacheOptions = options;

this.setState({
title,
text,
content,
comment: undefined,
commentPlaceholder: comment,
commentRequired,
modalVisible: true,
disableCancel,
confirmText,
cancelText
});
}

onCancel() {
const {cancel} = this._cacheOptions;

if (typeof cancel === 'function') {
cancel();
}

this._cacheOptions = null;
this.setState({
modalVisible: false
});
}

onConfirm() {
const {comment = null, confirm} = this._cacheOptions;
let confirmResult = false;

if (typeof confirm === 'function') {
confirmResult = confirm(comment ? {
comment: this.state.comment
} : undefined);
}

// 返回值false时关闭弹窗, 返回true不做处理
if (!confirmResult) {
this.setState({
modalVisible: false
});
} else if (confirmResult instanceof Promise) {
// 返回Promise时, promise成功后关闭
confirmResult.then(() => {
this.setState({
modalVisible: false
});
});
} else {
return undefined;
}
}

render() {
const {
title, text, content, confirmText, cancelText, comment, commentPlaceholder, modalVisible, commentRequired, disableCancel
} = this.state;

return (
<Modal
animationType={'none'}
transparent
visible={modalVisible}
onRequestClose={() => {
console.debug('close');
}}
>
<View style={Style.wrapper}>
<View style={Style.body}>
{title && <View style={Style.title}>
<Text style={Style.outerView}>{title}</Text>
</View>}
<View style={Style.content}>
{text && <Text style={Style.subText}>{text}</Text>}
{content}
{commentPlaceholder &&
<Input wrapperStyle={{marginTop: 5}}
onChangeText={(inputText) => this.setState({
comment: inputText
})}
placeholder={commentPlaceholder}
/>}
</View>

<View style={Style.innerView}>
{!disableCancel &&
<Button
theme="gray"
selfStyle={[Style.footerBtn]}
size="default"
onPress={this.onCancel}>{cancelText}</Button>}
<Button
theme="orange"
selfStyle={[Style.footerBtn]}
disabled={commentRequired && !comment}
onPress={this.onConfirm}
>{confirmText}</Button>
</View>
</View>
</View>
</Modal>
);
}
}

export default Confirm;
Loading

0 comments on commit 1882fd5

Please sign in to comment.