Skip to content

Conversation

@tconkling
Copy link
Contributor

@tconkling tconkling commented May 18, 2021

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).

  • FormsManager is now folded into WidgetStateManager.
  • When a form is submitted, WidgetStateManager emits a signal. All widgets in the form respond to this signal by resetting their values locally and committing those values back to the WidgetStateManager.
  • This resetting only happens on the frontend - the server doesn't know that values have been reset (this is by design).
  • FormClearHelper is a utility class that implements a useEffect-like pattern for subscribing to the formCleared events. (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.)
  • Each widget has a new test for clearOnSubmit behavior.
  • st.form_submit_button now takes an optional "help" parameter.

(Contains changes from #3286, which should be reviewed first)

@tconkling tconkling requested review from a team and asaini May 18, 2021 00:39
@tconkling tconkling requested a review from kantuni May 18, 2021 00:52

if (isValidFormId(node.deltaBlock.formId)) {
const { formId } = node.deltaBlock
if (node.deltaBlock.type === "form") {
Copy link
Collaborator

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?

Copy link
Contributor Author

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 {
Copy link
Collaborator

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(
Copy link
Collaborator

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 componentDidMount and componentDidUpdate?

Copy link
Contributor Author

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(
Copy link
Collaborator

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.

Copy link
Contributor Author

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

@asaini
Copy link

asaini commented May 21, 2021

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)
  • By default, clear_on_submit is false
  • Screenshot immediately after changing slider to 20 and pressing Submit

Screen Shot 2021-05-20 at 7 13 54 PM

* 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
@tconkling tconkling merged commit 216d7d2 into streamlit:develop May 24, 2021
@tconkling tconkling deleted the tim/FormClearOnSubmitNew branch May 24, 2021 18:33
tconkling added a commit to tconkling/streamlit that referenced this pull request May 25, 2021
# 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
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

Successfully merging this pull request may close these issues.

3 participants