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.

Default values on custom widgets from outside sources

See original GitHub issue

Prerequisites

Description

I am trying to set custom components to have default values. However, these default values don’t come from the schema, but other external sources. The problem I am having is that if I have a page with multiple custom widgets that set the default value on creation, only the last widget in the form has the default value set. In the example provided I made a simple Date field that defaults to today’s value. On submission you can see that only the last date in the form actually has a value.

We did manage to solve this by wrapping the onChange call in a useEffect hook and adding a timeout to the onChange. This JSFiddle shows how we have it setup, but for some reason I cannot get React.useEffect to work on the JSFiddle setup so the example doesn’t actually work…

So my main question is, is this the correct / best available way to set a default value on a custom widget? I know that onChange should really only be called when the value has actually changed, but I couldn’t find another function available to the Widget that allowed me to set the value.

Steps to Reproduce

  1. Create a custom component that calls onChange during creation to set a value.
  2. Load a form that has multiple custom components that do this.
  3. Submit form and see only one preset value come through

Expected behavior

All preset values are submitted

Actual behavior

Only last preset value is submitted

Version

1.8.1

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
gonzaloaunecommented, Jul 17, 2021

Thanks @ivanlewin, I ended up doing something similar but abandoned the use of this library altogether for something that I can have more control of. Thanks for the tip though!

0reactions
jcbrockschmidtcommented, Dec 12, 2022

On mount, I set a timeout that, after 1 ms, calls onChange with the value for the current date. (I am not sure if the cleanup function is necessary since we’re talking about 1ms here, but it’s my attempt at good practices).

    useEffect(() => {
        if (props.value === undefined) {
            const timeoutId = setTimeout(
                () => props.onChange(dayjs().format('YYYY-MM-DD')),
                1
            );
            return () => clearTimeout(timeoutId);
        }
    }, []);

(I’m on v2.5.1 so I don’t know if this is solved on v3)

For anyone stumbling across this, I was able to simplify this to something like

    useEffect(() => {
        if (props.value === undefined) {
            return () => props.onChange(dayjs().format('YYYY-MM-DD'));
        }
    }, []);

I’m on v4.2.0. The original issue appears to be unsolved still.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Define Default Field Values - Salesforce Help
Define a default value for a field. Use a formula to generate dynamic values or constants for static values.Required Editions and User Permissions...
Read more >
Default values of field widgets Drupal 8 - Stack Overflow
I have a custom field that extends the ImageField to add a checkbox to the image, similarly to how the image field contains...
Read more >
tkinter.ttk — Tk themed widgets — Python 3.11.1 documentation
The first element is the default image name. The rest of the list if a sequence of statespec/value pairs as defined by Style.map()...
Read more >
Create custom GUI Widgets for your Python apps with PyQt5
Build a completely functional custom widget from scratch using ... the Qt standard of camelCase method names for these external methods for ...
Read more >
Creating forms from models - Django documentation
Use a custom form field or widget if you're designing an API and want the default fallback behavior for a field that uses...
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