Skip to content

Commit 533edc7

Browse files
committed
2.1.0 tsx attribute types, in class render
1 parent 6e56bdd commit 533edc7

18 files changed

+134
-14
lines changed

docs/_sidebar.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
- Inheritance
1616
- [ECMAScript class](inheritance/es-class/es-class.md)
1717
- [Component](inheritance/component/component.md)
18-
- [Complex example](inheritance/complex-example/complex-example.md)
18+
- [Complex example](inheritance/complex-example/complex-example.md)
19+
- TSX
20+
- [Attribute types](tsx/attribute-types/attribute-types.md)

docs/class-component/component-property/code-option-type.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export default class MyComponent extends Vue {
1818
@Prop({
1919
type: String
2020
})
21-
p!: string
21+
p?: string
2222
}

docs/class-component/component-property/code-option-validator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export default class MyComponent extends Vue {
2222
return true
2323
}
2424
})
25-
p!: string
25+
p?: string
2626
}

docs/class-component/component-property/code-usage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ Vue option component
1616
@Component
1717
export default class MyComponent extends Vue {
1818
@Prop
19-
p!: string
19+
p?: string
2020
}

docs/class-component/component/component.md

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ This is the `render` in vue option component api.
5454

5555
If you use vue Single-File component. The render will be applied to the `export default` component automatically.
5656

57+
> This will overwrite the render in class body.
58+
5759
[](./code-option-template.ts ':include :type=code typescript')
5860

5961
### template

docs/class-component/event/event.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Usage
22

3-
You could define a method which triggers a vue event by `Emit` decorator.
3+
We could define a method which triggers a vue event by `Emit` decorator.
44

55
The decorator received an optional event name paramater. Event will be triggered with this name and the method returned value. If the event name parameter is omitted, use method's name by default.
66

docs/class-component/lifecycle-hook/lifecycle-hook.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ Class component supports almost all lifecycle hooks in vanilla vue. Write then a
2323
"renderTracked",
2424
"renderTriggered",
2525
"errorCaptured",
26-
"serverPrefetch"
26+
"serverPrefetch",
27+
"render"
2728
]
2829
```
2930

31+
All these hooks must be methods of a class, not properties.
32+
3033
## For other names
3134

3235
If your hook names aren't in the above list. You could use `options` or `modifier` in `Component` decorator.

docs/class-component/method/method.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Usage
22

3-
In a class component, you could define methods of vue option component as class methods directly. Library will analyze methods and transform them into `methods` option.
3+
In a class component, we could define methods of vue option component as class methods directly. Library will analyze methods and transform them into `methods` option.
44

55
[](./code-usage.ts ':include :type=code typescript')

docs/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
},
3030
"include": [
3131
"./**/*.ts",
32+
"./**/*.tsx",
3233
],
3334
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Usage
2+
3+
To make enable TSX attribute types:
4+
5+
1. Import `TSX` function from this repo.
6+
7+
2. Define an interface(i.e. `Props`) to decribe properties in component.
8+
9+
3. Make your component extend from `TSX<Props>()(BaseComponent)`.
10+
11+
> There are two `()`s after `TSX<Props>`.
12+
13+
[](./code-usage.tsx ':include :type=code tsx')
14+
15+
## Type checking
16+
17+
We could check if component implements properties by `implements`.
18+
19+
[](./code-type-checking.tsx ':include :type=code tsx')
20+
21+
## Component inheritance
22+
23+
TSX attributes supports component inheritance.
24+
25+
[](./code-component-inheritance.tsx ':include :type=code tsx')
26+
27+
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { TSX, Prop, Component, ComponentBase, Vue } from 'vue-facing-decorator'
2+
3+
interface BaseProps {
4+
propNumber: number
5+
}
6+
7+
@ComponentBase
8+
class MyBaseComponent extends TSX<BaseProps>()(Vue) implements BaseProps {
9+
@Prop({
10+
required: true
11+
})
12+
propNumber!: number
13+
}
14+
15+
interface Props {
16+
propString: string
17+
}
18+
19+
@Component
20+
export default class MyComponent extends TSX<Props>()(MyBaseComponent) implements Props {
21+
@Prop({
22+
required: true
23+
})
24+
propString!: string
25+
}
26+
27+
function render() {
28+
return <MyComponent propString='foobar' propNumber={1}></MyComponent>
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TSX, Prop, Component, Vue } from 'vue-facing-decorator'
2+
3+
interface Props {
4+
propString: string
5+
}
6+
7+
@Component
8+
export default class MyComponent extends TSX<Props>()(Vue) implements Props {
9+
@Prop({
10+
required: true
11+
})
12+
propString!: string
13+
}
14+
15+
function render() {
16+
return <MyComponent propString='foobar'></MyComponent>
17+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TSX, Prop, Component, Vue } from 'vue-facing-decorator'
2+
3+
interface Props {
4+
propString: string
5+
}
6+
7+
@Component
8+
export default class MyComponent extends TSX<Props>()(Vue) {
9+
@Prop({
10+
required: true
11+
})
12+
propString!: string
13+
}
14+
15+
//Typescript will check component attributes in tsx.
16+
function render() {
17+
return <MyComponent propString='foobar'></MyComponent>
18+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-facing-decorator",
3-
"version": "2.0.4",
3+
"version": "2.1.0",
44
"description": "Vue typescript class and decorator based component.",
55
"main": "dist/index.js",
66
"keywords": [

src/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ export { decorator as Inject } from './option/inject'
77
export { decorator as Emit } from './option/emit'
88
export { decorator as VModel } from './option/vmodel'
99
import type {
10-
ComponentPublicInstance,
11-
ComponentOptions
10+
ComponentPublicInstance
1211
} from 'vue'
1312
const IdentifySymbol = Symbol('vue-facing-decorator-identify')
1413
export interface BaseTypeIdentify {
1514
[IdentifySymbol]: undefined
1615
}
16+
export function TSX<Properties extends {}>() {
17+
return function <C extends { new(): ComponentPublicInstance & BaseTypeIdentify }>(cons: C) {
18+
return cons as unknown as {
19+
new(): Omit<ComponentPublicInstance<(InstanceType<C>['$props']) & Properties>, keyof Properties> & InstanceType<C>//& ComponentPublicInstance & BaseTypeIdentify
20+
}
21+
}
22+
}
1723

1824
export const Base = class { } as {
1925
new(): ComponentPublicInstance & BaseTypeIdentify
2026
}
21-
22-
2327
export const Vue = Base

src/option/methodsAndLifecycle.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const LifecycleNames = [
1717
"renderTracked",
1818
"renderTriggered",
1919
"errorCaptured",
20-
"serverPrefetch"
20+
"serverPrefetch",
21+
"render"
2122
] as const
2223

2324
export function build(cons: Cons, optionBuilder: OptionBuilder) {

test/test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ import './option/vmodel'
1212
import './feature/lifecycle'
1313
import './feature/classExtends'
1414
import './feature/componentExtends'
15-
import './feature/extends'
15+
import './feature/extends'
16+
17+
import './tsx/attributeTypes'

test/tsx/attributeTypes.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component, TSX, Prop, Base } from '../../dist'
2+
interface Props {
3+
p: string
4+
}
5+
@Component
6+
class TsxAttributeTypes extends TSX<Props>()(Base) implements Props {
7+
@Prop({
8+
required: true
9+
})
10+
p!: string
11+
}
12+
13+
export default {}

0 commit comments

Comments
 (0)