-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 20220805_fix_table
- Loading branch information
Showing
22 changed files
with
1,221 additions
and
1,320 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
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
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,127 @@ | ||
import { | ||
ref, Ref, SetupContext, computed, onBeforeUpdate, | ||
} from '@vue/composition-api'; | ||
import { VNode } from 'vue'; | ||
import get from 'lodash/get'; | ||
import isArray from 'lodash/isArray'; | ||
import { | ||
TdSelectProps, SelectKeysType, TdOptionProps, SelectOptionGroup, SelectValue, | ||
} from '../type'; | ||
|
||
type UniOption = (TdOptionProps | SelectOptionGroup) & { | ||
index?: number; | ||
slots?: () => VNode; | ||
}; | ||
|
||
export default function useSelectOptions(props: TdSelectProps, context: SetupContext, keys: Ref<SelectKeysType>) { | ||
const options = ref<UniOption[]>([]); | ||
|
||
const getOptions = () => { | ||
let dynamicIndex = 0; | ||
|
||
// 解析 props 中 options 字段的配置,以此初始化 innerOptions | ||
const innerOptions: UniOption[] = props.options.map((option) => { | ||
const getFormatOption = (option: TdOptionProps) => { | ||
const { value, label } = keys.value; | ||
const res = { | ||
...option, | ||
index: dynamicIndex, | ||
label: get(option, label), | ||
value: get(option, value), | ||
}; | ||
dynamicIndex += 1; | ||
return res; | ||
}; | ||
if ((option as SelectOptionGroup).group && (option as SelectOptionGroup).children) { | ||
return { | ||
...option, | ||
children: (option as SelectOptionGroup).children.map((child) => getFormatOption(child)), | ||
}; | ||
} | ||
return getFormatOption(option); | ||
}); | ||
|
||
// 处理 slots 中 t-option 与 t-option-group | ||
const currentSlots = context.parent.$slots.default || []; | ||
const optionsSlots = currentSlots.filter((item) => item.tag?.endsWith('TOption')); | ||
const groupSlots = currentSlots.filter((item) => item.tag?.endsWith('TOptionGroup')); | ||
if (isArray(groupSlots)) { | ||
groupSlots.forEach((group) => { | ||
const groupOption = { | ||
group: (group.componentOptions.propsData as TdOptionProps)?.label, | ||
...group.componentOptions.propsData, | ||
children: [] as TdOptionProps[], | ||
}; | ||
|
||
const res = group.componentOptions.children; | ||
if (isArray(res)) { | ||
res.forEach((child) => { | ||
groupOption.children.push({ | ||
// 单独处理 style 和 class 参数的透传 | ||
class: child.data.staticClass, | ||
style: child.data.staticStyle, | ||
// 透传其他常规参数 | ||
...child.componentOptions.propsData, | ||
slots: () => child.componentOptions.children, | ||
index: dynamicIndex, | ||
} as TdOptionProps); | ||
dynamicIndex += 1; | ||
}); | ||
} | ||
|
||
innerOptions.push(groupOption); | ||
}); | ||
} | ||
|
||
if (isArray(optionsSlots)) { | ||
optionsSlots.forEach((child) => { | ||
innerOptions.push({ | ||
// 单独处理 style 和 class 参数的透传 | ||
class: child.data.staticClass, | ||
style: child.data.staticStyle, | ||
// 透传其他常规参数 | ||
...child.componentOptions.propsData, | ||
slots: () => child.componentOptions.children, | ||
index: dynamicIndex, | ||
} as TdOptionProps); | ||
dynamicIndex += 1; | ||
}); | ||
} | ||
|
||
options.value = innerOptions; | ||
}; | ||
|
||
const optionsMap = computed(() => { | ||
const res = new Map<SelectValue, TdOptionProps>(); | ||
optionsList.value.forEach((option: TdOptionProps) => { | ||
res.set(option.value, option); | ||
}); | ||
return res; | ||
}); | ||
|
||
const optionsList = computed(() => { | ||
const res: TdOptionProps[] = []; | ||
const getOptionsList = (options: TdOptionProps[]) => { | ||
options.forEach((option) => { | ||
if ((option as SelectOptionGroup).group) { | ||
getOptionsList((option as SelectOptionGroup).children); | ||
} else { | ||
res.push(option); | ||
} | ||
}); | ||
}; | ||
getOptionsList(options.value); | ||
return res; | ||
}); | ||
|
||
getOptions(); | ||
onBeforeUpdate(() => { | ||
getOptions(); | ||
}); | ||
|
||
return { | ||
options, | ||
optionsMap, | ||
optionsList, | ||
}; | ||
} |
Oops, something went wrong.