slackapi / bolt-python

A framework to build Slack apps using Python
https://slack.dev/bolt-python/
MIT License
1.03k stars 237 forks source link

Update/reset the state of a input field in a view #1036

Closed TechShivvy closed 4 months ago

TechShivvy commented 4 months ago

Question: Say we have a view/modal/form and there are two static_select input fields. I want the second static_select to reset/clear itself whenever I change my choice in first field. Like If an option is selected in both fields, it will be displayed as the chosen option, correct? If I modify the option in the first field, I want the second field to revert to its placeholder value (no selected option), just as it would be when the form is initially opened.

Reproducible in:

The slack_bolt version

slack-bolt==1.18.1 slack_sdk==3.26.2 slackeventsapi==3.0.1

Python runtime version

Python 3.10.13

OS info

Microsoft Windows [Version 10.0.19045.4046]

Steps to reproduce:

  1. Right now, the state of the view is stored here : body['views']['state']
  2. I can do this in the @app.action of the first field: body['views']['state']['values'][second_field_block_id][second_field_action_id]['selected_option'] = None.
  3. That's of no use, as there's no way right now (i guess?) to pass this state to the next state.
  4. I have tried ack with response_action and view. I don't think it does anything.
  5. I tried removing the second block and inserting a new second block(basically a new view, but with the same block_id and action_id) using view_update(). It's still not working! As the data persists if block_id and action_id are maintained.

Expected result:

Want the second field to reset(no selected option)

Actual result:

Obviously no effect on second field image

seratch commented 4 months ago

Hi @TechShivvy, thanks for asking the question! I'd suggest going with an approach like the "Multiple steps in a modal" in this example app: https://github.com/seratch/apidays-workshop-2020?tab=readme-ov-file#multiple-steps-in-a-modal

The example removes the first select menu in the next step for simplicity, but you can have both the first and second select menus in a single view if you want to do so. You can check the source code for more details.

I hope this helps. When you get a more specific question on how to implement your app, please feel free to post it with detailed information.

TechShivvy commented 4 months ago

Thank you for your response @seratch. Your multimodal code is quite helpful and almost fits my usecase. Just that I would also require form data from first view. I can pass it through private_metadata though. So, I'll go with this approach.

Also, If I were to rephrase my question, it would be "how to lose the state of specific selected input fields within a form?". Your code accomplishes this. The trick is to update the form twice – once without the selected fields, and then again with the selected fields. This process effectively removes the state of the selected variables from the view payload.

I'm curious if you know of any other repositories, similar to the one you shared and this bolt-python repo, where I can find more code examples or implementations.

Finally, Thank you for your guidance! This issue can be closed.

seratch commented 4 months ago

I'm curious if you know of any other repositories, similar to the one you shared and this bolt-python repo, where I can find more code examples or implementations.

Alternative way to accomplish this is to use a different block_id for a block when refreshing it. Handling view.state.values can be a bit complex with this (meaning matching with prefix etc.) but I stably removes existing selected item/values. I hope this helps.

It seems everything is clear now. Let me close this thread. Thanks again for writing in!

TechShivvy commented 4 months ago

Yeah, that approach also works, but as you mentioned, it adds complexity. By the way, there seems to be a bit of confusion 😅 @seratch . I was actually looking for more general Slack bot code examples, not specifically related to this issue. I've seen the main repo(this one), "question" labeled issues, and some of your codes. Any other recommendations?

seratch commented 4 months ago

Here are the examples I can recommend:

Hope this helps!