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

fix: 修复 label 类型声明问题 #597

Merged
merged 4 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion packages/components/src/components/form/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Meta } from 'rc-field-form/lib/interface';
import FieldContext from 'rc-field-form/lib/FieldContext';
import React, { useContext } from 'react';
import classNames from 'classnames';
import { FormItemProps } from './interface'
import { FormItemProps } from './interface';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import { FormContext } from './context';
import { hasValidName, toArray } from './util';
Expand All @@ -25,6 +25,7 @@ const Item: React.FC<FormItemProps> = (props: FormItemProps) => {
style,
name,
label,
title,
help,
feedback,
feedbackType,
Expand Down Expand Up @@ -65,6 +66,7 @@ const Item: React.FC<FormItemProps> = (props: FormItemProps) => {
<div className={cls} data-message-type={mergedFeedbackType} style={style}>
<ItemLabel
label={label}
title={title}
labelAlign={labelAlign}
prefixCls={prefixCls}
fieldId={fieldId}
Expand Down
9 changes: 7 additions & 2 deletions packages/components/src/components/form/ItemLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import classNames from 'classnames';
import isNumber from 'lodash/isNumber';
import isString from 'lodash/isString';

import { FormLabelAlign, RequiredMark } from './context';

export interface Props {
label?: string;
label?: React.ReactNode;
title?: string;
fieldId?: string;
prefixCls: string;
labelWidth?: number;
Expand All @@ -21,6 +24,7 @@ const ItemLabel: React.FC<Props> = (props: Props) => {
const {
prefixCls,
label,
title = label,
labelWidth,
fieldId,
afterLabel,
Expand All @@ -35,6 +39,7 @@ const ItemLabel: React.FC<Props> = (props: Props) => {
const isOptional = !required && requiredMark === 'optional';
const innerMarker = isOptional ? '(选填)' : '*';
const mergedRequiredMarker = marker !== undefined ? marker : innerMarker;
const mergedTitle = isNumber(title) || isString(title) ? (title as string) : '';
const cls = classNames(
`${prefixCls}-label`,
isRequired && `${prefixCls}-label-required`,
Expand All @@ -61,7 +66,7 @@ const ItemLabel: React.FC<Props> = (props: Props) => {
return (
<div className={cls} style={{ width: labelWidth }}>
{label && (
<label title={label} htmlFor={htmlFor}>
<label title={mergedTitle} htmlFor={htmlFor}>
{labelChild}
</label>
)}
Expand Down
32 changes: 26 additions & 6 deletions packages/components/src/components/form/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface Props<Values = unknown> extends Omit<RcFormProps<Values>, 'form
*/
className?: string;
/**
表单名称
表单名称
*/
name?: string;
/**
Expand Down Expand Up @@ -52,13 +52,13 @@ export interface Props<Values = unknown> extends Omit<RcFormProps<Values>, 'form
/**
自定义样式
*/
style ?: React.CSSProperties;
style?: React.CSSProperties;
}

type RenderChildren = (form: FormInstance) => React.ReactNode;
type ChildrenType = RenderChildren | React.ReactNode;

export interface FormItemProps extends Omit<FieldProps, 'children'> {
export interface FormItemProps extends Omit<FieldProps, 'children'>, Omit<ItemLabelProps, 'colon'> {
/**
自定义 `css` 类前缀
*/
Expand All @@ -74,7 +74,12 @@ export interface FormItemProps extends Omit<FieldProps, 'children'> {
/**
`label` 标签的文本
*/
label?: string;
label?: React.ReactNode;

/**
* `label` 元素的 title 属性,不设置则尝试使用 string 类型的 label 属性
*/
title?: string;
afterLabel?: React.ReactNode;
afterInput?: React.ReactNode;
/**
Expand Down Expand Up @@ -123,7 +128,7 @@ export interface FormItemProps extends Omit<FieldProps, 'children'> {
*/
labelAlign?: FormLabelAlign;
fieldKey?: React.Key | React.Key[];
}
}

export interface FormContextProps {
name?: string;
Expand All @@ -133,4 +138,19 @@ export interface FormContextProps {
inputWidth?: number;
requiredMark?: RequiredMark;
colon?: boolean;
}
}

export interface ItemLabelProps {
label?: React.ReactNode;
title?: string;
fieldId?: string;
prefixCls?: string;
labelWidth?: number;
afterLabel?: React.ReactNode;
required?: boolean;
requiredMark?: RequiredMark;
marker?: React.ReactNode;
colon?: string;
htmlFor?: string;
labelAlign?: FormLabelAlign;
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ group:
| 参数 | 说明 | 类型 | 默认值 |
| ----------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | -------- |
| label | label 标签的文本 | string \| ReactNode | - |
| title | label 标签的 title 属性 | string | - |
| name | 字段名,支持数组 | [NamePath](#namepath) | - |
| valuePropName | 子节点的值的属性,如 Switch 的是 'checked'。该属性为 getValueProps 的封装,自定义 `getValueProps` 后会失效 | string | - |
| getValueFromEvent | 设置如何将 event 的值转换成字段值 | `(..args: any[]) => any` | - |
Expand Down