Skip to content

Commit

Permalink
onChangeBefore buttons #8
Browse files Browse the repository at this point in the history
  • Loading branch information
yasaricli committed May 1, 2020
1 parent d310e4f commit 2291d88
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
example
docs
static
capture.gif
capture.gif
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-counters": "../",
"react-native-counters": "^1.0.1",
"react-native-vector-icons": "^6.6.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5437,8 +5437,10 @@ react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.9.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.0.tgz#0f37c3613c34fe6b37cd7f763a0d6293ab15c527"
integrity sha512-GFMtL0vHkiBv9HluwNZTggSn/sCyEt9n02aM0dSAjGGyqyNlAyftYm4phPxdvCigG15JreC5biwxCgTAJZ7yAA==

react-native-counters@../:
version "0.0.1"
react-native-counters@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/react-native-counters/-/react-native-counters-1.0.1.tgz#458f8d639d501ff34c85d69b94e2af2960277223"
integrity sha512-boSbEXxYHUU7rkDIlTqMAYiveagJn30i37bDqleeJQEm9jW0WFcdAfvZzGQuR/lLTWCD1kpcjtYrBhoq4TYFKg==
dependencies:
prop-types "^15.6.2"

Expand Down
12 changes: 8 additions & 4 deletions src/components/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ export default class Button extends Component {
}

isDisabled() {
const { min, max, count } = this.props;
const { min, max, count, disabled } = this.props;

return (this.isMinus() ? min : max) == count;
if (disabled) {
return true;
}

return (this.isMinus() ? min : max) === count;
}

isMinus() {
return this.props.type == '-';
return this.props.type === '-';
}

isPlus() {
return this.props.type == '+';
return this.props.type === '+';
}

icon() {
Expand Down
28 changes: 26 additions & 2 deletions src/components/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,36 @@ export default class Counter extends Component {
super(props);
this.state = {
count: this.props.start,

// onChangeBefore loading --> disabled.
beforeLoading: false,
};
}

onPress(count, type) {
const { onChangeBefore } = this.props;

if (onChangeBefore) {
return this.setState({ beforeLoading: true }, () => {
onChangeBefore(() => {
return this.onChange(count, type);
});
});
}

return this.onChange(count, type);
}

onChange(count, type) {
const { onChange } = this.props;

return this.setState({ count }, () => {
return this.setState({ count, beforeLoading: false }, () => {
return onChange && onChange(count, type);
});
}

render() {
const { count } = this.state;
const { count, beforeLoading } = this.state;
const { countTextStyle } = this.props;

return (
Expand All @@ -35,6 +52,7 @@ export default class Counter extends Component {
type="-"
count={this.state.count}
onPress={this.onPress.bind(this)}
disabled={beforeLoading}
{...this.props}
/>

Expand All @@ -48,6 +66,7 @@ export default class Counter extends Component {
type="+"
count={this.state.count}
onPress={this.onPress.bind(this)}
disabled={beforeLoading}
{...this.props}
/>
</View>
Expand All @@ -62,7 +81,10 @@ Counter.propTypes = {
start: PropTypes.number,
min: PropTypes.number,
max: PropTypes.number,


onChange: PropTypes.func,
onChangeBefore: PropTypes.func,

minusIcon: PropTypes.func,
plusIcon: PropTypes.func,
Expand All @@ -86,4 +108,6 @@ Counter.defaultProps = {
buttonStyle: {},
buttonTextStyle: {},
countTextStyle: {},

onChangeBefore: null
};

0 comments on commit 2291d88

Please sign in to comment.