FastUI icon indicating copy to clipboard operation
FastUI copied to clipboard

Intial value does not work for forms

Open yjw1029 opened this issue 1 year ago • 1 comments

The initial value does not change the default value of different textareas. Similar issue exists in FormFieldInput.

Below is a simple demo: clicking the button will switch to different URLs, and the initial value of different question forms should be different.

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastui import prebuilt_html, FastUI, AnyComponent
from fastui import components as c
from fastui.events import GoToEvent
from pydantic import BaseModel, Field
from fastui.components.forms import FormFieldTextarea

# Create the app object
app = FastAPI()

# Root endpoint
@app.get(
    "/api/question/{question_id}",
    response_model=FastUI,
    response_model_exclude_none=True,
)
def api_index(question_id: str = None) -> list[AnyComponent]:
    return [
        c.PageTitle(text="FastUI Chatbot"),
        c.Page(
            components=[
                # Header
                c.Heading(text="Debug"),
                c.Button(
                    text="Q1",
                    named_style="secondary",
                    on_click=GoToEvent(url="/question/1"),
                ),
                c.Button(
                    text="Q2",
                    named_style="secondary",
                    on_click=GoToEvent(url="/question/2"),
                ),
                # Chat history
                c.Form(
                    form_fields=[
                        FormFieldTextarea(
                            name=f"answer",
                            title="回答",
                            rows=10,
                            initial=question_id,
                        ),
                    ],
                    loading=[
                        c.Spinner(
                            text="提交中...",
                            class_name="text-center",
                        )
                    ],
                    method="GOTO",
                    submit_url=".",
                ),
            ],
        ),
        # Footer
        c.Footer(extra_text="Made with FastUI", links=[]),
    ]


# Prebuilt HTML
@app.get("/{path:path}")
async def html_landing() -> HTMLResponse:
    """Simple HTML page which serves the React app, comes last as it matches all paths."""
    return HTMLResponse(prebuilt_html(title="FastUI Demo"))

When clicking the button Q2, the form's initial value does not change in the browser's rendering: image

However, it indeed changes in the HTML code. How should this issue be resolved in FastUI? image

yjw1029 avatar Mar 12 '24 04:03 yjw1029

If you want to solve it temporarily, maybe you can use placeholder instead of initial.

Dragon-GCS avatar Jul 01 '24 15:07 Dragon-GCS