I created a Formik form that uses Fluent UI v9 components on a Formik form (Input, Textarea and Dropdown). The first 2 work as expected but I cannot figure out how to wire up the Dropdown, resulting in two issues: (a) the dropdown does not update to show the current selection and (b) the state variable for the Dropdown is never updated.
Here is my code (simplified to show Dropdown only). The issue seems to be related to the fact that the severity field in initialValues does not get mapped to the Dropdown component, but I'm guessing.
import React from 'react';
import {Formik, Form} from 'formik';
import {FluentProvider, Label, Dropdown, teamsLightTheme } from '@fluentui/react-components';
// these arrays should be imported from tables
const options=[
{value:0, label:"Not sure", disabled:false},
{value:1, label:"We have been hit by a malware attack", disabled:false },
{value:2, label:"Some or all people cannot work", disabled:false },
{value:3, label:"A critical application cannot be used" , disabled:false},
{value:4, label:"Work is slowed", disabled:false },
{value:5, label:"Work is not disrupted - we have a technical question", disabled:false },
];
export async function saveItem(t){
// just show the results for now
console.log(JSON.stringify(t, null, 2));
};
// DropdownBox wraps the fluent-ui Dropdown component with a label and error message
export function DropdownBox({ label, options, ...props }){
const [field, meta] = useField(props);
const [selectedOptions,setSelectedOptions] = React.useState([options[field.value].label]);
const [value,setValue] = React.useState(field.value);
function onOptionSelect(ev, item){
setSelectedOptions(item.selectedOptions);
setValue(item.optionItem);
};
return (
<div>
<Label htmlFor={props.name}>{label}</Label>
<Dropdown {...field} {...props} defaultValue={options[field.value].label} selectedOptions={selectedOptions} onOptionSelect={onOptionSelect} >
{options.map((option) => (
<Option key={option.value} value={option.value} disabled={option.disabled}>
{option.label}
</Option>
))}
</Dropdown>
{meta.touched && meta.error ? (
<div className="error">{meta.error}</div>
) : null}
</div>
);
};
export default function MyForm(){
let item={
impact: 1, // the key value of the item selected
}
return (
<FluentProvider theme={teamsLightTheme}>
<Formik
initialValues={item}
onSubmit={async (values,{setSubmitting}) =>{
setTimeout(async ()=>{
await saveItem(values);
setSubmitting(false);
},(400));}}
>
<Form >
<DropdownBox
id="impact"
name="impact"
label="Business Impact"
options={options}
/>
</Form>
</Formik>
</FluentProvider>
);
};