-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
162 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/li-editor/src/widgets/DatasetsPanel/AddDataset/AddDatasetModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { LayerSchema } from '@antv/li-sdk'; | ||
import { Modal, Segmented } from 'antd'; | ||
import React, { useState } from 'react'; | ||
import { useEditorContext } from '../../../hooks/internal'; | ||
import type { AddDataset as AddDatasetType } from '../../../types'; | ||
|
||
type AddDatasetModalProps = { | ||
title: string; | ||
visible: boolean; | ||
onClose: () => void; | ||
onSubmit: (datasets: AddDatasetType[], layers?: LayerSchema[]) => void; | ||
}; | ||
|
||
const AddDatasetModal = ({ title, visible, onClose, onSubmit }: AddDatasetModalProps) => { | ||
const { containerSlotMap } = useEditorContext(); | ||
const addWidgets = containerSlotMap.Datasets?.addDataset || []; | ||
const defaultSegmentedValue = addWidgets[0].metadata.name; | ||
|
||
const [segmentedValue, setSegmentedValue] = useState(defaultSegmentedValue); | ||
|
||
const segmentedOptions = addWidgets.map((widget) => ({ | ||
label: widget.metadata.displayName, | ||
value: widget.metadata.name, | ||
})); | ||
const AddWidget = addWidgets.find((widget) => widget.metadata.name === segmentedValue)!.component; | ||
|
||
return ( | ||
<Modal | ||
title={title} | ||
width="min-content" | ||
style={{ minWidth: 1000 }} | ||
destroyOnClose | ||
open={visible} | ||
footer={null} | ||
bodyStyle={{ paddingBottom: 0 }} | ||
onCancel={onClose} | ||
> | ||
{segmentedOptions.length > 1 && ( | ||
<Segmented | ||
options={segmentedOptions} | ||
onChange={(val) => { | ||
setSegmentedValue(val as string); | ||
}} | ||
defaultValue={segmentedValue} | ||
style={{ marginBottom: 20 }} | ||
/> | ||
)} | ||
<AddWidget onCancel={onClose} onSubmit={onSubmit} /> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default AddDatasetModal; |
22 changes: 22 additions & 0 deletions
22
packages/li-editor/src/widgets/DatasetsPanel/AddDataset/helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { DatasetSchema } from '@antv/li-sdk'; | ||
import { getDatasetColumns } from '@antv/li-sdk'; | ||
import type { AddDataset } from '../../../types'; | ||
import { validateDataset } from '../../../utils'; | ||
|
||
export const getAddDatasetSchema = (ddataset: AddDataset, id: string): DatasetSchema => { | ||
let datasetSchema: DatasetSchema; | ||
if (ddataset.type === 'local') { | ||
datasetSchema = { | ||
...ddataset, | ||
id, | ||
columns: ddataset.data?.length ? getDatasetColumns(ddataset.data[0]) : [], | ||
}; | ||
} else { | ||
datasetSchema = { | ||
...ddataset, | ||
id, | ||
}; | ||
} | ||
|
||
return validateDataset(datasetSchema); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/li-editor/src/widgets/DatasetsPanel/ReplaceDataset/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import { useEditorState } from '../../../hooks'; | ||
import type { AddDataset as AddDatasetType } from '../../../types'; | ||
import AddDatasetModal from '../AddDataset/AddDatasetModal'; | ||
import { getAddDatasetSchema } from '../AddDataset/helper'; | ||
|
||
type ReplaceDatasetProps = { | ||
datasetId: string; | ||
visible: boolean; | ||
onClose: () => void; | ||
}; | ||
|
||
const ReplaceDataset = ({ datasetId, visible, onClose }: ReplaceDatasetProps) => { | ||
const { updateState } = useEditorState(); | ||
|
||
// 替换 dataset | ||
const onReplaceDataset = (dataset: AddDatasetType) => { | ||
updateState((draft) => { | ||
const index = draft.datasets.findIndex((item) => item.id === datasetId); | ||
if (index !== -1) { | ||
const replaceDataset = getAddDatasetSchema(dataset, datasetId); | ||
draft.datasets[index] = replaceDataset; | ||
} | ||
}); | ||
}; | ||
|
||
const onSubmit = (datasets: AddDatasetType[]) => { | ||
const dataset = datasets[0]; | ||
if (dataset) { | ||
onReplaceDataset(dataset); | ||
} | ||
onClose(); | ||
}; | ||
|
||
return <AddDatasetModal title="替换数据集" visible={visible} onSubmit={onSubmit} onClose={onClose} />; | ||
}; | ||
|
||
export default ReplaceDataset; |