从 ExtReact 6.6.0 中的 FormPanel 获取值

Getting values from FormPanel in ExtReact 6.6.0

我应该如何使用 ext-react 6.6.0 从 FormPanel 获取值?

根据 API documentation 我应该使用 getValues 函数,它在 6.5.1 中有效,但我在 6.6.0

中得到错误 _this.form.getValues is not a function

代码

适用于 6.5.1:https://fiddle.sencha.com/?extreact#view/editor&fiddle/2n05

在 6.6.0 中失败(错误请查看控制台):https://fiddle.sencha.com/?extreact#view/editor&fiddle/2n04

I get error _this.form.getValues is not a function in 6.6.0

原因ref={form => this.form = form}。在 extreact-6.6.0 中,表单变量不准确 formpanel。所以为此你需要像这样访问

ref={form => this.form = (this.form || form.cmp)}}

另一种使用方式button.up('formpanel') to get the formpanel component. This button is first parameter of your handler

button.up('formpanel').getValues()

您可以在此处检查工作 fiddle

代码段

import React, { Component } from 'react';
import {launch} from '@sencha/ext-react';
import { ExtReact } from '@sencha/ext-react';
import { Container, Label, FormPanel, TextField, Button } from '@sencha/ext-modern';

class App extends Component {

    state = {
        values:JSON.stringify({
            fname: 'null',
            lname: 'null'
        })
    }

    submit = (btn) => {
        const values = btn.up('formpanel').getValues();

        console.log('Values using up selector',values);

        console.log('Values using up ref',this.form.getValues());

        this.setState({values:JSON.stringify(this.form.getValues())});
    }

    render() {
        return (
            <Container defaults={{ margin: 10, shadow: true }}>
                <FormPanel title="Form" ref={form => this.form = (this.form || form.cmp)}>
                    <TextField name="fname" label="First Name"/>
                    <TextField name="lname" label="Last Name"/>
                    <Button handler={this.submit} text="Submit"/>
                </FormPanel>

                <Label padding={'10'} html={this.state.values} />
            </Container>
        )
    }
}

launch(<ExtReact><App /></ExtReact>);