0

I am using React semantic ui. I am rendering a dropdown in Fieldset. I have written code such that, once a option is selected, the options is updated such that the selected option is removed from the list. But when I select an option from the dropdown, the selected value is not displayed, rather it shows empty.

Here is my code: This is my dropdown code:

<Dropdown
name={`rows.${index}.mainField`}
className={"dropdown fieldDropdown"}
widths={2}
placeholder="Field"
fluid
selection  
options={mainFieldOptions}
value={row.mainField}
onChange={(e, { value }) => {
     setFieldValue(`rows.${index}.mainField`, value)
     updateDropDownOptions(value)                               
     }
  }                                                            
/>

My options:

let mainField = [
    { key: "org", text: "org", value: "org" },
    { key: "role", text: "role", value: "role" },
    { key: "emailId", text: "emailId", value: "emailId" },
]

Also, I have:

const [mainFieldOptions, setMainFieldOptions] = useState(mainField)

And,

  const updateDropDownOptions = (value:any) => {
        let updatedOptions: { key: string; text: string; value: string }[] = []
        mainFieldOptions.forEach(option => {
            if(option.key != value){
                updatedOptions.push({ key:option.key , text:option.key, value:option.key  })
            }
        })
        setMainFieldOptions(updatedOptions)
        console.log("mainfield", mainField)
    }

In onChange, if I dont call updateDropDownOptions() method, the dropdown value is set. But when I call the method, its giving blank value. Please help.

7
  • when you are selecting an option did you check whether the value is getting changed in the Formik ? . Commented Jul 13, 2021 at 6:39
  • If i do console.log(value), in onChange, the selected value is printed Commented Jul 13, 2021 at 6:42
  • That is not formik value , that is the value returned to you by the select component . Can you check the Formik state whether it is getting updated when you select an option ? . What do you get in {row.mainField} when you select an option ? Commented Jul 13, 2021 at 6:45
  • Not getting anythin in {row.mainField}. Its empty Commented Jul 13, 2021 at 6:51
  • can you add a sandbox with your current code ? . Commented Jul 13, 2021 at 6:53

1 Answer 1

1

There are few changes required in your code,

  1. You are pushing the entire initialValues when you are adding a row which is an [{}] but you need to push only {} so change your code to initialValues[0] in your push method.
  2. Its not needed to maintain a additional state for the options. You can filter the options based on the selected option in other rows which is available in the values.rows .

Util for filtering the options

const getMainFieldOptions = (rows, index) => {
  const selectedOptions = rows.filter((row, rowIndex) => rowIndex !== index);

  const filteredOptions = mainField.filter(mainFieldOption => !selectedOptions.find(selectedOption => mainFieldOption.value === selectedOption.mainField));
  return filteredOptions;
}

Call this util when rendering each row

values.rows.length > 0 &&
                values.rows.map((row, index) => {
                  const  mainFieldOptions = getMainFieldOptions(values.rows, index);

Working Sandbox

Sign up to request clarification or add additional context in comments.

Comments

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.