seewindcn / reflex-antd

A Reflex wrapper for Antd
Apache License 2.0
21 stars 4 forks source link

form on_finish can not get default_value #8

Open lyc2017 opened 2 months ago

lyc2017 commented 2 months ago

form use on_finish,it can not get the for_items default_value

seewindcn commented 2 months ago

could you give more information or codes? maybe we can talk with Chinese?

lyc2017 commented 2 months ago

设置了默认值和不能为空的规则,提交on_finish()返回None,默认值不生效,但自己手动选择和输入之后能输出值,试了好几个组件都一样默认值on_finish()获取不到


class TestFormState(rx.State):
    form_data: dict = {}
    options = [{
        "value": 'jiangsu',
        "label": 'Jiangsu',
    },
        {
            "value": 'zhejiang',
            "label": 'Zhejiang',
    }]
    def handle_submit(self, form_data: dict):
        """Handle the form submit."""
        self.form_data = form_data
        print(self.form_data)

def test_form_items() -> list:
    return [
        entry.form_item(
            entry.cascader(
                default_value=[["jiangsu"], ["zhejiang"]],
                options=TestFormState.options,
                placeholder="请选择",
                multiple=True,
            ),
            width='100%',
            name="cascader",
            rules=[{"required": True, "message": "不能为空!"}],
        ),
        entry.form_item(
            entry.input(default_value="asdfasd"),
            name="input"
        ),
        entry.form_item(
            rx.button("提交", name="submit"),
        ),
    ]

def test_form() -> rx.Component:
    return entry.form(
        *test_form_items(),
        on_finish=TestFormState.handle_submit,
    )
seewindcn commented 2 months ago

参考:https://ant-design.antgroup.com/components/form-cn 在form中,组件中的default_value没用了; 需要配置form.initial_values,如:

initial_values: dict[str, str|list] = dict(cascader=[["jiangsu"], ["zhejiang"]], input='asdfasd')
initial_values=CasState.initial_values

另外,还有个别组件(如checkbox),submit也拿不到值,需要修改其form_item的 value_prop_name='checked'

seewindcn commented 2 months ago

另外,建议熟悉下.web里面的next.js代码(特别是里面pages目录下的);对于一些不解的问题,可以直接在里面改前端代码,查看具体效果;

lyc2017 commented 2 months ago

感谢,修改能获取默认值了

lyc2017 commented 2 months ago

还有一个小小的问题,range_picker日历的日期默认值传什么格式的?试了很多都无效,对前端js不熟悉

seewindcn commented 2 months ago

range_picker日历组件其相关的的date_picker一系列组件,都用的dayjs数据,这个有点麻烦,dayjs是前端的对象类型,不能直接通过state从后端设置后作用在前端; 我当前的做法是用JsLocalDictVar,类似的代码如下:

    _values = helper.JsLocalDictVar(
        'form_params', MainDomainState.form_state['form_params'],
        """ 
        values['register_time'] = values['register_time'] ? dayjs(values['register_time']) : null
        values['expire_time'] = values['expire_time'] ? dayjs(values['expire_time']) : null
        values['beian_time'] = values['beian_time'] ? dayjs(values['beian_time']) : null
        """,
    )

MainDomainState.form_state['form_params']是关联后端的state属性; JsLocalDictVar具体是用到前端的useState, useEffect去监听这个state的内容变动,然后执行上面字符串js的代码,从而将state的字典内容,更新到一个本地变量中; 再用本地变量赋值给组件属性;initial_values=_values

lyc2017 commented 2 months ago

好复杂我研究一下,不行就不设置默认值了,还是非常感谢大佬移植ant,省掉了很多功夫

seewindcn commented 2 months ago

ant这个组件做管理后台挺不错的,我也是需要用到就快速的封装了下;封装得并不太好,见笑了。 用起来有问题可以提issue,欢迎交流。