style: Code style optimization (#40083)

* style: Code style optimization

* add

* rename

* rename

* revert

* revert

* revert

* add
This commit is contained in:
lijianan 2023-01-08 15:36:50 +08:00 committed by GitHub
parent 4b4e7cb6f2
commit 130c22ab03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 40 deletions

View File

@ -12,16 +12,12 @@ const Editor: React.FC<JSONEditorPropsOptional> = (props) => {
props: { mode: Mode.text }, props: { mode: Mode.text },
}); });
return () => { return () => {
if (editorRef.current) { editorRef.current?.destroy();
editorRef.current.destroy();
}
}; };
}, []); }, []);
useEffect(() => { useEffect(() => {
if (editorRef.current) { editorRef.current?.updateProps(props);
editorRef.current.updateProps(props);
}
}, [props]); }, [props]);
return <div ref={container} className="vanilla-jsoneditor-react" />; return <div ref={container} className="vanilla-jsoneditor-react" />;

View File

@ -16,24 +16,36 @@ export interface ActionButtonProps {
children?: React.ReactNode; children?: React.ReactNode;
} }
function isThenable(thing?: PromiseLike<any>): boolean { function isThenable<T extends any>(thing?: PromiseLike<T>): boolean {
return !!(thing && !!thing.then); return !!(thing && thing.then);
} }
const ActionButton: React.FC<ActionButtonProps> = (props) => { const ActionButton: React.FC<ActionButtonProps> = (props) => {
const {
type,
children,
prefixCls,
buttonProps,
close,
autoFocus,
emitEvent,
quitOnNullishReturnValue,
actionFn,
} = props;
const clickedRef = React.useRef<boolean>(false); const clickedRef = React.useRef<boolean>(false);
const ref = React.useRef<HTMLInputElement>(null); const buttonRef = React.useRef<HTMLButtonElement | HTMLAnchorElement>(null);
const [loading, setLoading] = useState<ButtonProps['loading']>(false); const [loading, setLoading] = useState<ButtonProps['loading']>(false);
const { close } = props;
const onInternalClose = (...args: any[]) => { const onInternalClose = (...args: any[]) => {
close?.(...args); close?.(...args);
}; };
React.useEffect(() => { React.useEffect(() => {
let timeoutId: NodeJS.Timer | null = null; let timeoutId: NodeJS.Timer | null = null;
if (props.autoFocus) { if (autoFocus) {
timeoutId = setTimeout(() => { timeoutId = setTimeout(() => {
ref.current?.focus(); buttonRef.current?.focus();
}); });
} }
return () => { return () => {
@ -64,7 +76,6 @@ const ActionButton: React.FC<ActionButtonProps> = (props) => {
}; };
const onClick = (e: React.MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => { const onClick = (e: React.MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => {
const { actionFn } = props;
if (clickedRef.current) { if (clickedRef.current) {
return; return;
} }
@ -74,9 +85,9 @@ const ActionButton: React.FC<ActionButtonProps> = (props) => {
return; return;
} }
let returnValueOfOnOk: PromiseLike<any>; let returnValueOfOnOk: PromiseLike<any>;
if (props.emitEvent) { if (emitEvent) {
returnValueOfOnOk = actionFn(e); returnValueOfOnOk = actionFn(e);
if (props.quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) { if (quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) {
clickedRef.current = false; clickedRef.current = false;
onInternalClose(e); onInternalClose(e);
return; return;
@ -95,7 +106,6 @@ const ActionButton: React.FC<ActionButtonProps> = (props) => {
handlePromiseOnOk(returnValueOfOnOk); handlePromiseOnOk(returnValueOfOnOk);
}; };
const { type, children, prefixCls, buttonProps } = props;
return ( return (
<Button <Button
{...convertLegacyProps(type)} {...convertLegacyProps(type)}
@ -103,7 +113,7 @@ const ActionButton: React.FC<ActionButtonProps> = (props) => {
loading={loading} loading={loading}
prefixCls={prefixCls} prefixCls={prefixCls}
{...buttonProps} {...buttonProps}
ref={ref} ref={buttonRef}
> >
{children} {children}
</Button> </Button>

View File

@ -1,5 +1,5 @@
import classNames from 'classnames'; import classNames from 'classnames';
import React, { useCallback, useContext, useEffect, useState } from 'react'; import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import type { ChangeEvent, CSSProperties } from 'react'; import type { ChangeEvent, CSSProperties } from 'react';
import type { ConfigConsumerProps, RenderEmptyHandler } from '../config-provider'; import type { ConfigConsumerProps, RenderEmptyHandler } from '../config-provider';
import { ConfigContext } from '../config-provider'; import { ConfigContext } from '../config-provider';
@ -153,11 +153,11 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
onSelectChange, onSelectChange,
} = props; } = props;
const [sourceSelectedKeys, setSourceSelectedKeys] = useState<string[]>( const [sourceSelectedKeys, setSourceSelectedKeys] = useState<string[]>(() =>
selectedKeys.filter((key) => !targetKeys.includes(key)), selectedKeys.filter((key) => !targetKeys.includes(key)),
); );
const [targetSelectedKeys, setTargetSelectedKeys] = useState<string[]>( const [targetSelectedKeys, setTargetSelectedKeys] = useState<string[]>(() =>
selectedKeys.filter((key) => targetKeys.includes(key)), selectedKeys.filter((key) => targetKeys.includes(key)),
); );
@ -306,34 +306,31 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
); );
}; };
const handleListStyle = ( const handleListStyle = (direction: TransferDirection): CSSProperties => {
listStyles: TransferProps<RecordType>['listStyle'], if (typeof listStyle === 'function') {
direction: TransferDirection, return listStyle({ direction });
): CSSProperties => {
if (typeof listStyles === 'function') {
return listStyles({ direction });
} }
return listStyles || {}; return listStyle || {};
}; };
const separateDataSource = () => { const [leftDataSource, rightDataSource] = useMemo(() => {
const leftDataSource: KeyWise<RecordType>[] = []; const leftData: KeyWise<RecordType>[] = [];
const rightDataSource: KeyWise<RecordType>[] = new Array(targetKeys.length); const rightData: KeyWise<RecordType>[] = new Array(targetKeys.length);
const targetKeysMap = groupKeysMap(targetKeys); const targetKeysMap = groupKeysMap(targetKeys);
dataSource.forEach((record: KeyWise<RecordType>) => { dataSource.forEach((record: KeyWise<RecordType>) => {
if (rowKey) { if (rowKey) {
record = { ...record, key: rowKey(record) }; record = { ...record, key: rowKey(record) };
} }
// rightDataSource should be ordered by targetKeys // rightData should be ordered by targetKeys
// leftDataSource should be ordered by dataSource // leftData should be ordered by dataSource
if (targetKeysMap.has(record.key)) { if (targetKeysMap.has(record.key)) {
rightDataSource[targetKeysMap.get(record.key)!] = record; rightData[targetKeysMap.get(record.key)!] = record;
} else { } else {
leftDataSource.push(record); leftData.push(record);
} }
}); });
return { leftDataSource, rightDataSource }; return [leftData, rightData] as const;
}; }, [dataSource, targetKeys, rowKey]);
const configContext = useContext<ConfigConsumerProps>(ConfigContext); const configContext = useContext<ConfigConsumerProps>(ConfigContext);
const formItemContext = useContext<FormItemStatusContextProps>(FormItemInputContext); const formItemContext = useContext<FormItemStatusContextProps>(FormItemInputContext);
@ -345,8 +342,6 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
const mergedStatus = getMergedStatus(status, customStatus); const mergedStatus = getMergedStatus(status, customStatus);
const mergedPagination = !children && pagination; const mergedPagination = !children && pagination;
const { leftDataSource, rightDataSource } = separateDataSource();
const leftActive = targetSelectedKeys.length > 0; const leftActive = targetSelectedKeys.length > 0;
const rightActive = sourceSelectedKeys.length > 0; const rightActive = sourceSelectedKeys.length > 0;
@ -373,7 +368,7 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
titleText={leftTitle} titleText={leftTitle}
dataSource={leftDataSource} dataSource={leftDataSource}
filterOption={filterOption} filterOption={filterOption}
style={handleListStyle(listStyle, 'left')} style={handleListStyle('left')}
checkedKeys={sourceSelectedKeys} checkedKeys={sourceSelectedKeys}
handleFilter={leftFilter} handleFilter={leftFilter}
handleClear={handleLeftClear} handleClear={handleLeftClear}
@ -409,7 +404,7 @@ const Transfer = <RecordType extends TransferItem = TransferItem>(
titleText={rightTitle} titleText={rightTitle}
dataSource={rightDataSource} dataSource={rightDataSource}
filterOption={filterOption} filterOption={filterOption}
style={handleListStyle(listStyle, 'right')} style={handleListStyle('right')}
checkedKeys={targetSelectedKeys} checkedKeys={targetSelectedKeys}
handleFilter={rightFilter} handleFilter={rightFilter}
handleClear={handleRightClear} handleClear={handleRightClear}