Programmatically changing the value of an input not working
See original GitHub issueHey 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:
- Created a year ago
- Comments:8 (5 by maintainers)
Top 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&#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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@slax57
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.
@jogibs I believe you need to use
useFormContext
instead ofuseForm
. 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), anduseFormContext
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