Skip to content

Commit

Permalink
fix(comp:select): select grouped options indent not working (#1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
sallerli1 committed Aug 11, 2023
1 parent 7337b23 commit 8807a31
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/components/select/demo/Group.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<IxSpace>
<IxSelect v-model:value="value" :dataSource="dataSource"> </IxSelect>
<IxSelect v-model:value="value" multiple :dataSource="dataSource"> </IxSelect>
<IxSelect v-model:value="value">
<IxSelectOptionGroup key="manager" label="Manager">
<IxSelectOption key="tom" label="Tom"></IxSelectOption>
Expand Down
27 changes: 20 additions & 7 deletions packages/components/select/src/composables/useOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface FlattenedOption {
disabled?: boolean
rawData: SelectData
type: 'group' | 'item'
parentKey?: VKey
}

export function useConvertedOptions(props: SelectProps, slots: Slots): ComputedRef<SelectData[]> {
Expand Down Expand Up @@ -57,7 +58,7 @@ export function useFlattenedOptions(

const filterKeys = [optionKey, optionGroupKey]

function convertOptions(nodes: VNode[] | undefined): SelectData[] {
function convertOptions(nodes: VNode[] | undefined, parentKey?: VKey): SelectData[] {
const convertedOptions: Array<SelectData> = []

flattenNode(nodes, { key: filterKeys }).forEach((node, index) => {
Expand All @@ -75,14 +76,15 @@ function convertOptions(nodes: VNode[] | undefined): SelectData[] {
disabled: _disabled,
label,
value,
parentKey,
customLabel: customLabel ?? customLabel2,
}

convertedOptions.push(option)
} else {
const { key = index, label, children } = props
const { label: customLabel, default: defaultSlot } = slots
const _children = children ?? convertOptions(defaultSlot?.())
const _children = children ?? convertOptions(defaultSlot?.(), key)

convertedOptions.push({ key, label, children: _children, customLabel })
}
Expand Down Expand Up @@ -133,28 +135,39 @@ function filterOptions(

function flattenOptions(options: SelectData[] | undefined, childrenKey: string, getKeyFn: GetKeyFn, labelKey: string) {
const mergedOptions: FlattenedOption[] = []
const appendOption = (item: SelectData, index?: number) =>
mergedOptions.push(parseOption(item, item => getKeyFn(item) ?? index, childrenKey, labelKey))
const appendOption = (item: SelectData, index: number | undefined, parentKey?: VKey) => {
const parsedOption = parseOption(item, item => getKeyFn(item) ?? index, childrenKey, labelKey, parentKey)
mergedOptions.push(parsedOption)

return parsedOption.key
}

options?.forEach((item, index) => {
const children = item[childrenKey] as SelectData[]

appendOption(item, index)
const optionKey = appendOption(item, index)

if (children && children.length > 0) {
children.forEach(child => {
appendOption(child)
appendOption(child, undefined, optionKey)
})
}
})

return mergedOptions
}

function parseOption(option: SelectData, getKey: GetKeyFn, childrenKey: string, labelKey: string): FlattenedOption {
function parseOption(
option: SelectData,
getKey: GetKeyFn,
childrenKey: string,
labelKey: string,
parentKey?: VKey,
): FlattenedOption {
const children = option[childrenKey] as SelectData[] | undefined
return {
key: getKey(option),
parentKey,
label: option[labelKey],
disabled: !!option.disabled,
type: children && children.length > 0 ? 'group' : 'item',
Expand Down
22 changes: 14 additions & 8 deletions packages/components/select/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@
color: @select-icon-color;
}
}

.@{select-prefix}-option-group:not(:first-child) {
border-top: @select-option-group-border;
}

.@{select-prefix}-option-grouped {
padding-left: @select-option-grouped-padding-left;
}
}
&-panel {
overflow: auto;
Expand All @@ -85,6 +77,20 @@
background-color: @select-option-active-background-color;
}
}

.@{select-prefix}-option-grouped {
padding-left: @select-multiple-option-grouped-padding-left;
}
}

.@{select-prefix}-option-group:not(:first-child) {
border-top: @select-option-group-border;
}

&:not(&-multiple) {
.@{select-prefix}-option-grouped {
padding-left: @select-option-grouped-padding-left;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
@select-option-group-color: @color-graphite;
@select-option-group-margin: 0 @spacing-md;
@select-option-group-padding-left: 0;
@select-multiple-option-grouped-padding-left: 0;
@select-option-grouped-padding-left: @spacing-xl;

@select-option-container-padding: @spacing-sm 0;
Expand Down

0 comments on commit 8807a31

Please sign in to comment.