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.

better support for const

See original GitHub issue

Prerequisites

Description

Currently, const is only used to validate the form data, but there is no UX to enforce it. This is a proposal to add more support of const.

Steps to Reproduce

https://mozilla-services.github.io/react-jsonschema-form/#eyJmb3JtRGF0YSI6e30sInNjaGVtYSI6eyJ0aXRsZSI6IkEgcmVnaXN0cmF0aW9uIGZvcm0iLCJkZXNjcmlwdGlvbiI6IkEgc2ltcGxlIGZvcm0gZXhhbXBsZS4iLCJ0eXBlIjoib2JqZWN0IiwicHJvcGVydGllcyI6eyJmaXJzdE5hbWUiOnsidHlwZSI6InN0cmluZyIsInRpdGxlIjoiRmlyc3QgbmFtZSIsImNvbnN0IjoiQ2h1Y2sifX19LCJ1aVNjaGVtYSI6e319

Expected behavior

const should be respected in various areas:

  • default form values
  • readonly / disabled form fields
  • any other places?

Actual behavior

Only form validation respects const. It does not get applied as a default form state and there is no UI indiciation that a field cannot be changed from a certain value.

Version

1.3.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
rap1dscommented, Oct 6, 2022

Hi, we have forms where we use const values. We don’t need/want to show the input field for those. Thus, we use ui:widget: "hidden". There are a couple of situations where the library behaves a bit differently from what we’d expect:

1. Const + hidden widget

A form with const + hidden widget is always invalid

Playground example

What I’d expect: const value is automatically picked as the value of the hidden field

Actual: const value is not automatically picked, making the form always invalid

The workaround for this is to use default, but it also fails when the form data already exists, see the example below.

2. Const + default + hidden widget

A form with const + default + hidden widget is always invalid if form data already contains data (e.g. data loaded from server) that doesn’t match const. Because the field is hidden, there’s no way in the UI to make the form valid.

Playground example

Expected: const value overrides the existing value in form data.

Actual: Old value is not overriden, no way to change and the form. Form is always invalid.

3. Const with complex data + hidden widget

We’d also want to use const with other data than primitives. But we don’t need/want to show anything in the UI, thus we tried to use the hidden widget. However, hidden widget doesn’t support complex data.

Playground example

Expected: const value would be added to form data

Actual: const value is not added to form data because hidden widget doesn’t support complex data

I hope these examples provide some context if you plan to work with this issue!

Thanks for the library, awesome work!

1reaction
a-b-r-o-w-ncommented, Apr 1, 2019

@epicfaace

IMO, the default should be set to the const because the form would be invalid if using an incorrect default field. A readonly field makes sense to me. I think in the case of the single enum field, I would expect the form to render a dropdown with 1 element even though a const might be the same thing. In my own usage, we sometimes populate the enum dynamically and it could only have 1 element, and is semantically different than a const. I would like for our users to know that there could be more options, but just not now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"const" | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
better support for const · Issue #1241 · rjsf-team/react- ...
Currently, const is only used to validate the form data, but there is no UX to enforce it. This is a proposal to...
Read more >
const - JavaScript - MDN Web Docs - Mozilla
The const declaration creates block-scoped constants, much like variables ... Tip: you can click/tap on a cell for more information.
Read more >
Browser Compatibility Testing of JAVASCRIPT const
BROWSER SUPPORT FOR JAVASCRIPT const · Google Chrome · Mozilla Firefox · Internet Explorer · Safari · Microsoft Edge · Opera.
Read more >
'as const' in Typescript, better constant object support in IDE
In most projects we have some kind of global config object. You may store there urls to API endpoints... Tagged with typescript, beginners, ......
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