Skip to content

Commit

Permalink
fix: fix type declaration (ant-design#30776)
Browse files Browse the repository at this point in the history
  • Loading branch information
qqabcv520 authored Jun 3, 2021
1 parent 0bc05bf commit 47dfa1f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface FormProps<Values = any> extends Omit<RcFormProps<Values>, 'form
hideRequiredMark?: boolean;
}

const InternalForm: React.ForwardRefRenderFunction<unknown, FormProps> = (props, ref) => {
const InternalForm: React.ForwardRefRenderFunction<FormInstance, FormProps> = (props, ref) => {
const contextSize = React.useContext(SizeContext);
const { getPrefixCls, direction, form: contextForm } = React.useContext(ConfigContext);

Expand Down
4 changes: 2 additions & 2 deletions components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ function FormItem<Values = any>(props: FormItemProps<Values>): React.ReactElemen
// Collect noStyle Field error to the top FormItem
const updateChildItemErrors = noStyle
? updateItemErrors
: (subName: string, subErrors: string[], originSubName: string) => {
: (subName: string, subErrors: string[], originSubName?: string) => {
setInlineErrors((prevInlineErrors = {}) => {
// Clean up origin error when name changed
if (originSubName !== subName) {
if (originSubName && originSubName !== subName) {
delete prevInlineErrors[originSubName];
}

Expand Down
6 changes: 3 additions & 3 deletions components/form/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function useForm<Values = any>(form?: FormInstance<Values>): [For

const wrapForm: FormInstance<Values> = React.useMemo(
() =>
form || {
form ?? {
...rcForm,
__INTERNAL__: {
itemRef: (name: InternalNamePath) => (node: React.ReactElement) => {
Expand All @@ -39,7 +39,7 @@ export default function useForm<Values = any>(form?: FormInstance<Values>): [For
}
},
},
scrollToField: (name: string, options: ScrollOptions = {}) => {
scrollToField: (name: NamePath, options: ScrollOptions = {}) => {
const namePath = toArray(name);
const fieldId = getFieldId(namePath, wrapForm.__INTERNAL__.name);
const node: HTMLElement | null = fieldId ? document.getElementById(fieldId) : null;
Expand All @@ -52,7 +52,7 @@ export default function useForm<Values = any>(form?: FormInstance<Values>): [For
});
}
},
getFieldInstance: (name: string) => {
getFieldInstance: (name: NamePath) => {
const namePathStr = toNamePathStr(name);
return itemsRef.current[namePathStr];
},
Expand Down
18 changes: 9 additions & 9 deletions components/input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ export function fixControlledValue<T>(value: T) {
return value;
}

export function resolveOnChange(
target: HTMLInputElement | HTMLTextAreaElement,
export function resolveOnChange<E extends HTMLInputElement | HTMLTextAreaElement>(
target: E,
e:
| React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
| React.ChangeEvent<E>
| React.MouseEvent<HTMLElement, MouseEvent>
| React.CompositionEvent<HTMLElement>,
onChange:
| undefined
| ((event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void),
| ((event: React.ChangeEvent<E>) => void),
targetValue?: string,
) {
if (!onChange) {
Expand All @@ -85,7 +85,7 @@ export function resolveOnChange(
event.currentTarget = target;
// change target ref value cause e.target.value should be '' when clear input
target.value = '';
onChange(event as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>);
onChange(event as React.ChangeEvent<E>);
// reset target ref value
target.value = originalInputValue;
return;
Expand All @@ -98,10 +98,10 @@ export function resolveOnChange(
event.currentTarget = target;

target.value = targetValue;
onChange(event as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>);
onChange(event as React.ChangeEvent<E>);
return;
}
onChange(event as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>);
onChange(event as React.ChangeEvent<E>);
}

export function getInputClassName(
Expand Down Expand Up @@ -168,9 +168,9 @@ class Input extends React.Component<InputProps, InputState> {
type: 'text',
};

input: HTMLInputElement;
input!: HTMLInputElement;

clearableInput: ClearableLabeledInput;
clearableInput!: ClearableLabeledInput;

removePasswordTimeout: any;

Expand Down
25 changes: 9 additions & 16 deletions components/locale-provider/LocaleReceiver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,28 @@ import defaultLocaleData from './default';
import LocaleContext from './context';
import { Locale } from '.';

export interface LocaleReceiverProps {
componentName?: string;
defaultLocale?: object | Function;
children: (locale: object, localeCode?: string, fullLocale?: object) => React.ReactNode;
export interface LocaleReceiverProps<C extends keyof Locale = keyof Locale> {
componentName: C;
defaultLocale?: Locale[C] | (() => Locale[C]);
children: (locale: Exclude<Locale[C], undefined>, localeCode?: string, fullLocale?: object) => React.ReactNode;
}

interface LocaleInterface {
[key: string]: any;
}

export interface LocaleReceiverContext {
antLocale?: LocaleInterface;
}

export default class LocaleReceiver extends React.Component<LocaleReceiverProps> {
export default class LocaleReceiver<C extends keyof Locale = keyof Locale> extends React.Component<LocaleReceiverProps<C>> {
static defaultProps = {
componentName: 'global',
};

static contextType = LocaleContext;

getLocale() {
getLocale(): Exclude<Locale[C], undefined> {
const { componentName, defaultLocale } = this.props;
const locale: object | Function =
defaultLocale || (defaultLocaleData as LocaleInterface)[componentName || 'global'];
const locale =
defaultLocale || defaultLocaleData[componentName ?? 'global'];
const antLocale = this.context;
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
return {
...(typeof locale === 'function' ? locale() : locale),
...(locale instanceof Function ? locale() : locale),
...(localeFromContext || {}),
};
}
Expand Down
14 changes: 7 additions & 7 deletions components/locale-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ export interface Locale {
locale: string;
Pagination?: PaginationLocale;
DatePicker?: DatePickerLocale;
TimePicker?: Object;
Calendar?: Object;
TimePicker?: Record<string, any>;
Calendar?: Record<string, any>;
Table?: TableLocale;
Modal?: ModalLocale;
Popconfirm?: PopconfirmLocale;
Transfer?: Partial<TransferLocale>;
Select?: Object;
Select?: Record<string, any>;
Upload?: UploadLocale;
Empty?: TransferLocaleForEmpty;
global?: Object;
PageHeader?: Object;
Icon?: Object;
Text?: Object;
global?: Record<string, any>;
PageHeader?: { back: string };
Icon?: Record<string, any>;
Text?: Record<string, any>;
Form?: {
optional?: string;
defaultValidateMessages: ValidateMessages;
Expand Down
6 changes: 3 additions & 3 deletions components/page-header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export interface PageHeaderProps {
footer?: React.ReactNode;
extra?: React.ReactNode;
avatar?: AvatarProps;
onBack?: (e: React.MouseEvent<HTMLDivElement>) => void;
onBack?: (e?: React.MouseEvent<HTMLDivElement>) => void;
className?: string;
ghost?: boolean;
}

const renderBack = (
prefixCls: string,
backIcon?: React.ReactNode,
onBack?: (e: React.MouseEvent<HTMLElement>) => void,
onBack?: (e?: React.MouseEvent<HTMLDivElement>) => void,
) => {
if (!backIcon || !onBack) {
return null;
Expand All @@ -40,7 +40,7 @@ const renderBack = (
{({ back }: { back: string }) => (
<div className={`${prefixCls}-back`}>
<TransButton
onClick={(e: React.MouseEvent<HTMLDivElement>) => {
onClick={(e?: React.MouseEvent<HTMLDivElement>) => {
onBack?.(e);
}}
className={`${prefixCls}-back-button`}
Expand Down

0 comments on commit 47dfa1f

Please sign in to comment.