style: update util

pull/6213/head
tangjinzhou 2 years ago
parent 94c2887c81
commit 0399ce0ec7

@ -5,6 +5,7 @@ import type { ButtonProps } from '../button';
import type { LegacyButtonType } from '../button/buttonTypes'; import type { LegacyButtonType } from '../button/buttonTypes';
import { convertLegacyProps } from '../button/buttonTypes'; import { convertLegacyProps } from '../button/buttonTypes';
import useDestroyed from './hooks/useDestroyed'; import useDestroyed from './hooks/useDestroyed';
import { objectType } from './type';
const actionButtonProps = { const actionButtonProps = {
type: { type: {
@ -14,15 +15,15 @@ const actionButtonProps = {
close: Function, close: Function,
autofocus: Boolean, autofocus: Boolean,
prefixCls: String, prefixCls: String,
buttonProps: Object as PropType<ButtonProps>, buttonProps: objectType<ButtonProps>(),
emitEvent: Boolean, emitEvent: Boolean,
quitOnNullishReturnValue: Boolean, quitOnNullishReturnValue: Boolean,
}; };
export type ActionButtonProps = ExtractPropTypes<typeof actionButtonProps>; export type ActionButtonProps = ExtractPropTypes<typeof actionButtonProps>;
function isThenable(thing?: PromiseLike<any>): boolean { function isThenable<T>(thing?: PromiseLike<T>): boolean {
return !!(thing && !!thing.then); return !!(thing && thing.then);
} }
export default defineComponent({ export default defineComponent({
@ -44,8 +45,11 @@ export default defineComponent({
clearTimeout(timeoutId); clearTimeout(timeoutId);
}); });
const onInternalClose = (...args: any[]) => {
props.close?.(...args);
};
const handlePromiseOnOk = (returnValueOfOnOk?: PromiseLike<any>) => { const handlePromiseOnOk = (returnValueOfOnOk?: PromiseLike<any>) => {
const { close } = props;
if (!isThenable(returnValueOfOnOk)) { if (!isThenable(returnValueOfOnOk)) {
return; return;
} }
@ -55,48 +59,46 @@ export default defineComponent({
if (!isDestroyed.value) { if (!isDestroyed.value) {
loading.value = false; loading.value = false;
} }
close(...args); onInternalClose(...args);
clickedRef.value = false; clickedRef.value = false;
}, },
(e: Error) => { (e: Error) => {
// Emit error when catch promise reject
// eslint-disable-next-line no-console
console.error(e);
// See: https://github.com/ant-design/ant-design/issues/6183 // See: https://github.com/ant-design/ant-design/issues/6183
if (!isDestroyed.value) { if (!isDestroyed.value) {
loading.value = false; loading.value = false;
} }
clickedRef.value = false; clickedRef.value = false;
return Promise.reject(e);
}, },
); );
}; };
const onClick = (e: MouseEvent) => { const onClick = (e: MouseEvent) => {
const { actionFn, close = () => {} } = props; const { actionFn } = props;
if (clickedRef.value) { if (clickedRef.value) {
return; return;
} }
clickedRef.value = true; clickedRef.value = true;
if (!actionFn) { if (!actionFn) {
close(); onInternalClose();
return; return;
} }
let returnValueOfOnOk; let returnValueOfOnOk: PromiseLike<any>;
if (props.emitEvent) { if (props.emitEvent) {
returnValueOfOnOk = actionFn(e); returnValueOfOnOk = actionFn(e);
if (props.quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) { if (props.quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) {
clickedRef.value = false; clickedRef.value = false;
close(e); onInternalClose(e);
return; return;
} }
} else if (actionFn.length) { } else if (actionFn.length) {
returnValueOfOnOk = actionFn(close); returnValueOfOnOk = actionFn(props.close);
// https://github.com/ant-design/ant-design/issues/23358 // https://github.com/ant-design/ant-design/issues/23358
clickedRef.value = false; clickedRef.value = false;
} else { } else {
returnValueOfOnOk = actionFn(); returnValueOfOnOk = actionFn();
if (!returnValueOfOnOk) { if (!returnValueOfOnOk) {
close(); onInternalClose();
return; return;
} }
} }

@ -1,4 +1,4 @@
export function isWindow(obj: any) { export function isWindow(obj: any): obj is Window {
return obj !== null && obj !== undefined && obj === obj.window; return obj !== null && obj !== undefined && obj === obj.window;
} }
@ -12,16 +12,22 @@ export default function getScroll(
const method = top ? 'scrollTop' : 'scrollLeft'; const method = top ? 'scrollTop' : 'scrollLeft';
let result = 0; let result = 0;
if (isWindow(target)) { if (isWindow(target)) {
result = (target as Window)[top ? 'pageYOffset' : 'pageXOffset']; result = target[top ? 'pageYOffset' : 'pageXOffset'];
} else if (target instanceof Document) { } else if (target instanceof Document) {
result = target.documentElement[method]; result = target.documentElement[method];
} else if (target instanceof HTMLElement) {
result = target[method];
} else if (target) { } else if (target) {
result = (target as HTMLElement)[method]; // According to the type inference, the `target` is `never` type.
// Since we configured the loose mode type checking, and supports mocking the target with such shape below::
// `{ documentElement: { scrollLeft: 200, scrollTop: 400 } }`,
// the program may falls into this branch.
// Check the corresponding tests for details. Don't sure what is the real scenario this happens.
result = target[method];
} }
if (target && !isWindow(target) && typeof result !== 'number') { if (target && !isWindow(target) && typeof result !== 'number') {
result = ((target as HTMLElement).ownerDocument || (target as Document)).documentElement?.[ result = ((target.ownerDocument ?? target) as any).documentElement?.[method];
method
];
} }
return result; return result;
} }

@ -1,6 +1,6 @@
import raf from './raf'; import raf from './raf';
import getScroll, { isWindow } from './getScroll';
import { easeInOutCubic } from './easings'; import { easeInOutCubic } from './easings';
import getScroll, { isWindow } from './getScroll';
interface ScrollToOptions { interface ScrollToOptions {
/** Scroll container, default as window */ /** Scroll container, default as window */
@ -23,8 +23,8 @@ export default function scrollTo(y: number, options: ScrollToOptions = {}) {
const nextScrollTop = easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration); const nextScrollTop = easeInOutCubic(time > duration ? duration : time, scrollTop, y, duration);
if (isWindow(container)) { if (isWindow(container)) {
(container as Window).scrollTo(window.pageXOffset, nextScrollTop); (container as Window).scrollTo(window.pageXOffset, nextScrollTop);
} else if (container instanceof HTMLDocument || container.constructor.name === 'HTMLDocument') { } else if (container instanceof Document || container.constructor.name === 'HTMLDocument') {
(container as HTMLDocument).documentElement.scrollTop = nextScrollTop; (container as Document).documentElement.scrollTop = nextScrollTop;
} else { } else {
(container as HTMLElement).scrollTop = nextScrollTop; (container as HTMLElement).scrollTop = nextScrollTop;
} }

@ -1,8 +1,8 @@
import type { ValidateStatus } from '../form/FormItem'; import type { ValidateStatus } from '../form/FormItem';
import classNames from './classNames'; import classNames from './classNames';
import { tuple } from './type';
const InputStatuses = tuple('warning', 'error', ''); const InputStatuses = ['warning', 'error', ''] as const;
export type InputStatus = typeof InputStatuses[number]; export type InputStatus = typeof InputStatuses[number];
export function getStatusClassNames( export function getStatusClassNames(

@ -1,6 +1,10 @@
import raf from './raf'; import raf from './raf';
export default function throttleByAnimationFrame<T extends unknown[]>(fn: (...args: T) => void) { type throttledFn = (...args: any[]) => void;
type throttledCancelFn = { cancel: () => void };
function throttleByAnimationFrame<T extends any[]>(fn: (...args: T) => void) {
let requestId: number | null; let requestId: number | null;
const later = (args: T) => () => { const later = (args: T) => () => {
@ -8,10 +12,7 @@ export default function throttleByAnimationFrame<T extends unknown[]>(fn: (...ar
fn(...args); fn(...args);
}; };
const throttled: { const throttled: throttledFn & throttledCancelFn = (...args: T) => {
(...args: T): void;
cancel: () => void;
} = (...args: T) => {
if (requestId == null) { if (requestId == null) {
requestId = raf(later(args)); requestId = raf(later(args));
} }
@ -25,29 +26,4 @@ export default function throttleByAnimationFrame<T extends unknown[]>(fn: (...ar
return throttled; return throttled;
} }
export function throttleByAnimationFrameDecorator() { export default throttleByAnimationFrame;
// eslint-disable-next-line func-names
return function (target: any, key: string, descriptor: any) {
const fn = descriptor.value;
let definingProperty = false;
return {
configurable: true,
get() {
// eslint-disable-next-line no-prototype-builtins
if (definingProperty || this === target.prototype || this.hasOwnProperty(key)) {
return fn;
}
const boundFn = throttleByAnimationFrame(fn.bind(this));
definingProperty = true;
Object.defineProperty(this, key, {
value: boundFn,
configurable: true,
writable: true,
});
definingProperty = false;
return boundFn;
},
};
};
}

@ -1,7 +0,0 @@
import warning, { resetWarned } from '../vc-util/warning';
export { resetWarned };
export default (valid, component, message = '') => {
warning(valid, `[antdv: ${component}] ${message}`);
};

@ -0,0 +1,21 @@
import vcWarning, { resetWarned } from '../vc-util/warning';
export { resetWarned };
export function noop() {}
type Warning = (valid: boolean, component: string, message?: string) => void;
// eslint-disable-next-line import/no-mutable-exports
let warning: Warning = noop;
if (process.env.NODE_ENV !== 'production') {
warning = (valid, component, message) => {
vcWarning(valid, `[antd: ${component}] ${message}`);
// StrictMode will inject console which will not throw warning in React 17.
if (process.env.NODE_ENV === 'test') {
resetWarned();
}
};
}
export default warning;
Loading…
Cancel
Save