被雨水过滤的空气-Rain ff16f59908
perf: improve the UX of SchemaInitializer (#2666)
* 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
2023-09-26 13:47:20 +08:00

51 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>
);
};