Skip to content

Commit

Permalink
refactor(comp:all): updalte all components ts definition (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
danranVm authored Jan 3, 2021
1 parent 063b4cb commit bc8ff33
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 36 deletions.
31 changes: 16 additions & 15 deletions packages/cdk/utils/typeof.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/** The method checks whether the given value is a Numeric value or not and returns the corresponding boolean value. */

const _toString = Object.prototype.toString

export function isNumeric(value: unknown): boolean {
return !isNaN(parseFloat(value as string)) && isFinite(value as number)
/** The method checks whether the given value is a Numeric value or not and returns the corresponding boolean value. */
export function isNumeric(val: unknown): boolean {
return !isNaN(parseFloat(val as string)) && isFinite(val as number)
}

export function isUndefined(val: unknown): val is undefined {
return typeof val === 'undefined'
}

export function isNil(value: unknown): value is null | undefined {
return typeof value === 'undefined' || value === null
export function isNull(val: unknown): val is null {
return val === null
}

export function isNonNil<T>(value: T): value is NonNullable<T> {
return typeof value !== 'undefined' && value !== null
export function isNil(val: unknown): val is null | undefined {
return isUndefined(val) || isNull(val)
}

export function isNonNil<T>(val: T): val is NonNullable<T> {
return !isNil(val)
}

export function isNumber(val: unknown): val is number {
Expand All @@ -26,14 +35,6 @@ export function isString(val: unknown): val is string {
return typeof val === 'string'
}

export function isUndefined(val: unknown): val is undefined {
return typeof val === 'undefined'
}

export function isNull(val: unknown): val is null {
return val === null
}

export function isSymbol(val: unknown): val is symbol {
return typeof val === 'symbol'
}
Expand Down
5 changes: 3 additions & 2 deletions packages/components/badge/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DefineComponent } from 'vue'

export interface BadgeProps {
// Badge显示的数字
readonly count: number | string
Expand All @@ -15,5 +17,4 @@ export interface SlotsExist {
count: boolean
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IxBadgeComponent extends BadgeProps {}
export type IxBadgeComponent = InstanceType<DefineComponent<BadgeProps>>
2 changes: 1 addition & 1 deletion packages/components/button/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ IxButton.install = installComponent(IxButton)
IxButtonGroup.install = installComponent(IxButtonGroup)

export { IxButton, IxButtonGroup }
export type { ButtonComponent, ButtonGroupComponent } from './src/types'
export type { IxButtonComponent, IxButtonGroupComponent } from './src/types'
7 changes: 3 additions & 4 deletions packages/components/button/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DefineComponent } from 'vue'
import type { ButtonMode, ComponentSize } from '@idux/components/core/types'

export type ButtonShape = 'circle' | 'round'
Expand All @@ -13,14 +14,12 @@ export interface ButtonProps {
readonly icon?: string
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ButtonComponent extends ButtonProps {}
export type IxButtonComponent = InstanceType<DefineComponent<ButtonProps>>

export interface ButtonGroupProps {
readonly mode?: ButtonMode
readonly size?: ComponentSize
readonly shape?: ButtonShape
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ButtonGroupComponent extends ButtonGroupProps {}
export type IxButtonGroupComponent = InstanceType<DefineComponent<ButtonGroupProps>>
4 changes: 2 additions & 2 deletions packages/components/divider/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DefineComponent } from 'vue'
import type { DividerPosition, DividerType } from '@idux/components/core/types'

export interface DividerProps {
Expand All @@ -11,5 +12,4 @@ export interface DividerProps {
type?: DividerType
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IxDividerComponent extends DividerProps {}
export type IxDividerComponent = InstanceType<DefineComponent<DividerProps>>
2 changes: 1 addition & 1 deletion packages/components/icon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ addIconDefinitions(innerStaticIcons)
IxIcon.install = installComponent(IxIcon)

export { IxIcon, addIconDefinitions, fetchFromIconfont }
export type { IconComponent, IconDefinition } from './src/types'
export type { IxIconComponent, IconDefinition } from './src/types'
5 changes: 3 additions & 2 deletions packages/components/icon/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { DefineComponent } from 'vue'

export interface IconProps {
readonly name?: string
readonly rotate?: boolean | number | string
readonly iconfont?: boolean
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IconComponent extends IconProps {}
export type IxIconComponent = InstanceType<DefineComponent<IconProps>>

export interface IconDefinition {
name: string
Expand Down
5 changes: 3 additions & 2 deletions packages/components/image/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { DefineComponent } from 'vue'

export interface ImageProps {
readonly src?: string
readonly width?: string | number
Expand All @@ -12,5 +14,4 @@ export interface ImagePreviewProps {
}
export type ImageStatus = 'loading' | 'loaded' | 'failed'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IxImageComponent extends ImageProps {}
export type IxImageComponent = InstanceType<DefineComponent<ImageProps>>
6 changes: 3 additions & 3 deletions scripts/gen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ if (moduleName === 'components') {
// 这里都是硬编码,有没有更好的实现方式?
let currIndexContent = readFileSync(indexFilePath, 'utf-8')
currIndexContent = currIndexContent
.replace('\n\n', `\nimport { Ix${upperFirstComponentName} } from './${compName}'\n\n`)
.replace('\n\n', `\n\nimport { Ix${upperFirstComponentName} } from './${compName}'\n\n`)
.replace(']', `, Ix${upperFirstComponentName}]`)
currIndexContent += `export * from './${compName}'\n`
currIndexContent += `\nexport * from './${compName}'\n`
writeFileSync(indexFilePath, currIndexContent)

const currLess = readFileSync(componentsLessPath, 'utf-8')
Expand All @@ -84,5 +84,5 @@ if (moduleName === 'components') {
const docsZhTemplate = getDocsZhTemplate(upperFirstComponentName, moduleName)
writeFileSync(`${componentDirname}/docs/index.zh.md`, docsZhTemplate)

const domeTemplate = getDomeTemplate(upperFirstComponentName, moduleName)
const domeTemplate = getDomeTemplate(compName)
writeFileSync(`${componentDirname}/demo/basic.md`, domeTemplate)
23 changes: 19 additions & 4 deletions scripts/gen/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ export function getLessTemplate(compName: string): string {
}

export function getTypesTemplate(compName: string): string {
return `export interface ${compName}Props {
return `import type { DefineComponent } from 'vue'
export interface ${compName}Props {
// please add readonly for every prop
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Ix${compName}Component extends ${compName}Props {}
export type Ix${compName}Component = InstanceType<DefineComponent<${compName}Props>>
`
}

Expand Down Expand Up @@ -120,7 +121,7 @@ cover:
`
}

export function getDomeTemplate(compName: string, moduleName: string): string {
export function getDomeTemplate(compName: string): string {
return `---
order: 0
title:
Expand All @@ -139,6 +140,20 @@ title:
## demo
\`\`\`html
<template>
<ix-${compName} />
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
setup() {
}
})
</script>
<style lang="less" scoped>
</style>
\`\`\`
`
Expand Down

0 comments on commit bc8ff33

Please sign in to comment.