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

Re-Opening dialog doesn't update() inputs #2149

Open
adosikas opened this issue Dec 10, 2023 · 9 comments
Open

Re-Opening dialog doesn't update() inputs #2149

adosikas opened this issue Dec 10, 2023 · 9 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@adosikas
Copy link
Contributor

adosikas commented Dec 10, 2023

Description

I am trying to "hide" some settings in a dialog, but whenever I reopen the dialog, the initial value from page load is reset, seeming ignoring binds.

But the bind actually works correctly, the input is just displaying it wrong.
The label changes as expected when I modify the input, but the input default value (either from the element or from the storage) is "restored" every time the dialog opens.

from nicegui import ui, app

@ui.page("/")
def index():
    with ui.dialog() as edit_dialog, ui.card():
        text_input = ui.input("Text", value="Default").bind_value(app.storage.user, "text")
    ui.button("Edit", on_click=edit_dialog.open)
    ui.label().bind_text_from(text_input, "value")

ui.run(storage_secret="123")

I can work around it by manually update all contained inputs when opening the dialog (before or after .open() doesn't actually matter), but that hardly seems intentional.

@adosikas adosikas changed the title Re-Opening dialog resets inputs Re-Opening dialog doesn't update() inputs Dec 10, 2023
@falkoschindler
Copy link
Contributor

Oh wow, thanks for reporting this issue, @adosikas!

Here is a reproduction without binding and without app storage:

model = {'text': 'test'}
with ui.dialog() as dialog, ui.card():
    input_ = ui.input(value=model['text'], on_change=lambda e: model.update(text=e.value))
ui.button('Edit', on_click=dialog.open)
ui.label().bind_text_from(input_, 'value')

It looks like the problem is that the HTML input doesn't exist while the dialog is closed. Vue re-creates it when the dialog is opened, but somehow an outdated value is set. I wonder if we can reproduce the behavior with a plain Vue app...

@falkoschindler falkoschindler added the bug Something isn't working label Dec 12, 2023
@falkoschindler
Copy link
Contributor

falkoschindler commented Dec 12, 2023

A plain Vue app works as expected:

<html>
  <head>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  </head>
  <body>
    <div id="app">
      <button @click="showDialog = true">Edit</button>
      <div v-if="showDialog" style="border: 1px solid black; padding: 10px">
        <input id="inputText" v-model="text" />
        <button @click="showDialog = false">Close</button>
      </div>
      <p>Entered Text: {{ text }}</p>
    </div>
    <script>
      Vue.createApp({
        data() {
          return { text: "test", showDialog: false };
        },
      }).mount("#app");
    </script>
  </body>
</html>

@falkoschindler
Copy link
Contributor

A Quasar app works as well:

<html>
  <head>
    <link href="https://cdn.jsdelivr.net/npm/quasar@2.14.1/dist/quasar.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="q-app">
      <q-btn label="Edit" @click="showDialog = true"></q-btn>
      <q-dialog v-model="showDialog">
        <q-card>
          <q-input v-model="text"></q-input>
          <q-btn label="Close" @click="showDialog = false"></q-btn>
        </q-card>
      </q-dialog>
      <div>Entered Text: {{ text }}</div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@2.14.1/dist/quasar.umd.js"></script>
    <script>
      const app = Vue.createApp({
        data() {
          return { text: "test", showDialog: false };
        },
      });
      app.use(Quasar);
      app.mount("#q-app");
    </script>
  </body>
</html>

@falkoschindler falkoschindler added the help wanted Extra attention is needed label Dec 12, 2023
@falkoschindler
Copy link
Contributor

The difference might be that NiceGUI doesn't use v-model...

@meslahik
Copy link

meslahik commented Dec 12, 2023

Good to see the problem here, I totally forgot to report this issue.

My use case was also an edit button for some data that opened a dialog: first time user edits an initial value 11 to 22, next time when they click the edit button, they do not see 22 but 11 inside the dialog.

My workaround was to create the dialog each time:

from nicegui import ui

def create_dialog():
    with ui.dialog(value=True) as dialog, ui.card():
        ui.input(value=model['text'], on_change=lambda e: model.update(text=e.value))

model = {'text': 'test'}
ui.button('Edit', on_click=create_dialog)
ui.label().bind_text_from(model, 'text')


ui.run()

@rayuel

This comment was marked as off-topic.

@falkoschindler

This comment was marked as off-topic.

@rayuel

This comment was marked as off-topic.

@falkoschindler

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants