reactjsreact-hooksreact-admin

React Admin SelectInput in a popup


I have the following requirement for my project using React Admin. In an edit screen, I'd like to customise the selectInput component so that it allows me to be not a drop-down list but a choice in a popup. When I click on the input I want a popup to open, and I'd like to know how I can change the value of the selectInput knowing that it comes from a component source. The main problem is that I don't know how to change the value of the input manually so that it fits into the logic of React Admin.


Solution

  • Setting a field value in a form goes something like this useFormState, setValue:

    import { useFormContext } from 'react-hook-form'
    import { SelectInput } from 'react-admin' 
    
    function MySelectInput() {
      const { setValue } = useFormContext()
      const value_from_popup = ... 
    
      useEffect(() => {
        setValue('my_field', value_from_popup, { shouldValidate: true, shouldDirty: true })  
      }, [value_from_popup, setValue])
    
      return <SelectInput source="my_field" choices={ ... } />
    }