Currency 货币输入框

货币输入框。

何时使用

当用户需要输入货币类型值时。

代码演示

基本使用

基本使用。

Currency货币输入框 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Currency, Row, Col } from 'choerodon-ui/pro';
  4. ReactDOM.render(
  5. <div>
  6. <Row gutter={10}>
  7. <Col span={12}>
  8. <Currency defaultValue={10000} />
  9. </Col>
  10. <Col span={12}>
  11. <Currency currency="CNY" defaultValue={10000} />
  12. </Col>
  13. </Row>
  14. </div>,
  15. document.getElementById('container')
  16. );

受控货币输入框

受控货币输入框

Currency货币输入框 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Currency } from 'choerodon-ui/pro';
  4. class App extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. value: 100,
  9. };
  10. }
  11. handleChange = (value, oldValue) => {
  12. console.log('[newValue]', value, '[oldValue]', oldValue);
  13. this.setState({
  14. value,
  15. });
  16. }
  17. render() {
  18. return <Currency value={this.state.value} onChange={this.handleChange} />;
  19. }
  20. }
  21. ReactDOM.render(
  22. <App />,
  23. document.getElementById('container')
  24. );

数据源

绑定数据源。

Currency货币输入框 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Currency } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, name, value, oldValue }) {
  5. console.log('[dataset newValue]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
  6. }
  7. class App extends React.Component {
  8. ds = new DataSet({
  9. autoCreate: true,
  10. fields: [
  11. { name: 'money', type: 'number', defaultValue: 100000000000000, required: true, currency: 'USD' },
  12. ],
  13. events: {
  14. update: handleDataSetChange,
  15. },
  16. });
  17. render() {
  18. return <Currency dataSet={this.ds} name="money" />;
  19. }
  20. }
  21. ReactDOM.render(
  22. <App />,
  23. document.getElementById('container')
  24. );

精度控制

根据字段值,控制值显示及精度。

Currency货币输入框 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Currency, NumberField, Row, Col } from 'choerodon-ui/pro';
  4. function handleDataSetChange({ record, name, value, oldValue }) {
  5. console.log('[dataset newValue]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
  6. }
  7. class App extends React.Component {
  8. ds = new DataSet({
  9. autoCreate: true,
  10. fields: [
  11. { name: 'precision', type: 'number', defaultValue: 4, min: 0, max: 100 },
  12. { name: 'money', type: 'number', defaultValue: 1000000.123, required: true },
  13. ],
  14. events: {
  15. update: handleDataSetChange,
  16. },
  17. });
  18. render() {
  19. return (
  20. <Row gutter={10}>
  21. <Col span="12">
  22. <NumberField placeholder="精度" dataSet={this.ds} name="precision" />
  23. </Col>
  24. <Col span="12">
  25. <Currency
  26. dataSet={this.ds}
  27. name="money"
  28. renderer={({ value, record }) => {
  29. // 仅为示例,具体精度处理根据需求调整

API

Currency

参数说明类型
currency货币代码,详见Current currency & funds code list.string

更多属性请参考 NumberField

Static method

名称说明参数
format(value, lang, options)货币格式化value - 数值 lang - 语言代码 options - 详见Intl.NumberFormatOptions