简体   繁体   English

如何使用 React 和 TypeScript 创建 ag-Grid 单元格编辑器?

[英]How do I create an ag-Grid cell editor using React and TypeScript?

I see that the ag-grid-react repo has types , and I also see that the ag-grid-react-example repo has examples .我看到ag-grid-react 仓库有 types ,我还看到ag-grid-react-example 仓库有 examples But how do I put the two together and create a cell editor with React and Types?但是如何将两者结合在一起并使用 React 和 Types 创建一个单元格编辑器?

I'm guessing it's something like this but I can't make TypeScript happy:我猜是这样的,但我不能让 TypeScript 高兴:

class MyCellEditor implements ICellEditorReactComp {
  public getValue() {
    // return something
  }

  public render() {
    const { value } = this.props
    // return something rendering value
  }
}

I implemented ICellEditor and used ICellEditorParams for prop definitions.我实现了 ICellEditor 并使用 ICellEditorParams 来定义道具。 For example, this MyCellEditor example from their documentation:例如,他们文档中的这个 MyCellEditor 示例:

// function to act as a class
function MyCellEditor () {}

// gets called once before the renderer is used
MyCellEditor.prototype.init = function(params) {
    // create the cell
    this.eInput = document.createElement('input');
    this.eInput.value = params.value;
};

// gets called once when grid ready to insert the element
MyCellEditor.prototype.getGui = function() {
    return this.eInput;
};

// focus and select can be done after the gui is attached
MyCellEditor.prototype.afterGuiAttached = function() {
    this.eInput.focus();
    this.eInput.select();
};

// returns the new value after editing
MyCellEditor.prototype.getValue = function() {
    return this.eInput.value;
};

// any cleanup we need to be done here
MyCellEditor.prototype.destroy = function() {
    // but this example is simple, no cleanup, we could
    // even leave this method out as it's optional
};

// if true, then this editor will appear in a popup
MyCellEditor.prototype.isPopup = function() {
    // and we could leave this method out also, false is the default
    return false;
};

became this:变成了这个:

class MyCellEditor extends Component<ICellEditorParams,MyCellEditorState> implements ICellEditor {
  constructor(props: ICellEditorParams) {
    super(props);

    this.state = {
      value: this.props.eGridCell.innerText
    };
  }

  // returns the new value after editing
  getValue() {
    // Ag-Grid will display this array as a string, with elements separated by commas, by default
    return this.state.value;
  };

  // Not sure how to do afterGuiAttached()

  // if true, then this editor will appear in a popup
  isPopup() {
    return true;
  };

  render() {
    return (
      <div>
        hello
      </div>
    );
  }
}

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

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