选择组丢失默认选择项

时间:2018-09-28 17:15:09

标签: javascript reactjs typescript

我在选择小组方面遇到问题。当窗格打开时,它将显示组并显示defaultSelectedKey。当我单击其他值时,我将检索一组不同的SharePoint列表。

我遇到的问题是,当我单击一个不同的值时,我会重新填充-this._locationsGroupOptions,而在重新渲染时,我会丢失defaultSelectedKey值。

关闭并重新打开面板后,它会再次正确显示。

<Panel
    isOpen={this.state.shouldShowLocationsSelectionPanel}
    ...
>
    <ChoiceGroup
        options={this._locationsGroupOptions}
        label="Branch Locations"
        onChange={this._onChangeLocationChoice.bind(this)}
        defaultSelectedKey={(this.state.location !== undefined && this.state.location.Id !== undefined) ? ""+this.state.location.Id : ""}
    ></ChoiceGroup>
</Panel>

我刷新了_locationsGroupOptions列表:

private _onChangeLocationChoice(ev: React.FormEvent<HTMLInputElement>, 
    options: IChoiceGroupOption): void {
    console.log("TruckSchedule | _onChangeLocationChoice | fired");

    for (let location of this.state.locations) {
        if (options.key === String(location.Id)) {
            console.log("TruckSchedule | _onChangeLocationChoice | Found the loction: ", location);
            this.setState({
                location: location
            },() => {
                this.props.updateLocation(this.state.location);

                this._loadLists()
                    .then(() => {});
            });

            break;
        }
        else {
            console.log("TruckSchedule | _onChangeLocationChoice | Could not find location: ", location);
        }
    }
}

_loadLists()中,我刷新了位置列表:

private _fetchLocations(): Promise<void> {  
    return (
        new LocationServices().getLocations()
        .then((newLocations) => {
            console.log("TruckDeliverySchedule | _fetchLocations | newLocations: ", newLocations);

            this._locationsGroupOptions = [];

            for (let location of newLocations) {
                this._locationsGroupOptions.push({key: ""+location.Id, text: location.Title});
            }

            this.setState({
                locations: newLocations, 
            });
        })
    );
}

执行此操作时,我丢失了defaultSelectedKey。我不明白为什么。新的位置在新的_locationsGroupOptions中,当我登录时就知道在那里。

enter image description here

我选择test02:

enter image description here

它会加载一组新的列表和显示,但是所选内容消失了。

0 个答案:

没有答案