Popual中的输入引用从未在Semantic UI React中获取

问题描述 投票:0回答:1

我在Semantic 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中找到Input,我也会得到一个null结果:

    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();
        }
    }

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

谢谢!

reactjs typescript semantic-ui-react
1个回答
0
投票

这是解决方案,如下所示: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>
        )
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.