Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating .value on VSCodeTextField does not update text in the field #271

Open
alisonlhart opened this issue Jan 30, 2025 · 5 comments
Open

Comments

@alisonlhart
Copy link

alisonlhart commented Jan 30, 2025

Currently, updating the VSCodeTextField value attribute doesn't appear to edit the text inside the field, only the "value" element property. This only occurs if the text in the field has been edited.

This may be related to the shadow root where an field is located under a VSCodeTextField element.

For example, say the nameTextField has had "test" entered into it, and then the following executes:

let nameTextField = document.getElementById(
    "test-name",
  ) as VscodeTextfield;
nameTextField.value = "";

The actual text in the field will remain "test", while the input element's value attribute will be set to "".

Accessing the input element directly under the shadow root does permit updating the text inside the field, e.g.:

let inputElement = nameTextField.shadowRoot?.querySelector(
    "#input",
  ) as HTMLInputElement;
inputElement.value = "";

This works to reset the text, but not the value attribute of the input element.

Ideally, both the value attribute and the text in the field itself should be modified by setting .value on the VSCodeTextField itself.

@bendera
Copy link
Member

bendera commented Jan 30, 2025

I couldn't reproduce it. Did I miss something?

Screen.Recording.2025-01-30.202949.mp4

@alisonlhart
Copy link
Author

alisonlhart commented Jan 31, 2025

Hmm, that's very strange. That is the same pattern we're using, but the text just doesn't clear. Here's what I'm seeing:

VSCodeTextField id="namespace-name", entering the string "test" into the field changes the value attribute, and getting the element by ID returns "test". Using .value to set it to an empty string sets the value attribute="", but the text in the field remains unchanged.

Screencast.from.2025-01-31.10-42-08.webm

@bendera
Copy link
Member

bendera commented Jan 31, 2025

You changed the value attribute inside the component on the wrapped <input>, but not on the component itself. Then, you selected the <vscode-textfield> component via getElementById and changed its value to an empty string. However, this didn't trigger a re-render because, from the <vscode-textfield> component's perspective, nothing had changed.

Despite this, the component behaves unexpectedly, in terms of how I had planned it. The value property of a native input differs from the value attribute. The value attribute is the default value of the input, while the property is the actual value of it. So, if a native input contains text (e.g., 'test') and you set its value attribute to 'default text', nothing will happen. The input's actual value will change to 'default text' only when the form is reset.

I thought this could be misleading, so I implemented it in a way that the 'value' in the <vscode-textfield> always represents the actual value, and I created a separate property for the default value. However, in the render function, I mistakenly change the inner input's value attribute instead of the property. I should fix this, although I'm not sure if it will resolve your issue.

@alisonlhart
Copy link
Author

@bendera I believe that solution should resolve this, based on the behavior I'm seeing.

@bendera
Copy link
Member

bendera commented Jan 31, 2025

Stay tuned, I will publish a pre-release in a few minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants