在语义UI React中,从未获得Inside Ref Inside Ref Inside弹出窗口



我在包含输入字段的语义UI React中具有简单的弹出窗口。打开弹出窗口时,应立即将此输入字段聚焦。到目前为止没有运气。这就是我尝试的:

import * as React from 'react';
import { Input, Label, Popup, Segment } from 'semantic-ui-react';
export class Test extends React.Component {
    private searchInput: React.RefObject<Input>;
    constructor(props: any) {
        super(props);
        this.searchInput = React.createRef();
    }
    public render() {
        return (
            <Popup
                trigger={<Label>Test</Label>}
                content={this.renderSelector()}
                on="hover"
                hoverable={true}
                position="bottom left"
                onOpen={() => this.focusInput()}
            />
        )
    }
    private renderSelector() {
        return (
            <Segment>
                <Input ref={this.searchInput} fluid={true} icon="search" iconPosition="left" />
            </Segment>
        )
    }
    private focusInput() {
        if (this.searchInput.current) {
            this.searchInput.current.focus()
        }
    }
}

this.searchInput.current始终为空。我还尝试将输入包装在Ref组件中,但结果相同:

    private renderSelector() {
        return (
            <Segment>
                <Ref innerRef={this.searchInput}>
                    <Input fluid={true} icon="search" iconPosition="left" />
                </Ref>
            </Segment>
        )
    }

最后,即使试图在DOM中找到输入,我也会得到一个空结果:

    private renderSelector() {
        return (
            <Segment>
                <Input id="foobar" fluid={true} icon="search" iconPosition="left" />
            </Segment>
        )
    }
    private focusInput() {
        const foo = document.getElementById("foobar");
        if (foo) {
            const bar = foo as HTMLInputElement;
            bar.focus();
        }
    }

知道我在这里缺少什么吗?

谢谢!

这是解决方案,如下所示:https://github.com/semantic-org/semantic-ui-react/issues/3473

主要问题基本上是REF仅在提交阶段可用(其中包括ComponentDidupDate方法)。

import * as React from 'react';
import { Input, Label, Popup, Segment } from 'semantic-ui-react';
interface TestState {
    isOpen: boolean;
}
export class Test extends React.Component<{}, TestState> {
    private searchInput: React.RefObject<Input>;
    constructor(props: any) {
        super(props);
        this.searchInput = React.createRef();
        this.state = { isOpen: false };
    }
    public componentDidUpdate(prevProps: any, prevState: TestState) {
        if (!prevState.isOpen && this.state.isOpen) {
            if (this.searchInput.current) {
                this.searchInput.current.focus();
            }
        }
    }
    public render() {
        return (
            <Popup
                trigger={<Label>Test</Label>}
                content={this.renderSelector()}
                on="hover"
                hoverable={true}
                position="bottom left"
                onMount={() => this.setState({ isOpen: true })}
                onUnmount={() => this.setState({ isOpen: false })}
            />
        )
    }
    private renderSelector() {
        return (
            <Segment>
                <Input ref={this.searchInput} fluid={true} icon="search" iconPosition="left" />
            </Segment>
        )
    }
}

最新更新