-
Notifications
You must be signed in to change notification settings - Fork 693
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
roymondchen
committed
Jun 27, 2024
1 parent
77f13fa
commit 0ffc223
Showing
10 changed files
with
273 additions
and
78 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
packages/editor/src/fields/DataSourceFieldSelect/FieldSelect.vue
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,152 @@ | ||
<template> | ||
<div class="m-editor-data-source-field-select"> | ||
<TMagicSelect | ||
:model-value="selectDataSourceId" | ||
clearable | ||
filterable | ||
:size="size" | ||
:disabled="disabled" | ||
@change="dsChangeHandler" | ||
> | ||
<component | ||
v-for="option in dataSourcesOptions" | ||
class="tmagic-design-option" | ||
:key="option.value" | ||
:is="optionComponent?.component || 'el-option'" | ||
v-bind=" | ||
optionComponent?.props({ | ||
label: option.text, | ||
value: option.value, | ||
disabled: option.disabled, | ||
}) || { | ||
label: option.text, | ||
value: option.value, | ||
disabled: option.disabled, | ||
} | ||
" | ||
> | ||
</component> | ||
</TMagicSelect> | ||
<TMagicCascader | ||
:model-value="selectFieldsId" | ||
clearable | ||
filterable | ||
:size="size" | ||
:disabled="disabled" | ||
:options="fieldsOptions" | ||
:props="{ | ||
checkStrictly, | ||
}" | ||
@change="fieldChangeHandler" | ||
></TMagicCascader> | ||
<TMagicButton | ||
v-if="selectDataSourceId && hasDataSourceSidePanel" | ||
class="m-fields-select-action-button" | ||
:size="size" | ||
@click="editHandler(selectDataSourceId)" | ||
><MIcon :icon="!notEditable ? Edit : View"></MIcon | ||
></TMagicButton> | ||
</div> | ||
</template> | ||
<script lang="ts" setup> | ||
import { computed, inject, ref, watch } from 'vue'; | ||
import { Edit, View } from '@element-plus/icons-vue'; | ||
import { getConfig as getDesignConfig, TMagicButton, TMagicCascader, TMagicSelect } from '@tmagic/design'; | ||
import { type FilterFunction, filterFunction, type FormState, type SelectOption } from '@tmagic/form'; | ||
import { DataSourceFieldType } from '@tmagic/schema'; | ||
import { DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX } from '@tmagic/utils'; | ||
import MIcon from '@editor/components/Icon.vue'; | ||
import { type EventBus, type Services, SideItemKey } from '@editor/type'; | ||
import { getCascaderOptionsFromFields, removeDataSourceFieldPrefix } from '@editor/utils'; | ||
const props = defineProps<{ | ||
/** | ||
* 是否要编译成数据源的data。 | ||
* key: 不编译,就是要数据源id和field name; | ||
* value: 要编译(数据源data[`${filed}`]) | ||
* */ | ||
value?: 'key' | 'value'; | ||
disabled?: boolean; | ||
checkStrictly?: boolean; | ||
size?: 'large' | 'default' | 'small'; | ||
dataSourceFieldType?: DataSourceFieldType[]; | ||
/** 是否可以编辑数据源,disable表示的是是否可以选择数据源 */ | ||
notEditable?: boolean | FilterFunction; | ||
}>(); | ||
const emit = defineEmits<{ | ||
change: [v: string[]]; | ||
}>(); | ||
const modelValue = defineModel<string[] | any>('modelValue', { default: [] }); | ||
const optionComponent = getDesignConfig('components')?.option; | ||
const services = inject<Services>('services'); | ||
const mForm = inject<FormState | undefined>('mForm'); | ||
const eventBus = inject<EventBus>('eventBus'); | ||
const dataSources = computed(() => services?.dataSourceService.get('dataSources') || []); | ||
const valueIsKey = computed(() => props.value === 'key'); | ||
const notEditable = computed(() => filterFunction(mForm, props.notEditable, props)); | ||
const dataSourcesOptions = computed<SelectOption[]>(() => | ||
dataSources.value.map((ds) => ({ | ||
text: ds.title || ds.id, | ||
value: valueIsKey.value ? ds.id : `${DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX}${ds.id}`, | ||
})), | ||
); | ||
const selectDataSourceId = ref(''); | ||
const selectFieldsId = ref<string[]>([]); | ||
watch( | ||
modelValue, | ||
(value) => { | ||
if (Array.isArray(value)) { | ||
const [dsId, ...fields] = value; | ||
selectDataSourceId.value = dsId; | ||
selectFieldsId.value = fields; | ||
} else { | ||
selectDataSourceId.value = ''; | ||
selectFieldsId.value = []; | ||
} | ||
}, | ||
{ | ||
immediate: true, | ||
}, | ||
); | ||
const fieldsOptions = computed(() => { | ||
const ds = dataSources.value.find((ds) => ds.id === removeDataSourceFieldPrefix(selectDataSourceId.value)); | ||
if (!ds) return []; | ||
return getCascaderOptionsFromFields(ds.fields, props.dataSourceFieldType); | ||
}); | ||
const dsChangeHandler = (v: string) => { | ||
modelValue.value = [v]; | ||
emit('change', modelValue.value); | ||
}; | ||
const fieldChangeHandler = (v: string[]) => { | ||
modelValue.value = [selectDataSourceId.value, ...v]; | ||
emit('change', modelValue.value); | ||
}; | ||
const hasDataSourceSidePanel = computed(() => | ||
(services?.uiService.get('sideBarItems') || []).find((item) => item.$key === SideItemKey.DATA_SOURCE), | ||
); | ||
const editHandler = (id: string) => { | ||
eventBus?.emit('edit-data-source', removeDataSourceFieldPrefix(id)); | ||
}; | ||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.m-fields-data-source-field-select { | ||
width: 100%; | ||
.m-editor-data-source-field-select { | ||
display: flex; | ||
width: 100%; | ||
|
||
.tmagic-design-select { | ||
flex: 1; | ||
margin-right: 10px; | ||
} | ||
|
||
.tmagic-design-cascader { | ||
flex: 2; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.