forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improvement] TreeSelect: tsx (youzan#2803)
- Loading branch information
1 parent
0ddafc7
commit 9c1248d
Showing
3 changed files
with
114 additions
and
102 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,105 @@ | ||
import { use } from '../utils'; | ||
import { emit, inherit } from '../utils/functional'; | ||
import Icon from '../icon'; | ||
|
||
// Types | ||
import { CreateElement, RenderContext } from 'vue/types'; | ||
import { DefaultSlots } from '../utils/use/sfc'; | ||
|
||
export type TreeSelectItem = { | ||
text: string; | ||
disabled?: boolean; | ||
children: TreeSelectChildren[]; | ||
}; | ||
|
||
export type TreeSelectChildren = { | ||
id: number; | ||
text: string; | ||
disabled?: boolean; | ||
}; | ||
|
||
export type TreeSelectProps = { | ||
height: number; | ||
items: TreeSelectItem[]; | ||
activeId: number | string; | ||
mainActiveIndex: number; | ||
}; | ||
|
||
const [sfc, bem] = use('tree-select'); | ||
|
||
function TreeSelect( | ||
h: CreateElement, | ||
props: TreeSelectProps, | ||
slots: DefaultSlots, | ||
ctx: RenderContext<TreeSelectProps> | ||
) { | ||
const { height, items, mainActiveIndex, activeId } = props; | ||
|
||
const selectedItem = items[mainActiveIndex] || {}; | ||
const subItems = selectedItem.children || []; | ||
|
||
return ( | ||
<div class={bem()} style={{ height: `${height}px` }} {...inherit(ctx)}> | ||
<div class={bem('nav')}> | ||
{items.map((item, index) => ( | ||
<div | ||
key={index} | ||
class={[ | ||
'van-ellipsis', | ||
bem('nitem', { | ||
active: mainActiveIndex === index, | ||
disabled: item.disabled | ||
}) | ||
]} | ||
onClick={() => { | ||
if (!item.disabled) { | ||
emit(ctx, 'navclick', index); | ||
} | ||
}} | ||
> | ||
{item.text} | ||
</div> | ||
))} | ||
</div> | ||
<div class={bem('content')}> | ||
{subItems.map(item => ( | ||
<div | ||
key={item.id} | ||
class={[ | ||
'van-ellipsis', | ||
bem('item', { | ||
active: activeId === item.id, | ||
disabled: item.disabled | ||
}) | ||
]} | ||
onClick={() => { | ||
if (!item.disabled) { | ||
emit(ctx, 'itemclick', item); | ||
} | ||
}} | ||
> | ||
{item.text} | ||
{activeId === item.id && ( | ||
<Icon name="checked" size="16px" class={bem('selected')} /> | ||
)} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
TreeSelect.props = { | ||
items: Array, | ||
mainActiveIndex: Number, | ||
activeId: { | ||
type: [Number, String], | ||
default: 0 | ||
}, | ||
height: { | ||
type: Number, | ||
default: 300 | ||
} | ||
}; | ||
|
||
export default sfc<TreeSelectProps>(TreeSelect); |
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