2023-03-16 12:12:14 +08:00
|
|
|
import { useField, useFieldSchema } from '@formily/react';
|
2023-03-29 17:11:20 +08:00
|
|
|
import { BlockProvider, FixedBlockWrapper, SchemaComponentOptions, useBlockRequestContext } from '@nocobase/client';
|
2023-04-17 10:12:49 +08:00
|
|
|
import React, { createContext, useContext, useState } from 'react';
|
2023-03-16 12:12:14 +08:00
|
|
|
|
|
|
|
export const MapBlockContext = createContext<any>({});
|
|
|
|
|
|
|
|
const InternalMapBlockProvider = (props) => {
|
|
|
|
const { fieldNames } = props;
|
|
|
|
const fieldSchema = useFieldSchema();
|
|
|
|
const field = useField();
|
|
|
|
const { resource, service } = useBlockRequestContext();
|
|
|
|
const [selectedRecordKeys, setSelectedRecordKeys] = useState([]);
|
|
|
|
|
|
|
|
return (
|
2023-03-29 17:11:20 +08:00
|
|
|
<FixedBlockWrapper>
|
|
|
|
<SchemaComponentOptions scope={{ selectedRecordKeys }}>
|
|
|
|
<MapBlockContext.Provider
|
|
|
|
value={{
|
|
|
|
field,
|
|
|
|
service,
|
|
|
|
resource,
|
|
|
|
fieldNames,
|
|
|
|
fixedBlock: fieldSchema?.['x-decorator-props']?.fixedBlock,
|
|
|
|
selectedRecordKeys,
|
|
|
|
setSelectedRecordKeys,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</MapBlockContext.Provider>
|
|
|
|
</SchemaComponentOptions>
|
|
|
|
</FixedBlockWrapper>
|
2023-03-16 12:12:14 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const MapBlockProvider = (props) => {
|
2023-08-10 17:36:02 +08:00
|
|
|
const { params, fieldNames } = props;
|
|
|
|
const appends = params.appends || [];
|
|
|
|
const { field } = fieldNames || {};
|
|
|
|
if (Array.isArray(field) && field.length > 1) {
|
|
|
|
appends.push(field[0]);
|
|
|
|
}
|
2023-03-16 12:12:14 +08:00
|
|
|
return (
|
2023-09-27 20:00:17 +08:00
|
|
|
<BlockProvider data-testid="map-block" {...props} params={{ ...params, appends, paginate: false }}>
|
2023-03-16 12:12:14 +08:00
|
|
|
<InternalMapBlockProvider {...props} />
|
|
|
|
</BlockProvider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useMapBlockContext = () => {
|
|
|
|
return useContext(MapBlockContext);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useMapBlockProps = () => {
|
|
|
|
const ctx = useMapBlockContext();
|
|
|
|
|
|
|
|
return {
|
|
|
|
...ctx,
|
|
|
|
dataSource: ctx?.service?.data?.data,
|
|
|
|
zoom: ctx?.field?.componentProps?.zoom || 13,
|
|
|
|
};
|
|
|
|
};
|