question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Programmatically changing the value of an input not working

See original GitHub issue

Hey there! I’m having an issue with programmatically changing the value of an input, but none of the options seem to work. See below:

What you were expecting: The value should be applied to the input

What happened instead: The value isn’t applied to the input

Related code:

import {
  Create,
  SimpleForm,
  required,
  TextInput,
  CreateProps,
  SelectInput,
  SelectInputProps,
  TextInputProps,
} from 'react-admin';
import { useWatch } from 'react-hook-form';

const commands = [{ id: 'test', name: 'Executes the test command', command: 'yarn test' }];

export const CommandCreateTypeInput = (props: SelectInputProps) => {
  // I've even have had an onChange={(event) => { setValue('command', event.target.value) }}
  // but that didn't work either
  return <SelectInput choices={commands} fullWidth {...props} />;
};

export const CommandCreateCommandInput = (props: TextInputProps) => {
  const type = useWatch({ name: 'type' });
  // const { setValue } = useForm();

  const value = commands.find((command) => {
    return command.id === type;
  })?.command;
  
  // Doesn't work
  // setValue('command', value);

  // Doesn't work either
  return <TextInput validate={required()} value={value} fullWidth {...props} />;
};

export const CommandCreate = (props: CreateProps) => (
  <Create {...props}>
    <SimpleForm>
      <CommandCreateTypeInput source="type" />
      <CommandCreateCommandInput source="command" />
    </SimpleForm>
  </Create>
);

Environment

  • React-admin version: 4.1.2

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
christiaanwesterbeekcommented, Oct 13, 2022

@slax57

useForm is to declare a new form (which is done internally by react-admin), and useFormContext is a helper to access the form context from within the form.

This is golden! Will print out on a tile and hang it in my office. Will save me more hours debugging my custom inputs and forms not playing ball with me.

1reaction
slax57commented, Jun 8, 2022

@jogibs I believe you need to use useFormContext instead of useForm. As we can see on the example in the react-hook-form doc, useForm is to declare a new form (which is done internally by react-admin), and useFormContext is a helper to access the form context from within the form.

Here is a working example for your case: https://codesandbox.io/s/zealous-glade-t8cykp?file=/src/posts/PostCreate.tsx

Btw, linking two input fields is documented here: https://marmelab.com/react-admin/Inputs.html#linking-two-inputs

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Trigger Change event when the Input value ...
When changing the value programmatically, you need to call the input event, not change event. Note: The input event is fired every time...
Read more >
Programatically changing input value does not fire Onchange ...
The expected behavior. Whenever the value is changed, on change event should be fired irrespective of the way the value was changed.
Read more >
Input field label doesn&#38;#039;t update styles when value
I have tried programmatically triggering various events, such as change, input, focus, and others, after updating the value and the result was the...
Read more >
Remove restriction on programmatically setting lightning-input ...
The lighting-input-field documentation states the following:"If a user enters anything in an input field, you can no longer programmatically set the value.
Read more >
<input type="text"> - HTML: HyperText Markup Language | MDN
A Boolean attribute which, if present, means this field cannot be edited by the user. Its value can, however, still be changed by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found