A Pen by Benjamin Reid on CodePen.
Created
June 28, 2018 09:54
-
-
Save ekzn/238d175c9185d7ebe0d2e7892cf16a59 to your computer and use it in GitHub Desktop.
gRbgxK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FormattedInput = class extends React.Component { | |
constructor(props) { | |
super(props); | |
this.amountChanged = this.amountChanged.bind(this); | |
this.state = { | |
value: 0, | |
}; | |
} | |
amountChanged(e) { | |
this.setState({ | |
value: e.target.value, | |
}); | |
} | |
formatValue(value) { | |
return accounting.formatMoney(parseFloat(value) / 100); | |
} | |
render() { | |
return ( | |
<div> | |
<label for="formatted">Formatted input</label> | |
<input | |
id="formatted" | |
type="text" | |
value={this.formatValue(this.state.value)} | |
/> | |
<label for="amount">Actual user input (type in here)</label> | |
<input | |
id="amount" | |
type="text" | |
onChange={this.amountChanged} | |
value={this.state.value} | |
/> | |
</div> | |
); | |
} | |
} | |
const App = () => | |
<div> | |
<FormattedInput/> | |
</div> | |
; | |
ReactDOM.render( | |
<App/>, | |
document.querySelector('#app') | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/accounting.js/0.4.1/accounting.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 20px; | |
} | |
label { | |
display: block; | |
margin-bottom: 10px; | |
} | |
input { | |
margin-bottom: 25px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment