From aa65e75e105bdbc461b98ded67bb2845e4115ed8 Mon Sep 17 00:00:00 2001 From: joshihemal <100120373+joshihemal@users.noreply.github.com> Date: Tue, 25 Oct 2022 19:54:33 +0530 Subject: [PATCH 1/2] added minimumOneStar prop i've added minimumOneStar prop by this prop value is boolean by passing true value to it when user tries to decrease star less then one the it will not decrease the star count for both type of stars half and full --- index.js | 76 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 6c671bb..2ad54c7 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ export default class StarReview extends Component { createPartialStar(partial, blockStyle, emptyBlockStyle, starStyle) { return this.props.opacity ? this.isReactElement(this.props.fullStar) ? - + {this.props.fullStar} : @@ -41,19 +41,20 @@ export default class StarReview extends Component { height: this.props.starSize, width: this.props.starSize, opacity: partial, - backgroundColor:'transparent'}} source={this.props.fullStar}/> + backgroundColor: 'transparent' + }} source={this.props.fullStar} /> : this.isReactElement(this.props.fullStar) ? - + {this.props.emptyStar} - {this.props.fullStar} + {this.props.fullStar} : - + - + @@ -62,10 +63,10 @@ export default class StarReview extends Component { displayMode() { const partial = this.displayValue - Math.floor(this.displayValue) - const emptyBlockStyle = {height: this.props.starSize, width: this.props.starSize * (1.0 - partial), backgroundColor: 'transparent'} - const blockStyle = {height: this.props.starSize, width: this.props.starSize * partial, backgroundColor: 'transparent', overflow: 'hidden'} - const starStyle = {height: this.props.starSize, width: this.props.starSize, backgroundColor: this.props.backingColor} - const spacingStyle = {paddingLeft: this.props.spacing / 2, paddingRight: this.props.spacing / 2} + const emptyBlockStyle = { height: this.props.starSize, width: this.props.starSize * (1.0 - partial), backgroundColor: 'transparent' } + const blockStyle = { height: this.props.starSize, width: this.props.starSize * partial, backgroundColor: 'transparent', overflow: 'hidden' } + const starStyle = { height: this.props.starSize, width: this.props.starSize, backgroundColor: this.props.backingColor } + const spacingStyle = { paddingLeft: this.props.spacing / 2, paddingRight: this.props.spacing / 2 } const stars = [] for (let i = 1; i < this.props.count + 1; i++) { if (i == Math.floor(this.displayValue) + 1) { @@ -82,7 +83,7 @@ export default class StarReview extends Component { {this.props.emptyStar} : - + stars.push(emptyStarComponent) @@ -92,7 +93,7 @@ export default class StarReview extends Component { {this.props.fullStar} : - + stars.push(starComponent) @@ -100,7 +101,7 @@ export default class StarReview extends Component { } return ( - {stars} + {stars} ) } @@ -111,22 +112,27 @@ export default class StarReview extends Component { starComponent = this.isReactElement(starComponent) ? starComponent : - - + + return ( - + {starComponent} - - { - this.setState({rating: index - 0.5}) - this.props.update(index - 0.5) - }}/> - { - this.setState({rating: index}) + + { + if (index == 1 && this.props.minimumOneStar) { + this.setState({ rating: index - 0 }) + this.props.update(index - 0) + } else { + this.setState({ rating: index - 0.5 }) + this.props.update(index - 0.5) + } + }} /> + { + this.setState({ rating: index }) this.props.update(index) - }}/> + }} /> ) @@ -142,7 +148,7 @@ export default class StarReview extends Component { ) } return ( - + {stars} ) @@ -152,13 +158,18 @@ export default class StarReview extends Component { const starComponent = this.isReactElement(star) ? star : - + return ( - - { - this.setState({rating: index}) - this.props.update(index) + + { + if (index == 1 && this.props.minimumOneStar) { + this.setState({ rating: index - 0 }) + this.props.update(index - 0) + } else { + this.setState({ rating: index }) + this.props.update(index) + } }}> {starComponent} @@ -175,7 +186,7 @@ export default class StarReview extends Component { ) } return ( - + {stars} ) @@ -207,6 +218,7 @@ StarReview.propTypes = { half: PropTypes.bool, spacing: PropTypes.number, disabled: PropTypes.bool, + minimumOneStar: PropTypes.bool } StarReview.defaultProps = { @@ -218,7 +230,7 @@ StarReview.defaultProps = { count: 5, default: 0, starSize: 30, - update: () => {}, + update: () => { }, opacity: false, half: false, spacing: 0 From 4a63649b8bc2d3c9725eedddcfdf2111587cd655 Mon Sep 17 00:00:00 2001 From: joshihemal <100120373+joshihemal@users.noreply.github.com> Date: Tue, 25 Oct 2022 20:02:00 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b9fadd1..cbf9b68 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ For when you want to display an aggregate value. The value displayed will remain |**`opacity`**|bool|(in Display Mode) displays star values as opacity percentages (0 - 1.0)|No|`false`| |**`half`**|bool|(in Selection Mode) allows for half star ratings|No|`false`| |**`disabled`**|bool|Disables onPress/buttons in Selection mode|No|`false`| +|**`minimumOneStar`**|bool|When true user can not be able to decrease star count less then one|No|`false`| ### Image Input