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

hooks and arrow functions #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
sudo: true
node_js:
- "4.4"
- "13"
cache:
directories:
- node_modules
Expand Down
349 changes: 119 additions & 230 deletions LabelSelect/index.js
Original file line number Diff line number Diff line change
@@ -1,252 +1,141 @@
/**
* Created by TinySymphony on 2017-01-03.
* Created by TinySymphony on 2017-01-03. edited by Yonimdo at 2020-04-28 corona time
*/

import React, {Component, PropTypes} from 'react';
import {
View,
Text,
Image,
Modal,
ScrollView,
TouchableHighlight
} from 'react-native';
import React, { useState } from 'react';
import { View, Text, Image, Modal, ScrollView, StyleSheet, TouchableHighlight, Dimensions } from 'react-native';
import Styles, {IMG} from './LabelSelectStyle';

class LabelSelect extends Component {
addIcon = {
uri: IMG.addIcon
}
static propTypes = {
title: PropTypes.string,
readOnly: PropTypes.bool,
enable: PropTypes.bool,
onConfirm: PropTypes.func,
enableAddBtn: PropTypes.bool,
confirmText: PropTypes.string,
cancelText: PropTypes.string
}
static defaultProps = {
style: {},
customStyle: {},
title: ' ',
enable: true,
readOnly: false,
onConfirm: () => {},
enableAddBtn: true,
confirmText: 'Confirm',
cancelText: 'Cancel'
}
constructor(props) {
super(props);
// 初始状态
this.state = {
isModalVisible: false
};
this.selectedList = [];
this.toggleSelect = this.toggleSelect.bind(this);
this.cancelSelect = this.cancelSelect.bind(this);
this.confirmSelect = this.confirmSelect.bind(this);
this.openModal = this.openModal.bind(this);
}
setModalVisible(isVisible) {
this.setState({isModalVisible: isVisible});
}
cancelSelect() {
this.selectedList = [];
this.setModalVisible(false);
}
confirmSelect() {
const {onConfirm} = this.props;
onConfirm(this.selectedList);
this.selectedList = [];
this.cancelSelect();
}
openModal() {
if (!React.Children.toArray(this.props.children).filter(item => item.type === ModalItem).length) {
// TODO
}
this.props.enable && !this.props.readOnly && this.setModalVisible(true);
}
toggleSelect(time) {
let index = this.selectedList.findIndex(item => item === time);
if (~index) {this.selectedList.splice(index, 1);}
else {this.selectedList.push(time);}
}
render() {
const {
readOnly,
enable,
title,
style,
enableAddBtn,
customStyle,
confirmText,
cancelText
} = this.props;
let selectedLabels = React.Children.toArray(this.props.children)
.filter(item => item.type === Label)
.map((child, index) => {
return React.cloneElement(child, {
enable: enable,
readOnly: readOnly
});
});

let modalItems = this.state.isModalVisible ? React.Children.toArray(this.props.children)
.filter(item => item.type === ModalItem)
.map((child, index) => {
return React.cloneElement(child, {
toggleSelect: this.toggleSelect
});
}) : null;
function LabelSelect(props) {
const [isModalVisible, setIsModalVisible] = useState(false);
const addIcon = { uri: IMG.addIcon }

let tempSelected = []

props.options.map((option) => {
option.isSelected = props.selectedOptions.includes(option);
});
const toggleSelect = (data) => {
// if(tempSelected.includes(data))
if (data.isSelected) {
tempSelected.filter(d => { data.code == d.code })
} else {
tempSelected.push(data);
};
}
return (
<View style={[Styles.selectedView, style]}>
{selectedLabels}
{enable && !readOnly && enableAddBtn &&
<TouchableHighlight
style={[Styles.selectedItem, Styles.addItem]}
underlayColor="transparent"
onPress={this.openModal}>
<Image
style={Styles.addIcon}
source={this.addIcon}
resizeMode="cover"
/>
</TouchableHighlight>
}
<Modal
transparent={true}
visible={this.state.isModalVisible}
onRequestClose={() => {}}>
<View style={{flex: 1}}>
<TouchableHighlight
style={Styles.modalMask}
activeOpacity={1}
underlayColor="#00000077"
onPress={this.cancelSelect}>
<View style={Styles.modalContainer}>
<View style={[Styles.modal, customStyle.modal || {}]}>
<View style={Styles.title}><Text style={Styles.titleText}>{title}</Text></View>
<View style={Styles.scrollView}>
<ScrollView>
{modalItems}
</ScrollView>
</View>
<View style={[Styles.buttonView, customStyle.buttonView || {}]}>
<TouchableHighlight
underlayColor="transparent"
activeOpacity={0.8}
onPress={this.cancelSelect}>
<View style={[Styles.modalButton, customStyle.cancelButton || {}]}>
<Text style={[Styles.buttonText, customStyle.cancelText || {}]}>{cancelText}</Text>
</View>
</TouchableHighlight>
<View style={[Styles.selectedView]}>
{props.selectedOptions.map((item) => (
<Label key={item.code} data={item} onCancel={()=>{props.onCancelItem}}>{item.title}</Label>
))}

{!props.readOnly &&<TouchableHighlight
style={[Styles.selectedItem, Styles.addItem]}
underlayColor="transparent"
onPress={() => setIsModalVisible(true)}>
<Image
style={Styles.addIcon}
source={addIcon}
resizeMode="cover"
/>
</TouchableHighlight>}
{!props.readOnly && <Modal
transparent={true}
visible={isModalVisible}
onRequestClose={() => { }}>
<View style={{ flex: 1 }}>
<TouchableHighlight
underlayColor="transparent"
activeOpacity={0.8}
onPress={this.confirmSelect}>
<View style={[Styles.modalButton, Styles.confirmButton, customStyle.confirmButton || {}]}>
<Text style={[Styles.buttonText, customStyle.confirmText || {}]}>{confirmText}</Text>
</View>
style={Styles.modalMask}
activeOpacity={1}
underlayColor="#00000077"
onPress={() => setIsModalVisible(false)}>
<View style={Styles.modalContainer}>
<View style={Styles.modal}>
<View style={Styles.title}><Text style={Styles.titleText}>{props.title}</Text></View>
<View style={Styles.scrollView}>
<ScrollView>
{props.options.map((item) => (<ModalItem key={item.key}
isSelected={item.isSelected} data={item} toggleSelect={toggleSelect} >
{item.title}</ModalItem>))}
</ScrollView>
</View>
<View style={[Styles.buttonView]}>
<TouchableHighlight
underlayColor="transparent"
activeOpacity={0.8}
onPress={() => setIsModalVisible(false)}>
<View style={[Styles.modalButton, props.cancelButton || {}]}>
<Text style={[Styles.buttonText, props.cancelText || {}]}>Cancel</Text>
</View>
</TouchableHighlight>
<TouchableHighlight
underlayColor="transparent"
activeOpacity={0.8}
onPress={() => {
setIsModalVisible(false)
props.onConfirm(tempSelected);
tempSelected = [];
}}>
<View style={[Styles.modalButton, Styles.confirmButton, props.confirmButton || {}]}>
<Text style={[Styles.buttonText, props.confirmText || {}]}>Confirm</Text>
</View>
</TouchableHighlight>
</View>
</View>
</View>
</TouchableHighlight>
</View>
</View>
</View>
</TouchableHighlight>
</View>
</Modal>
</View>
</Modal>}
</View>
);
}
}

class Label extends Component {
closeIcon = {
uri: IMG.closeIcon
}
static propTypes = {
onCancel: PropTypes.func,
readOnly: PropTypes.bool,
enable: PropTypes.bool
}
static defaultProps = {
onCancel: () => {},
enable: true,
readOnly: false,
customStyle: {}
}
constructor(props) {
super(props);
}
render() {
const {enable, readOnly, onCancel, customStyle} = this.props;
function Label(props) {
const closeIcon = { uri: IMG.closeIcon };
return (
<View style={[Styles.selectedItem, !enable && Styles.disableColor]}>
<Text style={[Styles.labelText, !enable && Styles.disableText, customStyle.text || {}]}
numberOfLines={1} ellipsisMode="tail">{this.props.children}</Text>
{enable && !readOnly && <TouchableHighlight
style={Styles.closeContainer}
underlayColor="transparent"
activeOpacity={0.5}
onPress={onCancel}>
<View>
<Image
style={Styles.closeIcon}
source={this.closeIcon}
resizeMode="cover"/>
</View>
</TouchableHighlight>}
</View>
<View style={[Styles.selectedItem, !props.enable && Styles.disableColor]}>
<Text style={[Styles.labelText, !props.enable && Styles.disableText || {}]}
numberOfLines={1} >
{props.children}
</Text>
{!props.readOnly && <TouchableHighlight
style={Styles.closeContainer}
underlayColor="transparent"
activeOpacity={0.5}
onPress={()=>{props.onCancel(props.data)}}>
<View>
<Image
style={Styles.closeIcon}
source={closeIcon}
resizeMode="cover" />
</View>
</TouchableHighlight>}
</View>
);
}
}

class ModalItem extends Component {
static propTypes = {
toggleSelect: PropTypes.func
}
static defaultProps = {
customStyle: {}
}
constructor (props) {
super(props);
this.isSelected = false;
this._toggleSelect = this._toggleSelect.bind(this);
}
_toggleSelect() {
const {toggleSelect, data} = this.props;
this.isSelected = !this.isSelected;
this.forceUpdate();
toggleSelect(data);
}
render () {
const {
customStyle
} = this.props;
function ModalItem(props) {
const [isSelected, setIsSelected] = useState(props.isSelected);
return (
<TouchableHighlight
activeOpacity={0.5}
underlayColor="transparent"
onPress={this._toggleSelect}>
<View style={Styles.modalItem}>
<Text
style={[Styles.modalText, customStyle.modalText || {}]}
numberOfLines={1}
ellipsisMode="tail">
{this.props.children}
</Text>
<View style={[Styles.outerCircle, this.isSelected ? Styles.enableCircle : {}, customStyle.outerCircle || {}]}>
{this.isSelected && <View style={[Styles.innerCircle, customStyle.innerCircle || {}]}/>}
</View>
</View>
</TouchableHighlight>
<TouchableHighlight
activeOpacity={0.5}
underlayColor="transparent"
onPress={() => {
setIsSelected(!isSelected);
props.toggleSelect(props.data);
}}>
<View style={Styles.modalItem}>
<Text
style={[Styles.modalText]}
numberOfLines={1}>
{props.children}
</Text>
<View style={[Styles.outerCircle, isSelected ? Styles.enableCircle : {}]}>
{isSelected && <View style={[Styles.innerCircle]} />}
</View>
</View>
</TouchableHighlight>
);
}
}

LabelSelect.Label = Label;
LabelSelect.ModalItem = ModalItem;

export default LabelSelect;
Loading