Skip to content

Commit

Permalink
should change value when props.value changed
Browse files Browse the repository at this point in the history
  • Loading branch information
budiadiono committed Sep 24, 2018
1 parent 6fdb2bf commit 4cae70e
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/CalculatorInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ interface State {
text: string
}

function propsToState(props: CalculatorInputProps): Partial<State> {
const value = props.value || 0
return {
value: props.value || 0,
text: formatNumber(
value,
props.decimalSeparator as string,
props.thousandSeparator as string
)
}
}

export class CalculatorInput extends React.Component<
CalculatorInputProps,
State
Expand All @@ -77,20 +89,23 @@ export class CalculatorInput extends React.Component<
prefix: ''
}

static getDerivedStateFromProps(
props: CalculatorInputProps,
state: State
): Partial<State> | null {
if (props.value !== state.value) {
return propsToState(props)
}
return null
}

constructor(props: CalculatorInputProps) {
super(props)

const value = props.value || 0

this.calculatorModalToggle = this.calculatorModalToggle.bind(this)
this.state = {
modalVisible: false,
value,
text: formatNumber(
value,
props.decimalSeparator as string,
props.thousandSeparator as string
)
...(propsToState(props) as State),
modalVisible: false
}
}

Expand Down

0 comments on commit 4cae70e

Please sign in to comment.