fix: can't open field link in embed page (#6222)

This commit is contained in:
gchust 2025-02-16 16:03:44 +08:00 committed by GitHub
parent 0a8a609a52
commit 3ba8ef7db3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -42,50 +42,57 @@ const filterProperties = (s) => {
return s['x-component'] === 'Action.Container';
};
const FieldLink = (props: any) => {
const { WrappedComponent, ...rest } = props;
const fieldSchema = useFieldSchema();
const { openPopup } = usePopupUtils();
const needWaitForFieldSchemaUpdatedRef = useRef(false);
const insertPopup = useInsertSchema();
const fieldSchemaRef = useRef(fieldSchema);
fieldSchemaRef.current = fieldSchema;
const getCustomActionSchema = useCallback(() => {
return fieldSchemaRef.current;
}, []);
const handleClick = useCallback(() => {
if (!fieldSchema.properties) {
insertPopup(popupSchema);
needWaitForFieldSchemaUpdatedRef.current = true;
}
if (needWaitForFieldSchemaUpdatedRef.current) {
// When first inserting, the fieldSchema instance will be updated to a new instance.
// We need to wait for the instance update before opening the popup to prevent configuration loss.
setTimeout(() => {
openPopup({ customActionSchema: getCustomActionSchema() });
});
needWaitForFieldSchemaUpdatedRef.current = false;
// Only open the popup when the popup schema exists
} else if (fieldSchema.properties) {
openPopup();
}
}, [fieldSchema, insertPopup, openPopup, getCustomActionSchema]);
return (
<a onClick={handleClick}>
<WrappedComponent {...rest} />
</a>
);
};
// 高阶组件:用来包装每个组件并添加弹窗功能
function withPopupWrapper<T>(WrappedComponent: React.ComponentType<T>) {
return (props: T) => {
const [visible, setVisible] = useState(false);
const [formValueChanged, setFormValueChanged] = useState(false);
const insertPopup = useInsertSchema();
const collection = useCollection();
const ctx = useActionContext();
const field: any = useField();
const fieldSchema = useFieldSchema();
const { enableLink, openMode, openSize } = fieldSchema?.['x-component-props'] || {};
const { visibleWithURL, setVisibleWithURL } = usePopupUtils();
const { openPopup } = usePopupUtils();
const needWaitForFieldSchemaUpdatedRef = useRef(false);
const fieldSchemaRef = useRef(fieldSchema);
fieldSchemaRef.current = fieldSchema;
const getCustomActionSchema = useCallback(() => {
return fieldSchemaRef.current;
}, []);
const handleClick = useCallback(() => {
if (!fieldSchema.properties) {
insertPopup(popupSchema);
needWaitForFieldSchemaUpdatedRef.current = true;
}
if (needWaitForFieldSchemaUpdatedRef.current) {
// When first inserting, the fieldSchema instance will be updated to a new instance.
// We need to wait for the instance update before opening the popup to prevent configuration loss.
setTimeout(() => {
openPopup({
customActionSchema: getCustomActionSchema(),
});
});
needWaitForFieldSchemaUpdatedRef.current = false;
// Only open the popup when the popup schema exists
} else if (fieldSchema.properties) {
openPopup();
}
}, [fieldSchema, insertPopup, openPopup, getCustomActionSchema]);
const { setSubmitted } = ctx;
const handleVisibleChange = useCallback(
(value: boolean): void => {
setVisible?.(value);
@ -93,6 +100,11 @@ function withPopupWrapper<T>(WrappedComponent: React.ComponentType<T>) {
},
[setVisibleWithURL],
);
const fieldSchemaRef = useRef(fieldSchema);
fieldSchemaRef.current = fieldSchema;
const { setSubmitted } = ctx;
return enableLink ? (
<PopupVisibleProvider visible={false}>
<ActionContextProvider
@ -119,9 +131,7 @@ function withPopupWrapper<T>(WrappedComponent: React.ComponentType<T>) {
</VariablePopupRecordProvider>
</SchemaComponentOptions>
</CollectionProvider>
<a onClick={handleClick}>
<WrappedComponent {...props} />
</a>
<FieldLink {...props} WrappedComponent={WrappedComponent} />
</ActionContextProvider>
</PopupVisibleProvider>
) : (