2

I have a Dropdown Field with options and a Textfield which should be used to store user input in an object array with key value pairs. This happens in the onchange event of textfield. Additionally the textfield should retrieve the data out of the array and show the value on change of the dropdown in the same textfield.

I am not sure if this is possible, I only tried to set the "defaultValue" attribute of the textfield dynamically.

The part with saving userinput in the object array works fine. But the value in the textfield doesnt change on onchange of dropdown. I get the selected value and therefore also the value out of the array while debugging.

I have also a problem with setting the initial value of dropdown element. I dont want it to be blank on init, instead it should have one of the values out of the optionset which I provide.

Dropdown: Where I set selectedValue

<Dropdown

    defaultValue={this.options["foo"]}  //<-- This doesnt work too
    id="Dropdown"
    options={this.options}
    onChange={(event: React.FormEvent<HTMLDivElement>, option?: IDropdownOption): void => {
       this.setState({
                        selectValue : option?.text
                     }, this.onNameChanged);
                     }
             }
 />

Textfield:

<TextField
 id="inputVarLang"
 defaultValue={this.state.arrayObj[this.state.selectValue as any]}
 placeholder={"---"}
 onChange={(event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement | HTMLDivElement>, 
 newValue?: string): void => {
                         this.setState({
                                   arrayObj: update(this.state.arrayObj 
                                   {[this.state.selectValue as any]: {$set: newValue ?? ""}})
                                        }, this.onNameChanged);
                             }
           }/>

It would be great if someone could help me with this.

2
  • Whitch library do you use for Dropdown component (not material ui) ? Commented Dec 26, 2021 at 18:59
  • to initialize the dropdown, you need to use "defaultSelectedKeys" prop. see example here fabricweb.z5.web.core.windows.net/oufr/6.111.2/#/examples/… Commented Dec 26, 2021 at 19:38

1 Answer 1

1

You need to use "value" prop of TextField instead of defaultValue, like this

<TextField
 id="inputVarLang"
 value={this.state.arrayObj[this.state.selectValue as any]}
 placeholder={"---"}
 onChange={(event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement | HTMLDivElement>, 
 newValue?: string): void => {
                         this.setState({
                                   arrayObj: update(this.state.arrayObj 
                                   {[this.state.selectValue as any]: {$set: newValue ?? ""}})
                                        }, this.onNameChanged);
                             }
           }/>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god, thank you. It was that simple and I was trying to solve this like hell. I thought, I can't use "value" instead of "defaultValue" because this makes the textfield readonly. But its not the case. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.