-
Notifications
You must be signed in to change notification settings - Fork 4k
st.form: clear_on_submit #3287
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
st.form: clear_on_submit #3287
Conversation
* develop: Fix inactive DatePicker if the date value is 10 years earlier (streamlit#3241) Upgrade trim to 0.0.3 or later (streamlit#3250) Manually name all snapshots and generate missing ones (streamlit#3205)
|
|
||
| if (isValidFormId(node.deltaBlock.formId)) { | ||
| const { formId } = node.deltaBlock | ||
| if (node.deltaBlock.type === "form") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we keep the block types in an enum?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are defined as a type union, so they're type-safe:
public type?: ("vertical"|"horizontal"|"column"|"expandable"|"form")
| import { WidgetStateManager } from "src/lib/WidgetStateManager" | ||
| import { SignalConnection } from "typed-signals" | ||
|
|
||
| export class FormClearHelper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should open an issue to translate our widgets to hooks, which will eliminate this logic.
| const style = { width } | ||
|
|
||
| // Manage our form-clear event handler. | ||
| this.formClearHelper.useFormClearListener( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why should this be called inside
render? - Can we move this logic to
componentDidMountandcomponentDidUpdate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just follows the hooks pattern - it's logic that should be call on every update, and render is the function that's also called on every update. It could be moved into componentDidMount and componentDidUpdate - but this wouldn't change the functionality, and would introduce another call site.
| * Hooks-based widgets can just use `useEffect` and call | ||
| * `widgetMgr.addFormClearedListener` directly. | ||
| */ | ||
| public useFormClearListener( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should use useFoo naming here. It mimics a hook, but it isn't one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to manageFormClearListener
* develop: Widget test cleanup (streamlit#3286) pin click to < 8.0 (streamlit#3256) Up version to 0.82.0 Update change log
|
Looking good 🚢🚢🚢 Verified in PR Preview. Notes: import streamlit as st
with st.form(key='my_form', clear_on_submit=True):
number = st.number_input('Enter a number')
slider = st.slider('Enter a value', 0, 100, 50)
submit_button = st.form_submit_button()
if submit_button:
st.write('slider=', slider)
|
* develop: Init test variables in beforeEach Init test variables in beforeEach Add database tutorials to docs (streamlit#3272) update contact email to support (streamlit#3282) Better typing Remove dummy script Add testing and some comments Fix the bug, don't love the solution Improve test name Add script for testing Fix test after merge Make sure selected options aren't visible in the dropdown Add test for the fuzzy filter Import filter function from another component
# By Ken McGrady (19) and others # Via GitHub (9) and others * develop: (96 commits) st.form: clear_on_submit (streamlit#3287) Init test variables in beforeEach Init test variables in beforeEach Add database tutorials to docs (streamlit#3272) update contact email to support (streamlit#3282) Better typing Remove dummy script Add testing and some comments Fix the bug, don't love the solution Improve test name Add script for testing Fix test after merge Make sure selected options aren't visible in the dropdown Widget test cleanup (streamlit#3286) Add test for the fuzzy filter Import filter function from another component Remove tag from PR template (streamlit#3284) Add test req version bound for TF to fix tests (streamlit#3266) pin click to < 8.0 (streamlit#3256) Up version to 0.82.0 ... # Conflicts: # frontend/src/components/shared/Dropdown/Selectbox.test.tsx

Implements
clear_on_submit, a new optional parameter for st.form. When a form has this flag, it means that the frontend "resets" all the form's widgets when the form is submitted.CustomComponents are not currently affected by this flag (this is by design - properly handling this would require a CustomComponents API update, and all widget-like components in the wild would need updating. So we're punting on it for now).
FormsManageris now folded intoWidgetStateManager.WidgetStateManageremits a signal. All widgets in the form respond to this signal by resetting their values locally and committing those values back to the WidgetStateManager.FormClearHelperis a utility class that implements auseEffect-like pattern for subscribing to theformClearedevents. (None of our widgets are currently implemented using React hooks, so this is a workaround for easily and correctly composing that behavior into each widget.)clearOnSubmitbehavior.st.form_submit_buttonnow takes an optional "help" parameter.(Contains changes from #3286, which should be reviewed first)