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

chore: form's code style optimization #6279

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/components/form/demos/demo-subscribe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ export default function () {
</Form.Item>

<Form.Subscribe to={['loginMethod']}>
{({ loginMethod }) => {
return (
<>
{loginMethod === 'mobile' && (
<Form.Item name='account' label='手机号'>
<Input placeholder='请输入手机号' />
</Form.Item>
)}
{loginMethod === 'email' && (
<Form.Item name='account' label='邮箱'>
<Input placeholder='请输入邮箱' />
</Form.Item>
)}
</>
)
}}
{({ loginMethod }) => (
<>
{loginMethod === 'mobile' && (
<Form.Item name='account' label='手机号'>
<Input placeholder='请输入手机号' />
</Form.Item>
)}
{loginMethod === 'email' && (
<Form.Item name='account' label='邮箱'>
<Input placeholder='请输入邮箱' />
</Form.Item>
)}
</>
)}
</Form.Subscribe>
</Form>
)
Expand Down
8 changes: 2 additions & 6 deletions src/components/form/demos/demo2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@ export default () => {
</Form.Item>
<Form.Item
label='表单联动-字段B'
shouldUpdate={(prevValues, curValues) => {
return prevValues.b !== curValues.b
}}
shouldUpdate={(prevValues, curValues) => prevValues.b !== curValues.b}
>
{({ getFieldValue }) => {
return JSON.stringify(getFieldValue('b'))
}}
{({ getFieldValue }) => JSON.stringify(getFieldValue('b'))}
</Form.Item>
<DatePickerInputItem />
</Form>
Expand Down
2 changes: 1 addition & 1 deletion src/components/form/demos/demo4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PickerValue } from 'antd-mobile/es/components/picker'
import { DownOutline } from 'antd-mobile-icons'

interface MobileValue {
preValue: string
preValue: string | number
realValue: string
}

Expand Down
61 changes: 30 additions & 31 deletions src/components/form/form-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import React, { FC, useContext, useCallback, useState, useRef } from 'react'
import classNames from 'classnames'
import { NativeProps, withNativeProps } from '../../utils/native-props'
import { Field, FormInstance } from 'rc-field-form'
import type { FieldProps } from 'rc-field-form/lib/Field'
import FieldContext from 'rc-field-form/lib/FieldContext'
import type { FieldProps } from 'rc-field-form/lib/Field'
import type { Meta, InternalNamePath } from 'rc-field-form/lib/interface'
import type { FormLayout } from './index'
import { devWarning } from '../../utils/dev-log'
import { FormContext, NoStyleItemContext } from './context'
import { toArray, isSafeSetRefComponent } from './utils'
import List, { ListItemProps } from '../list'
import type { FormLayout } from './index'
import Popover from '../popover'
import { QuestionCircleOutline } from 'antd-mobile-icons'
import { useConfig } from '../config-provider'
Expand Down Expand Up @@ -153,7 +153,7 @@ const FormItemLayout: React.FC<FormItemLayoutProps> = props => {
}
})()

const labelElement = label ? (
const labelElement = !!label && (
<label className={`${classPrefix}-label`} htmlFor={htmlFor}>
{label}
{requiredMark}
Expand All @@ -170,34 +170,33 @@ const FormItemLayout: React.FC<FormItemLayoutProps> = props => {
</Popover>
)}
</label>
) : null

const description =
props.description || hasFeedback ? (
<>
{props.description}
{hasFeedback && (
<>
{props.errors.map((error, index) => (
<div
key={`error-${index}`}
className={`${classPrefix}-feedback-error`}
>
{error}
</div>
))}
{props.warnings.map((warning, index) => (
<div
key={`warning-${index}`}
className={`${classPrefix}-feedback-warning`}
>
{warning}
</div>
))}
</>
)}
</>
) : null
)

const description = (!!props.description || hasFeedback) && (
<>
{props.description}
{hasFeedback && (
<>
{props.errors.map((error, index) => (
<div
key={`error-${index}`}
className={`${classPrefix}-feedback-error`}
>
{error}
</div>
))}
{props.warnings.map((warning, index) => (
<div
key={`warning-${index}`}
className={`${classPrefix}-feedback-warning`}
>
{warning}
</div>
))}
</>
)}
</>
)

return withNativeProps(
props,
Expand Down
Loading