一个TextField的两个onChange事件未同时执行两个onChange事件

时间:2020-04-03 22:56:47

标签: reactjs uitextfield office-js onchange tsx

在下面的代码中,我试图为一个TextField执行两个onChange事件。我尝试分别执行两个onChange事件,它们工作正常。为什么我的两个事件不能同时运行?当我组合它们时,仅执行“ handleChange”事件,而不执行“ _onChange”事件。我在做什么错了?

import * as React from 'react';
import { TextField } from 'office-ui-fabric-react/lib/TextField';
import { Stack, IStackProps } from 'office-ui-fabric-react/lib/Stack';
export interface TestState {
  [key: string]: TestState[keyof TestState];
  descriptionVal: string;
  multiline: boolean;
}

export default class Test extends React.Component<{}, TestState> {
  constructor(props: {}) {
    super(props);
    this.state = {
      descriptionVal: "",
      multiline: false
    };

  }

  public render(): JSX.Element {
    const columnProps: Partial<IStackProps> = {
      tokens: { childrenGap: 15 },
      styles: { root: { width: 300 } },
    };

    return (
      <Stack horizontal tokens={{ childrenGap: 50 }} styles={{ root: { width: 650 } }}>

        <Stack {...columnProps}>
          <TextField
            label="Switches from single to multiline if more than 50 characters are entered"
            multiline={this.state.multiline}
            onChange={this.twoCalls}
            value={this.state["descriptionVal"]}
          />
        </Stack>
      </Stack>
    );
  }
  twoCalls = e => {
    {this.handleChange("descriptionVal")(e)}
    {this._onChange}
  }
  private _onChange = (ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newText: string): void => {
    const newMultiline = newText.length > 50;
    if (newMultiline !== this.state.multiline) {
      console.log(ev);
      this.setState({ multiline: newMultiline });
    }
  };
    handleChange = (field: string) => (event: any) => {
    const value = event.target.value.toUpperCase();
    this.setState({ [field]: value });
  };
  }

2 个答案:

答案 0 :(得分:1)

首先,语句周围没有多余的花括号。其次,您实际上并没有调用第二个功能this.function !== this.function()。您应该执行以下操作:

twoCalls = e => {
    this.handleChange("descriptionVal")(e);
    this._onChange(e);
}

答案 1 :(得分:0)

“ twoCalls”必须是这样

onFieldSubmitted
相关问题