types(TreeSelect): use tsx (#8126)

This commit is contained in:
neverland 2021-02-10 22:33:57 +08:00 committed by GitHub
parent 6b28f12e7e
commit 6d465b17a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 11 deletions

View File

@ -1,3 +1,5 @@
import { PropType } from 'vue';
// Utils // Utils
import { createNamespace, addUnit } from '../utils'; import { createNamespace, addUnit } from '../utils';
@ -8,6 +10,21 @@ import SidebarItem from '../sidebar-item';
const [createComponent, bem] = createNamespace('tree-select'); const [createComponent, bem] = createNamespace('tree-select');
export type TreeSelectChild = {
id: number | string;
text: string;
disabled?: boolean;
};
export type TreeSelectItem = {
dot?: boolean;
text: string;
badge?: number | string;
children?: TreeSelectChild[];
disabled?: boolean;
className?: any;
};
export default createComponent({ export default createComponent({
props: { props: {
max: { max: {
@ -15,7 +32,7 @@ export default createComponent({
default: Infinity, default: Infinity,
}, },
items: { items: {
type: Array, type: Array as PropType<TreeSelectItem[]>,
default: () => [], default: () => [],
}, },
height: { height: {
@ -23,7 +40,9 @@ export default createComponent({
default: 300, default: 300,
}, },
activeId: { activeId: {
type: [Number, String, Array], type: [Number, String, Array] as PropType<
number | string | Array<number | string>
>,
default: 0, default: 0,
}, },
selectedIcon: { selectedIcon: {
@ -44,22 +63,20 @@ export default createComponent({
], ],
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const isMultiple = () => Array.isArray(props.activeId); const isActiveItem = (id: number | string) => {
return Array.isArray(props.activeId)
const isActiveItem = (id) => {
return isMultiple()
? props.activeId.indexOf(id) !== -1 ? props.activeId.indexOf(id) !== -1
: props.activeId === id; : props.activeId === id;
}; };
const renderSubItem = (item) => { const renderSubItem = (item: TreeSelectChild) => {
const onClick = () => { const onClick = () => {
if (item.disabled) { if (item.disabled) {
return; return;
} }
let activeId; let activeId;
if (isMultiple()) { if (Array.isArray(props.activeId)) {
activeId = props.activeId.slice(); activeId = props.activeId.slice();
const index = activeId.indexOf(item.id); const index = activeId.indexOf(item.id);
@ -97,7 +114,7 @@ export default createComponent({
); );
}; };
const onSidebarChange = (index) => { const onSidebarChange = (index: number) => {
emit('update:mainActiveIndex', index); emit('update:mainActiveIndex', index);
emit('click-nav', index); emit('click-nav', index);
}; };

7
src/vue-shim.d.ts vendored
View File

@ -1,5 +1,7 @@
import 'vue'; import 'vue';
type EventHandler = (...args: any[]) => void;
// TODO // TODO
// should be removed after Vue supported component events typing // should be removed after Vue supported component events typing
// see: https://github.com/vuejs/vue-next/issues/1553 // see: https://github.com/vuejs/vue-next/issues/1553
@ -8,7 +10,8 @@ declare module 'vue' {
interface ComponentCustomProps { interface ComponentCustomProps {
role?: string; role?: string;
tabindex?: number; tabindex?: number;
onClick?: (event: MouseEvent) => void; onClick?: EventHandler;
onClosed?: () => void; onClosed?: EventHandler;
onChange?: EventHandler;
} }
} }