-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(pro:search): add 'cascader'
searchField
#1485
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,11 @@ interface TreeSelectData { | |
label: string | ||
children?: TreeSelectData[] | ||
} | ||
interface CascaderData { | ||
key: string | ||
label: string | ||
children?: TreeSelectData[] | ||
} | ||
|
||
const labels = ['Archer', 'Berserker', 'Lancer', 'Rider', 'Saber', 'Caster', 'Assassin'] | ||
const baseSelectData: SelectData[] = Array.from(new Array(50)).map((_, idx) => { | ||
|
@@ -66,6 +71,8 @@ const baseTreeSelectData: TreeSelectData[] = Array.from(new Array(20)).map((_, i | |
], | ||
} | ||
}) | ||
const baseCascaderData = baseTreeSelectData as CascaderData[] | ||
|
||
const createSelectData = (searchValue: string) => { | ||
return baseSelectData.filter(item => new RegExp(searchValue.toLowerCase()).test(item.label.toLowerCase())) | ||
} | ||
|
@@ -74,17 +81,26 @@ const createTreeSelectData = (searchValue: string) => { | |
new RegExp(searchValue.toLowerCase()).test(item.label.toLowerCase()), | ||
) | ||
} | ||
const createCascaderData = (searchValue: string) => { | ||
return filterTree(baseCascaderData, 'children', item => | ||
new RegExp(searchValue.toLowerCase()).test(item.label.toLowerCase()), | ||
) | ||
} | ||
|
||
const value = ref<SearchValue[]>() | ||
const selectData = ref<SelectData[]>(createSelectData('')) | ||
const treeSelectData = ref<TreeSelectData[]>(createTreeSelectData('')) | ||
const cascaderData = ref<CascaderData[]>(createCascaderData('')) | ||
|
||
const selectOnSearch = (searchValue: string) => { | ||
selectData.value = createSelectData(searchValue) | ||
} | ||
const treeSelectOnSearch = (searchValue: string) => { | ||
treeSelectData.value = createTreeSelectData(searchValue) | ||
} | ||
const cascaderOnSearch = (searchValue: string) => { | ||
cascaderData.value = createCascaderData(searchValue) | ||
} | ||
|
||
const searchFields = computed<SearchField[]>(() => [ | ||
{ | ||
|
@@ -113,6 +129,19 @@ const searchFields = computed<SearchField[]>(() => [ | |
onSearch: treeSelectOnSearch, | ||
}, | ||
}, | ||
{ | ||
type: 'cascader', | ||
label: 'Cascader Data', | ||
key: 'cascader_data', | ||
fieldConfig: { | ||
multiple: true, | ||
searchable: true, | ||
cascaderStrategy: 'all', | ||
dataSource: cascaderData.value, | ||
searchFn: () => true, | ||
onSearch: cascaderOnSearch, | ||
}, | ||
}, | ||
]) | ||
|
||
const onChange = (value: SearchValue[] | undefined, oldValue: SearchValue[] | undefined) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with a brief overview of the code patch. The patch adds an additional interface called CascaderData, and adds a new search field with cascader data as its data source. The code also contains functions to create data for select, tree select, and cascader fields, as well as an onChange function. Now for the review, I'd suggest ensuring that the new interface is properly type-checked, since it's being used in multiple places. Additionally, it might be worth checking if the functions to create the data are optimized for performance. Finally, the onChange function should be thoroughly tested, as it's the main logic of the code. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import type { DateConfig } from '@idux/components/config' | |
|
||
import { type ComputedRef, type Slots, computed } from 'vue' | ||
|
||
import { createCascaderSegment } from '../segments/CreateCascaderSegment' | ||
import { createDatePickerSegment } from '../segments/CreateDatePickerSegment' | ||
import { createDateRangePickerSegment } from '../segments/CreateDateRangePickerSegment' | ||
import { createNameSegment } from '../segments/CreateNameSegment' | ||
|
@@ -73,6 +74,8 @@ function createSearchItemContentSegment( | |
return createSelectSegment(prefixCls, searchField) | ||
case 'treeSelect': | ||
return createTreeSelectSegment(prefixCls, searchField) | ||
case 'cascader': | ||
return createCascaderSegment(prefixCls, searchField) | ||
case 'input': | ||
return createInputSegment(prefixCls, searchField) | ||
case 'datePicker': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with the code review
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* @license | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE | ||
*/ | ||
|
||
import { defineComponent, inject, onUnmounted, watch } from 'vue' | ||
|
||
import { type VKey, callEmit } from '@idux/cdk/utils' | ||
import { CascaderPanelProps, IxCascaderPanel } from '@idux/components/cascader' | ||
|
||
import PanelFooter from './PanelFooter' | ||
import { proSearchContext } from '../token' | ||
import { proSearchCascaderPanelProps } from '../types' | ||
|
||
export default defineComponent({ | ||
props: proSearchCascaderPanelProps, | ||
setup(props) { | ||
const { mergedPrefixCls, locale } = inject(proSearchContext)! | ||
|
||
watch( | ||
() => props.searchValue, | ||
searchValue => { | ||
callEmit(props.onSearch, searchValue ?? '') | ||
}, | ||
) | ||
onUnmounted(() => { | ||
if (props.searchValue) { | ||
callEmit(props.onSearch, '') | ||
} | ||
}) | ||
|
||
const changeSelected = (keys: VKey[] | VKey[] | VKey[][]) => { | ||
callEmit(props.onChange, props.multiple ? keys : [keys]) | ||
} | ||
|
||
const handleConfirm = () => { | ||
callEmit(props.onConfirm) | ||
} | ||
const handleCancel = () => { | ||
callEmit(props.onCancel) | ||
} | ||
|
||
const renderFooter = () => { | ||
if (!props.multiple) { | ||
return | ||
} | ||
|
||
return ( | ||
<PanelFooter | ||
prefixCls={mergedPrefixCls.value} | ||
locale={locale} | ||
onConfirm={handleConfirm} | ||
onCancel={handleCancel} | ||
/> | ||
) | ||
} | ||
|
||
return () => { | ||
const prefixCls = `${mergedPrefixCls.value}-cascader-panel` | ||
const { | ||
dataSource, | ||
disableData, | ||
expandIcon, | ||
expandTrigger, | ||
fullPath, | ||
loadChildren, | ||
multiple, | ||
|
||
searchValue, | ||
searchFn, | ||
separator, | ||
strategy, | ||
virtual, | ||
|
||
onExpand, | ||
onLoaded, | ||
} = props | ||
const panelProps = { | ||
selectedKeys: props.value, | ||
dataSource, | ||
disableData, | ||
childrenKey: 'children', | ||
getKey: 'key', | ||
expandIcon, | ||
expandTrigger, | ||
fullPath, | ||
loadChildren, | ||
labelKey: 'label', | ||
multiple, | ||
searchFn, | ||
searchValue, | ||
separator, | ||
strategy, | ||
virtual, | ||
onExpand, | ||
onLoaded, | ||
'onUpdate:selectedKeys': changeSelected, | ||
} as CascaderPanelProps | ||
|
||
return ( | ||
<div class={prefixCls} tabindex={-1} onMousedown={evt => evt.preventDefault()}> | ||
<IxCascaderPanel {...panelProps} /> | ||
{renderFooter()} | ||
</div> | ||
) | ||
} | ||
}, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code review First of all, the code looks well formatted and organized. There is no risk of any bugs here. However, there are some potential improvements that can be made.
Overall, the code looks well written and bug-free. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with a brief code review.
Firstly, it looks like the code is written correctly, and there are no obvious bugs or errors in the code. However, there could be some possible improvements.
Firstly, it might be better to make sure that the dataSource is defined outside of the searchFields array, so that it can be more easily reused in other places.
Also, it would be good to add some validation when entering the data, to make sure all the keys and labels are unique.
Finally, it would be a good idea to add comments to the code, so that it is easier to understand what is going on.
Overall, it looks like the code is written correctly, and all the necessary checks have been made.