简体   繁体   English

在 AntD InputNumber 组件中接收 defaultValue 作为 prop

[英]Receive defaultValue as prop in AntD InputNumber component

I'm using AntD components to build a table and one of my columns consists of the InputNumber component.我正在使用 AntD 组件来构建一个表,其中一列由 InputNumber 组件组成。 I would like the InputNumber component to have a default value that is contained in a prop that is passed to the table component.我希望 InputNumber 组件具有包含在传递给 table 组件的 prop 中的默认值。 However I'm unsure how to access the props from the parent, or more specifically pass them to the InputNumber component as the render prop for the columns exists outside of the table component.但是,我不确定如何从父级访问道具,或者更具体地说,将它们传递给 InputNumber 组件,因为列的渲染道具存在于表格组件之外。 Here is an example of the code这是代码的示例

import React, { Component } from 'react';
import { Table, Divider, InputNumber } from 'antd';

const pageSize = 30; // Page size to show pagination
const reqColumns = [
  {
    title: 'Filled',
    dataIndex: 'slotFilled',
    editable: false,
  },
  {
    title: 'Required',
    dataIndex: 'slotMinimum',
    render: () => (
        <InputNumber min={0}/>
    ),
  },
];

export default class RequirementsTable extends Component {

  render() {
    return (
      <div>
        <Divider type="horizontal" orientation="left">
          Requirements
        </Divider>
        <Table
          rowKey="senateShortname"
          bordered
          dataSource={this.props.data}
          columns={reqColumns}
          pagination={1 > pageSize && { pageSize }}
          size="small"
        />
      </div>
    );
  }
}

I've attempted to set the defaultValue = {this.props.data} but of course that points to the props of the InputNumber.我试图设置defaultValue = {this.props.data}但当然它指向 InputNumber 的道具。

The AntD table already was attempting to pass the value to the cell. AntD 表已经尝试将值传递给单元格。 The solution was quite simple.解决办法很简单。

  {
    title: 'Required',
    dataIndex: 'slotMinimum',
    render: (value) => (
        <InputNumber min={0} defaultValue={value}/>
    ),
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM