import * as React from 'react'; import { View, Image, TextInput, TouchableOpacity } from 'react-native'; import { IMAGE_HOST } from '../_constants'; import styles from './style'; export interface StepperProps { value?: number; min?: number; max?: number; disabled?: boolean; onChange?: Function; style?: React.CSSProperties; } const Stepper: React.FC = props => { const { min = 1, value = min, max, onChange, disabled = false } = props; const [current, setCurrent] = React.useState(value); React.useEffect(() => { if (current === value) { return; } if (onChange) { onChange(current); } }, [current]); React.useEffect(() => { if (current === value) { return; } setCurrent(value); }, [value]); const onPressAdd = () => { var a = current; a++; setCurrent(a); }; const onPressReduce = () => { var a = current; a--; setCurrent(a); }; const onChangeText = (v: number) => { setCurrent(v); }; const onSubText = () => { if (current < min) { setCurrent(min); } else if (max && current > max) { setCurrent(max); } if (onChange) { onChange(current); } }; return ( {current <= min || disabled ? ( { console.log('不能减少呐'); }} > ) : ( )} onChangeText(Number(text.replace(/[^\d]+/, '')))} /> {(max && current >= max) || disabled ? ( ) : ( )} ); }; export default Stepper;