mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 23:49:27 +08:00
* perf: improve the UX of SchemaInitializer * fix: fix error of Charts block * fix: fix fields * fix: fix search * chore: avoid crash * chore: fix build * chore: avoid crash * refactor: rename SelectCollection to SearchCollections * refactor: increased code versatility for improved reusability * fix: fix Add chart * perf: workflow * refactor: remove useless code * fix: fix block template * fix: should clean search value when creating a block
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { Divider, Input } from 'antd';
|
||
import React, { useEffect, useState } from 'react';
|
||
import { useTranslation } from 'react-i18next';
|
||
|
||
export const SearchCollections = ({ value: outValue, onChange }) => {
|
||
const { t } = useTranslation();
|
||
const [value, setValue] = useState<string>(outValue);
|
||
const inputRef = React.useRef(null);
|
||
|
||
// 之所以要增加个内部的 value 是为了防止用户输入过快时造成卡顿的问题
|
||
useEffect(() => {
|
||
setValue(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 (
|
||
<div style={{ width: 210 }}>
|
||
<Input
|
||
ref={inputRef}
|
||
allowClear
|
||
style={{ padding: '0 4px 6px' }}
|
||
bordered={false}
|
||
placeholder={t('Search and select collection')}
|
||
value={value}
|
||
onChange={(e) => {
|
||
onChange(e.target.value);
|
||
setValue(e.target.value);
|
||
}}
|
||
/>
|
||
<Divider style={{ margin: 0 }} />
|
||
</div>
|
||
);
|
||
};
|