ant-design / pro-components

🏆 Use Ant Design like a Pro!
https://pro-components.antdigital.dev
MIT License
4.3k stars 1.36k forks source link

feat: add form item help render type #8462

Closed yunho1017 closed 4 months ago

yunho1017 commented 5 months ago

related: https://github.com/ant-design/ant-design/discussions/48001

Motivation

I want to use help props as guide message for the input. (always visible) so i use help props, I cant't see the validate error when after validation. i want to use help and validate together.

So I added a render type that passes errors to params.

As-is

code

    <ProFormText
        label="name"
        name="name"
        help={"You can also input emojis and special character"}
        rules={[Validations.required()]}
    />

before validate

스크린샷 2024-03-26 오후 2 48 52

after validate

스크린샷 2024-03-26 오후 2 49 02

After validation, help is displayed instead of the required error message.

To-be

code

    <ProFormText
        label="name"
        name="name"
        help={({errors}) => errors.length ? errors[0] : "You can also input emojis and special character"}
        rules={[Validations.required()]}
    />

before validate

스크린샷 2024-03-26 오후 2 48 52

after validate

스크린샷 2024-03-26 오후 2 48 28

After validation, I want to show an error message.

API

help?:
    | React.ReactNode
    | ((params: { errors: React.ReactNode[]; warnings: React.ReactNode[] }) => React.ReactNode);

Basic Example

    <ProFormText
        label={"name"}
        name={"name"}
        help={({errors}) => errors.length ? errors[0] : "You can also input emojis and special character"}
        rules={[Validations.required()]}
    />
github-actions[bot] commented 5 months ago

⚡️ Deploying PR Preview...

chenshuai2144 commented 4 months ago

感觉功能不是很有必要? 你有什么使用场景吗?给我几个看看

yunho1017 commented 4 months ago

I think there are two scenarios for use.

  1. Show guide text before Validation and show error message after Validation. e.g.

    <ProFormText
        label={"name"}
        name={"name"}
        help={({errors}) => errors.length ? errors[0] : "You can also input emojis and special character"}
        rules={[Validations.required()]}
    />
  2. Render custom error list e.g.

   <ProFormText
        label={"name"}
        name={"name"}
        help={({errors}) => <CustomErrorListField errors={errors}/>}
        rules={[Validations.required()]}
    />