Skip to content
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

feat(type->api): resultField推断api的返回值应该包含recordbale类型 #3699

Merged
merged 2 commits into from
Mar 29, 2024

Conversation

electroluxcode
Copy link
Contributor

prop中存在resultField 这种支持xx.xx.xx类型。那么api的返回值应该包含 recordbale 类型

@likui628 likui628 self-requested a review March 28, 2024 01:54
@likui628
Copy link
Collaborator

能说下这么做的原因吗?

@electroluxcode
Copy link
Contributor Author

electroluxcode commented Mar 28, 2024

能说下这么做的原因吗?

举一个例子,例如我有如下代码

<template>
  <PageWrapper title="上传组件示例">
    <BasicForm @register="register" class="my-5" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  
  import { BasicForm, FormSchema, useForm } from '@/components/Form';
  import { PageWrapper } from '@/components/Page';
  const schemas: FormSchema[] = [
    {
      field: 'field2',
      component: "ApiSelect",
      label: '字段1',
      colProps: {
        span: 8,
      },
      componentProps: {
        resultField:"list.a",
        api: ()=>{
          return new Promise((resolve)=>{
            resolve({
              list:{
                a:[{roleName:"aa",roleValue:"aa"},{roleName:"bb",roleValue:"bb"}]
              }
            })
          })
        },
        labelField: 'roleName',
        valueField: 'roleValue',
      },
    },
  ];
  const [register] = useForm({
    labelWidth: 120,
    schemas,
  });
</script>

由于 resultfield的存在,所以这段代码是能够正常渲染的,但是目前却报错

对象字面量只能指定已知属性,并且“list”不在类型“OptionsItem[] | PromiseLike<OptionsItem[]>”中。

看了下源码,这部分主要是用到 lodash 的 get 从object中拿到object的嵌套数据。因此改成这种recordable 这种类型应该是符合要求

@likui628
Copy link
Collaborator

对象字面量只能指定已知属性,并且“list”不在类型“OptionsItem[] | PromiseLike<OptionsItem[]>”中。

看了下源码,这部分主要是用到 lodash 的 get 从object中拿到object的嵌套数据。因此改成这种recordable 这种类型应该是符合要求

其实只需要把自身的api结果重现map一下就可以了

componentProps: {
      resultField: 'list.a',
      api: () => {
        return new Promise((resolve) => {
          resolve([
            { roleName: 'aa', roleValue: 'aa' },
            { roleName: 'bb', roleValue: 'bb' },
          ]);
        });
      },
      labelField: 'roleName',
      valueField: 'roleValue',
    },

@electroluxcode
Copy link
Contributor Author

对象字面量只能指定已知属性,并且“list”不在类型“OptionsItem[] | PromiseLike<OptionsItem[]>”中。

看了下源码,这部分主要是用到 lodash 的 get 从object中拿到object的嵌套数据。因此改成这种recordable 这种类型应该是符合要求

其实只需要把自身的api结果重现map一下就可以了

componentProps: {
      resultField: 'list.a',
      api: () => {
        return new Promise((resolve) => {
          resolve([
            { roleName: 'aa', roleValue: 'aa' },
            { roleName: 'bb', roleValue: 'bb' },
          ]);
        });
      },
      labelField: 'roleName',
      valueField: 'roleValue',
    },

我理解你的意思,但是您的这种写法相当于否定了resultField 的存在的意义。在你目前这种写法下面,只有把resultField删掉才能够正常回显

Copy link
Collaborator

@likui628 likui628 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

懂你的意思了,稍微简化一下吧

PropType<(arg?: any) => Promise<OptionsItem[] | Recordable>>

@electroluxcode
Copy link
Contributor Author

懂你的意思了,稍微简化一下吧

PropType<(arg?: any) => Promise<OptionsItem[] | Recordable>>

okok

@likui628 likui628 merged commit c7ab4a5 into vbenjs:main Mar 29, 2024
2 checks passed
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.

None yet

2 participants