Skip to content

Commit

Permalink
Allow presetting of roll values
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamTyler committed Aug 26, 2019
1 parent 5f3a53a commit 983a638
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ class App extends React.Component {
## Provided functions
`rollAll()`: rolls all die and calls `rollDone` with total from roll
`rollAll([values])`: rolls all die and calls `rollDone` with total from roll. You can preset the `values` you would like the outcome to be by passing array of numbers, if not result will be random.
6 changes: 4 additions & 2 deletions src/DiceContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ export default class DiceContainer extends Component {
this.getDiceTotal = this.getDiceTotal.bind(this)
}

rollAll() {
rollAll(values) {
this.rollCount = 0
let index = 0
for (let die of this.dice) {
if (die !== null) {
this.rollCount++
die.rollDie()
die.rollDie(values[index])
index++
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Die.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class Die extends Component {
return Math.floor(Math.random() * max) + min
}

rollDie() {
rollDie(value) {
this.die.className = `die`
void this.die.offsetWidth
let roll = this.getRandomInt()
let roll = value || this.getRandomInt()
this.die.classList.add(`roll${roll}`)
setTimeout(() => {
this.setState({ currentValue: roll })
Expand Down Expand Up @@ -109,7 +109,7 @@ class Die extends Component {
return (
<div
className='die-container'
onClick={this.props.disableIndividual ? null : this.rollDie}
onClick={this.props.disableIndividual ? null : () => this.rollDie()}
style={containerStyle}
>
<div className='die' ref={die => (this.die = die)} style={rollStyle}>
Expand Down
4 changes: 2 additions & 2 deletions src/ReactDice.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class ReactDice extends Component {
this.props.rollDone(value)
}

rollAll() {
this.diceContainer.rollAll()
rollAll(values = []) {
this.diceContainer.rollAll(values)
}

render() {
Expand Down

0 comments on commit 983a638

Please sign in to comment.