fix: should auto focus in drop-down menu (#2234)
* fix: should auto focus in drop-down menu * fix: fix error in console
This commit is contained in:
parent
05058c04b6
commit
57507b310e
@ -130,7 +130,7 @@ SchemaInitializer.Button = observer(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (item.type === 'itemGroup') {
|
if (item.type === 'itemGroup') {
|
||||||
const label = compile(item.title);
|
const label = isString(item.title) ? compile(item.title) : item.title;
|
||||||
return (
|
return (
|
||||||
!!item.children?.length && {
|
!!item.children?.length && {
|
||||||
type: 'group',
|
type: 'group',
|
||||||
@ -203,7 +203,7 @@ SchemaInitializer.Item = function Item(props: SchemaInitializerItemProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (items?.length > 0) {
|
if (items?.length > 0) {
|
||||||
const renderMenuItem = (items: SchemaInitializerItemOptions[]) => {
|
const renderMenuItem = (items: SchemaInitializerItemOptions[], parentKey: string) => {
|
||||||
if (!items?.length) {
|
if (!items?.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -212,28 +212,30 @@ SchemaInitializer.Item = function Item(props: SchemaInitializerItemProps) {
|
|||||||
return { type: 'divider', key: `divider-${indexA}` };
|
return { type: 'divider', key: `divider-${indexA}` };
|
||||||
}
|
}
|
||||||
if (item.type === 'itemGroup') {
|
if (item.type === 'itemGroup') {
|
||||||
const label = compile(item.title);
|
const label = isString(item.title) ? compile(item.title) : item.title;
|
||||||
|
const key = `${parentKey}-item-group-${indexA}`;
|
||||||
return {
|
return {
|
||||||
type: 'group',
|
type: 'group',
|
||||||
key: item.key || `item-group-${indexA}`,
|
key,
|
||||||
label,
|
label,
|
||||||
title: label,
|
title: label,
|
||||||
className: styles.nbMenuItemGroup,
|
className: styles.nbMenuItemGroup,
|
||||||
children: renderMenuItem(item.children),
|
children: renderMenuItem(item.children, key),
|
||||||
} as MenuProps['items'][0];
|
} as MenuProps['items'][0];
|
||||||
}
|
}
|
||||||
if (item.type === 'subMenu') {
|
if (item.type === 'subMenu') {
|
||||||
const label = compile(item.title);
|
const label = compile(item.title);
|
||||||
|
const key = `${parentKey}-sub-menu-${indexA}`;
|
||||||
return {
|
return {
|
||||||
key: item.key || `sub-menu-${indexA}`,
|
key,
|
||||||
label,
|
label,
|
||||||
title: label,
|
title: label,
|
||||||
children: renderMenuItem(item.children),
|
children: renderMenuItem(item.children, key),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const label = compile(item.title);
|
const label = compile(item.title);
|
||||||
return {
|
return {
|
||||||
key: item.key || `${info.key}-${item.title}-${indexA}`,
|
key: `${parentKey}-${item.title}-${indexA}`,
|
||||||
label,
|
label,
|
||||||
title: label,
|
title: label,
|
||||||
onClick: (info) => {
|
onClick: (info) => {
|
||||||
@ -252,7 +254,7 @@ SchemaInitializer.Item = function Item(props: SchemaInitializerItemProps) {
|
|||||||
key: info.key,
|
key: info.key,
|
||||||
label: isString(children) ? compile(children) : children,
|
label: isString(children) ? compile(children) : children,
|
||||||
icon: typeof icon === 'string' ? <Icon type={icon as string} /> : icon,
|
icon: typeof icon === 'string' ? <Icon type={icon as string} /> : icon,
|
||||||
children: renderMenuItem(items),
|
children: renderMenuItem(items, info.key),
|
||||||
};
|
};
|
||||||
|
|
||||||
collectMenuItem(item);
|
collectMenuItem(item);
|
||||||
|
@ -5,16 +5,35 @@ import { useTranslation } from 'react-i18next';
|
|||||||
export const SelectCollection = ({ value: outValue, onChange }) => {
|
export const SelectCollection = ({ value: outValue, onChange }) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [value, setValue] = useState<string>(outValue);
|
const [value, setValue] = useState<string>(outValue);
|
||||||
|
const inputRef = React.useRef(null);
|
||||||
|
|
||||||
// 之所以要增加个内部的 value 是为了防止用户输入过快时造成卡顿的问题
|
// 之所以要增加个内部的 value 是为了防止用户输入过快时造成卡顿的问题
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setValue(outValue);
|
setValue(outValue);
|
||||||
}, [outValue]);
|
}, [outValue]);
|
||||||
|
|
||||||
|
// TODO: antd 的 Input 的 autoFocus 有 BUG,会不生效,等待官方修复后再简化:https://github.com/ant-design/ant-design/issues/41239
|
||||||
|
useEffect(() => {
|
||||||
|
// 1. 组件在第一次渲染时自动 focus,提高用户体验
|
||||||
|
inputRef.current.input.focus();
|
||||||
|
|
||||||
|
// 2. 当组件已经渲染,并再次显示时,自动 focus
|
||||||
|
const observer = new IntersectionObserver((entries) => {
|
||||||
|
if (entries[0].isIntersecting) {
|
||||||
|
inputRef.current.input.focus();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe(inputRef.current.input);
|
||||||
|
return () => {
|
||||||
|
observer.disconnect();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ width: 210 }}>
|
<div style={{ width: 210 }}>
|
||||||
<Input
|
<Input
|
||||||
autoFocus
|
ref={inputRef}
|
||||||
allowClear
|
allowClear
|
||||||
style={{ padding: '0 4px 6px' }}
|
style={{ padding: '0 4px 6px' }}
|
||||||
bordered={false}
|
bordered={false}
|
||||||
|
@ -9,7 +9,6 @@ import { createMapBlockSchema } from './utils';
|
|||||||
export const MapBlockInitializer = (props) => {
|
export const MapBlockInitializer = (props) => {
|
||||||
const { insert } = props;
|
const { insert } = props;
|
||||||
const options = useContext(SchemaOptionsContext);
|
const options = useContext(SchemaOptionsContext);
|
||||||
console.log('🚀 ~ file: MapBlockInitializer.tsx:12 ~ MapBlockInitializer ~ options:', options);
|
|
||||||
const { getCollectionFieldsOptions } = useCollectionManager();
|
const { getCollectionFieldsOptions } = useCollectionManager();
|
||||||
const { t } = useMapTranslation();
|
const { t } = useMapTranslation();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user