-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add the form_action property to manually trigger form actions
#98
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@modelscope-studio/antd': minor | ||
| 'modelscope_studio': minor | ||
| --- | ||
|
|
||
| feat: add the `form_action` property to manually trigger form actions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import gradio as gr | ||
| import modelscope_studio.components.antd as antd | ||
| import modelscope_studio.components.base as ms | ||
|
|
||
|
|
||
| def submit(form_value): | ||
| print(form_value) | ||
|
|
||
|
|
||
| def add(state_value): | ||
| count = len(state_value) | ||
| return state_value + [{ | ||
| "form_name": str(count), | ||
| "label": "Label " + str(count) | ||
| }] | ||
|
|
||
|
|
||
| with gr.Blocks() as demo, ms.Application(), antd.ConfigProvider(): | ||
| state = gr.State([{"form_name": "0", "label": "Label 0"}]) | ||
| with antd.Form() as form: | ||
| with antd.Form.Item(): | ||
| add_btn = antd.Button("Add List") | ||
|
|
||
| @gr.render(inputs=[state]) | ||
| def render_inputs(state_data): | ||
| for item in state_data: | ||
| with antd.Form.Item(form_name=item["form_name"], | ||
| label=item["label"]): | ||
| antd.Input() | ||
|
|
||
| with antd.Form.Item(): | ||
| antd.Button("Submit", type="primary", html_type="submit") | ||
| add_btn.click(fn=add, inputs=[state], outputs=[state]) | ||
| form.finish(fn=submit, inputs=[form]) | ||
|
|
||
| if __name__ == "__main__": | ||
| demo.queue().launch() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import gradio as gr | ||
| import modelscope_studio.components.antd as antd | ||
| import modelscope_studio.components.base as ms | ||
|
|
||
|
|
||
| def on_submit(_form): | ||
| print(_form) # the Form Component will automatically collect the form data | ||
|
|
||
|
|
||
| def bind_action_event(action): | ||
|
|
||
| def on_action(): | ||
| return gr.update(form_action=action) | ||
|
|
||
| return on_action | ||
|
|
||
|
|
||
| with gr.Blocks() as demo: | ||
| with ms.Application(): | ||
| with antd.ConfigProvider(): | ||
| with antd.Card(title="Out of the Form"): | ||
| submit_btn = antd.Button("Submit", type="primary") | ||
| reset_btn = antd.Button("Reset") | ||
| validate_btn = antd.Button("Validate") | ||
|
|
||
| with antd.Form(label_col=dict(span=8), | ||
| wrapper_col=dict(span=16)) as form: | ||
| with antd.Form.Item( | ||
| form_name="username", | ||
| label="Username", | ||
| rules=[{ | ||
| "required": True, | ||
| "message": 'Please input your username!' | ||
| }, { | ||
| "pattern": | ||
| "^[a-zA-Z0-9]+$", | ||
| "message": | ||
| "Username can only contain letters and numbers!" | ||
| }, { | ||
| "min": | ||
| 6, | ||
| "message": | ||
| "Username must be at least 6 characters long!" | ||
| }, { | ||
| "max": | ||
| 20, | ||
| "message": | ||
| "Username must be at most 20 characters long!" | ||
| }]): | ||
| antd.Input() | ||
| with antd.Form.Item( | ||
| form_name="password", | ||
| label="Password", | ||
| rules=[ | ||
| { | ||
| "required": True, | ||
| "message": 'Please input your password!' | ||
| }, | ||
| { | ||
| # custom validator with javascript function | ||
| "validator": | ||
| """(rule, value, cb) => { | ||
| if (value !== '123') { | ||
| cb('Password must be "123"') | ||
| } | ||
| cb() | ||
| }""" | ||
| } | ||
| ]): | ||
| antd.Input.Password() | ||
|
|
||
| with antd.Form.Item(wrapper_col=dict(offset=8, span=16)): | ||
| antd.Button("Submit", type="primary", html_type="submit") | ||
| form.finish(on_submit, inputs=[form]) | ||
| submit_btn.click(bind_action_event("submit"), outputs=[form]) | ||
| reset_btn.click(bind_action_event("reset"), outputs=[form]) | ||
| validate_btn.click(bind_action_event("validate"), outputs=[form]) | ||
|
|
||
| if __name__ == "__main__": | ||
| demo.queue().launch() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,30 +2,51 @@ import { sveltify } from '@svelte-preprocess-react'; | |
| import type { SetSlotParams } from '@svelte-preprocess-react/slot'; | ||
| import { useEffect } from 'react'; | ||
| import { useFunction } from '@utils/hooks/useFunction'; | ||
| import { useMemoizedFn } from '@utils/hooks/useMemoizedFn'; | ||
| import { renderParamsSlot } from '@utils/renderParamsSlot'; | ||
| import { Form as AForm, type GetProps } from 'antd'; | ||
|
|
||
| export const Form = sveltify< | ||
| GetProps<typeof AForm> & { | ||
| value: Record<string, any>; | ||
| onValueChange: (value: Record<string, any>) => void; | ||
| setSlotParams: SetSlotParams; | ||
| }, | ||
| ['requiredMark'] | ||
| >( | ||
| export interface FormProps extends GetProps<typeof AForm> { | ||
| value: Record<string, any>; | ||
| onValueChange: (value: Record<string, any>) => void; | ||
| setSlotParams: SetSlotParams; | ||
| formAction?: 'reset' | 'submit' | 'validate' | null; | ||
| onResetFormAction: () => void; | ||
| } | ||
|
|
||
| export const Form = sveltify<FormProps, ['requiredMark']>( | ||
| ({ | ||
| value, | ||
| formAction, | ||
| onValueChange, | ||
| requiredMark, | ||
| onValuesChange, | ||
| feedbackIcons, | ||
| setSlotParams, | ||
| slots, | ||
| onResetFormAction, | ||
| ...props | ||
| }) => { | ||
| const [form] = AForm.useForm(); | ||
| const feedbackIconsFunction = useFunction(feedbackIcons); | ||
| const requiredMarkFunction = useFunction(requiredMark); | ||
| const onResetFormActionMemoized = useMemoizedFn(onResetFormAction); | ||
|
|
||
| useEffect(() => { | ||
| switch (formAction) { | ||
| case 'reset': | ||
| form.resetFields(); | ||
| break; | ||
| case 'submit': | ||
| form.submit(); | ||
| break; | ||
| case 'validate': | ||
| form.validateFields(); | ||
| break; | ||
| } | ||
| onResetFormActionMemoized(); | ||
| }, [form, formAction, onResetFormActionMemoized]); | ||
|
Comment on lines
+35
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity and to prevent unintended side effects, it's better to add a guard clause to ensure that |
||
|
|
||
| useEffect(() => { | ||
| if (value) { | ||
| form.setFieldsValue(value); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The custom validator logic is incorrect. The callback
cbis called unconditionally at the end, which will likely cause the validation to always pass, regardless of the input value. Thecb()call should only be made once per validation path. Usingreturnensures the function exits after calling the callback, which is a robust pattern for this kind of validator.