NumberField数字输入框

数字输入框。

何时使用

需要输入普通数字的时候使用。

如果要输入保留两位小数格式的金额,请使用 Currency 组件

代码演示

NumberField 数字输入框 - 图1

基本使用

基本使用。

  1. import { NumberField, Row, Col } from 'choerodon-ui/pro';
  2. function log(value) {
  3. console.log(value);
  4. }
  5. ReactDOM.render(
  6. <div>
  7. <Row gutter={10}>
  8. <Col span={12}>
  9. <NumberField placeholder="请输入整数" step={1} onChange={log} />
  10. </Col>
  11. <Col span={12}>
  12. <NumberField placeholder="精确两位小数" step={0.01} min={0} />
  13. </Col>
  14. </Row>
  15. <Row gutter={10}>
  16. <Col span={12}>
  17. <NumberField placeholder="step = 1.1, min = 0.3, max = 9" onChange={log} step={1.1} min={0.3} max={9} />
  18. </Col>
  19. <Col span={12}>
  20. <NumberField placeholder="step = 1.1, min = -0.3" onChange={log} step={1.1} min={-0.3} />
  21. </Col>
  22. </Row>
  23. </div>,
  24. mountNode
  25. );

NumberField 数字输入框 - 图2

数据源

绑定数据源。

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

NumberField 数字输入框 - 图3

范围

范围。

  1. import { DataSet, NumberField, Row, Col } from 'choerodon-ui/pro';
  2. function handleDataSetChange({ value, oldValue }) {
  3. console.log('[range dataset newValue]', value, '[oldValue]', oldValue);
  4. }
  5. function handleChange(value, oldValue) {
  6. console.log('[range newValue]', value, '[oldValue]', oldValue);
  7. }
  8. function rangeValidator(value, name) {
  9. console.log(`[validation ${name} value]`, value);
  10. return true;
  11. }
  12. class App extends React.Component {
  13. ds = new DataSet({
  14. autoCreate: true,
  15. fields: [
  16. {
  17. name: 'number1',
  18. type: 'number',
  19. range: ['start', 'end'],
  20. defaultValue: { start: 0, end: 4 },
  21. required: true,
  22. min: 1,
  23. max: 10,
  24. step: 1,
  25. validator: rangeValidator,
  26. },
  27. {
  28. name: 'number2',
  29. type: 'number',
  30. range: true,
  31. defaultValue: [0, 4],
  32. validator: rangeValidator,
  33. },
  34. { name: 'multipleNumber', type: 'number', range: true, multiple: true, required: true },
  35. ],
  36. events: {
  37. update: handleDataSetChange,
  38. },
  39. });
  40. render() {
  41. return (
  42. <Row gutter={10}>
  43. <Col span={24}>
  44. <NumberField
  45. dataSet={this.ds}
  46. name="number1"
  47. placeholder={['Range Start', 'Range End']}
  48. />
  49. </Col>
  50. <Col span={24}>
  51. <NumberField
  52. dataSet={this.ds}
  53. name="number2"
  54. placeholder={['Range Start', 'Range End']}
  55. />
  56. </Col>
  57. <Col span={24}>
  58. <NumberField
  59. range
  60. defaultValue={[0, 4]}
  61. placeholder={['Range Start', 'Range End']}
  62. onChange={handleChange}
  63. />
  64. </Col>
  65. <Col span={24}>
  66. <NumberField
  67. range={['start', 'end']}
  68. defaultValue={{ start: 0, end: 4 }}
  69. placeholder={['Range Start', 'Range End']}
  70. onChange={handleChange}
  71. />
  72. </Col>
  73. <Col span={24}>
  74. <NumberField dataSet={this.ds} name="multipleNumber" placeholder="Input Number" />
  75. </Col>
  76. </Row>
  77. );
  78. }
  79. }
  80. ReactDOM.render(<App />, mountNode);

NumberField 数字输入框 - 图4

受控数字输入框

受控数字输入框

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

NumberField 数字输入框 - 图5

最大最小值

指定最大最小值。

  1. import { DataSet, NumberField, Row, Col } from 'choerodon-ui/pro';
  2. import moment from 'moment';
  3. function filterDate(currentDate) {
  4. const dayInWeek = currentDate.get('d');
  5. return dayInWeek !== 0 && dayInWeek !== 1;
  6. }
  7. class App extends React.Component {
  8. ds = new DataSet({
  9. autoCreate: true,
  10. fields: [
  11. { name: 'start', type: 'number', max: 'end', step: 1 },
  12. { name: 'end', type: 'number', min: 'start', step: 1 },
  13. ],
  14. });
  15. render() {
  16. return (
  17. <Row gutter={10}>
  18. <Col span={6}>
  19. <NumberField dataSet={this.ds} name="start" placeholder="start" />
  20. </Col>
  21. <Col span={6}>
  22. <NumberField dataSet={this.ds} name="end" placeholder="end" />
  23. </Col>
  24. </Row>
  25. );
  26. }
  27. }
  28. ReactDOM.render(<App />, mountNode);

NumberField 数字输入框 - 图6

多值

通过属性multiple设置为多值。

  1. import { DataSet, NumberField, Row, Col } from 'choerodon-ui/pro';
  2. function handleChange(value, oldValue) {
  3. console.log('[multiple]', value, '[oldValue]', oldValue);
  4. }
  5. function handleDataSetChange({ record, name, value, oldValue }) {
  6. console.log('[dataset multiple]', value, '[oldValue]', oldValue, `[record.get('${name}')]`, record.get(name));
  7. }
  8. const data = [{
  9. user: [31],
  10. }];
  11. class App extends React.Component {
  12. ds = new DataSet({
  13. data,
  14. fields: [
  15. { name: 'size', type: 'number', label: '尺码', multiple: true, step: 1, max: 10, min: 5, required: true },
  16. ],
  17. events: {
  18. update: handleDataSetChange,
  19. },
  20. });
  21. render() {
  22. return (
  23. <Row gutter={10}>
  24. <Col span={12}>
  25. <NumberField dataSet={this.ds} name="size" placeholder="数据源多选" />
  26. </Col>
  27. <Col span={12}>
  28. <NumberField multiple onChange={handleChange} placeholder="多选" step={1} max={10} min={5} />
  29. </Col>
  30. </Row>
  31. );
  32. }
  33. }
  34. ReactDOM.render(
  35. <App />,
  36. mountNode
  37. );

API

NumberField

参数说明类型默认值
min最小值number
max最大值number
step步距number

更多属性请参考 TextField

static method

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