options-test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. import Vue, { PropType, VNode } from '../index'
  2. import { ComponentOptions, Component } from '../index'
  3. import { CreateElement } from '../vue'
  4. interface MyComponent extends Vue {
  5. a: number
  6. }
  7. const option: ComponentOptions<MyComponent> = {
  8. data() {
  9. return {
  10. a: 123
  11. }
  12. }
  13. }
  14. // contravariant generic should use never
  15. const anotherOption: ComponentOptions<never> = option
  16. const componentType: Component = option
  17. Vue.component('sub-component', {
  18. components: {
  19. a: Vue.component(''),
  20. b: {}
  21. }
  22. })
  23. Vue.component('prop-component', {
  24. props: {
  25. size: Number,
  26. name: {
  27. type: String,
  28. default: '0',
  29. required: true
  30. }
  31. },
  32. data() {
  33. return {
  34. fixedSize: this.size.toFixed(),
  35. capName: this.name.toUpperCase()
  36. }
  37. }
  38. })
  39. Vue.component('string-prop', {
  40. props: ['size', 'name'],
  41. data() {
  42. return {
  43. fixedSize: this.size.whatever,
  44. capName: this.name.isany
  45. }
  46. }
  47. })
  48. class User {
  49. private u = 1
  50. }
  51. class Cat {
  52. private u = 1
  53. }
  54. interface IUser {
  55. foo: string
  56. bar: number
  57. }
  58. interface ICat {
  59. foo: any
  60. bar: object
  61. }
  62. type ConfirmCallback = (confirm: boolean) => void
  63. Vue.component('union-prop', {
  64. props: {
  65. cat: Object as PropType<ICat>,
  66. complexUnion: { type: [User, Number] as PropType<User | number> },
  67. kittyUser: Object as PropType<ICat & IUser>,
  68. callback: Function as PropType<ConfirmCallback>,
  69. union: [User, Number] as PropType<User | number>
  70. },
  71. data() {
  72. this.cat
  73. this.complexUnion
  74. this.kittyUser
  75. this.callback(true)
  76. this.union
  77. return {
  78. fixedSize: this.union
  79. }
  80. }
  81. })
  82. Vue.component('union-prop-with-no-casting', {
  83. props: {
  84. mixed: [RegExp, Array],
  85. object: [Cat, User],
  86. primitive: [String, Number],
  87. regex: RegExp
  88. },
  89. data() {
  90. this.mixed
  91. this.object
  92. this.primitive
  93. this.regex.compile
  94. }
  95. })
  96. Vue.component('prop-with-primitive-default', {
  97. props: {
  98. id: {
  99. type: String,
  100. default: () => String(Math.round(Math.random() * 10000000))
  101. }
  102. },
  103. created(): void {
  104. this.id
  105. }
  106. })
  107. Vue.component('component', {
  108. data() {
  109. this.$mount
  110. this.size
  111. return {
  112. a: 1
  113. }
  114. },
  115. props: {
  116. size: Number,
  117. name: {
  118. type: String,
  119. default: '0',
  120. required: true
  121. }
  122. },
  123. propsData: {
  124. msg: 'Hello'
  125. },
  126. computed: {
  127. aDouble(): number {
  128. return this.a * 2
  129. },
  130. aPlus: {
  131. get(): number {
  132. return this.a + 1
  133. },
  134. set(v: number) {
  135. this.a = v - 1
  136. },
  137. cache: false
  138. }
  139. },
  140. methods: {
  141. plus(): void {
  142. this.a++
  143. this.aDouble.toFixed()
  144. this.aPlus = 1
  145. this.size.toFixed()
  146. }
  147. },
  148. watch: {
  149. a: function (val: number, oldVal: number) {
  150. console.log(`new: ${val}, old: ${oldVal}`)
  151. },
  152. b: 'someMethod',
  153. c: {
  154. handler(val, oldVal) {
  155. this.a = val
  156. },
  157. deep: true
  158. },
  159. d: {
  160. handler: 'someMethod',
  161. immediate: true
  162. }
  163. },
  164. el: '#app',
  165. template: '<div>{{ message }}</div>',
  166. render(createElement) {
  167. return createElement(
  168. 'div',
  169. {
  170. attrs: {
  171. id: 'foo'
  172. },
  173. props: {
  174. myProp: 'bar'
  175. },
  176. directives: [
  177. {
  178. name: 'a',
  179. value: 'foo'
  180. }
  181. ],
  182. domProps: {
  183. innerHTML: 'baz'
  184. },
  185. on: {
  186. click: new Function()
  187. },
  188. nativeOn: {
  189. click: new Function()
  190. },
  191. class: {
  192. foo: true,
  193. bar: false
  194. },
  195. style: {
  196. color: 'red',
  197. fontSize: '14px'
  198. },
  199. key: 'myKey',
  200. ref: 'myRef',
  201. refInFor: true
  202. },
  203. [
  204. createElement(),
  205. createElement('div', 'message'),
  206. createElement(Vue.component('component')),
  207. createElement({} as ComponentOptions<Vue>),
  208. createElement({
  209. functional: true,
  210. render(c: CreateElement) {
  211. return createElement()
  212. }
  213. }),
  214. createElement(() => Vue.component('component')),
  215. createElement(() => ({} as ComponentOptions<Vue>)),
  216. createElement((resolve, reject) => {
  217. resolve({} as ComponentOptions<Vue>)
  218. reject()
  219. }),
  220. 'message',
  221. [createElement('div', 'message')]
  222. ]
  223. )
  224. },
  225. renderError(createElement, err) {
  226. return createElement('pre', { style: { color: 'red' } }, err.stack)
  227. },
  228. staticRenderFns: [],
  229. beforeCreate() {
  230. ;(this as any).a = 1
  231. },
  232. created() {},
  233. beforeDestroy() {},
  234. destroyed() {},
  235. beforeMount() {},
  236. mounted() {},
  237. beforeUpdate() {},
  238. updated() {},
  239. activated() {},
  240. deactivated() {},
  241. errorCaptured(err, vm, info) {
  242. err.message
  243. vm.$emit('error')
  244. info.toUpperCase()
  245. return true
  246. },
  247. serverPrefetch() {
  248. return Promise.resolve()
  249. },
  250. directives: {
  251. a: {
  252. bind() {},
  253. inserted() {},
  254. update() {},
  255. componentUpdated() {},
  256. unbind() {}
  257. },
  258. b(el, binding, vnode, oldVnode) {
  259. el.textContent
  260. binding.name
  261. binding.value
  262. binding.oldValue
  263. binding.expression
  264. binding.arg
  265. binding.modifiers['modifier']
  266. }
  267. },
  268. components: {
  269. a: Vue.component(''),
  270. b: {} as ComponentOptions<Vue>
  271. },
  272. transitions: {},
  273. filters: {
  274. double(value: number) {
  275. return value * 2
  276. }
  277. },
  278. parent: new Vue(),
  279. mixins: [Vue.component(''), {} as ComponentOptions<Vue>],
  280. name: 'Component',
  281. extends: {} as ComponentOptions<Vue>,
  282. delimiters: ['${', '}']
  283. })
  284. Vue.component('custom-prop-type-function', {
  285. props: {
  286. callback: Function as PropType<(confirm: boolean) => void>
  287. },
  288. methods: {
  289. confirm() {
  290. this.callback(true)
  291. }
  292. }
  293. })
  294. Vue.component('provide-inject', {
  295. provide: {
  296. foo: 1
  297. },
  298. inject: {
  299. injectFoo: 'foo',
  300. injectBar: Symbol(),
  301. injectBaz: { from: 'baz' },
  302. injectQux: { default: 1 },
  303. injectQuux: { from: 'quuz', default: () => ({ value: 1 }) }
  304. }
  305. })
  306. Vue.component('provide-function', {
  307. provide: () => ({
  308. foo: 1
  309. })
  310. })
  311. Vue.component('component-with-slot', {
  312. render(h): VNode {
  313. return h('div', this.$slots.default)
  314. }
  315. })
  316. Vue.component('component-with-scoped-slot', {
  317. render(h) {
  318. interface ScopedSlotProps {
  319. msg: string
  320. }
  321. return h('div', [
  322. h('child', [
  323. // default scoped slot as children
  324. (props: ScopedSlotProps) => [h('span', [props.msg])]
  325. ]),
  326. h('child', {
  327. scopedSlots: {
  328. // named scoped slot as vnode data
  329. item: (props: ScopedSlotProps) => [h('span', [props.msg])]
  330. }
  331. }),
  332. h('child', [
  333. // return single VNode (will be normalized to an array)
  334. (props: ScopedSlotProps) => h('span', [props.msg])
  335. ]),
  336. h('child', {
  337. // Passing down all slots from parent
  338. scopedSlots: this.$scopedSlots
  339. }),
  340. h('child', {
  341. // Passing down single slot from parent
  342. scopedSlots: {
  343. default: this.$scopedSlots.default
  344. }
  345. })
  346. ])
  347. },
  348. components: {
  349. child: {
  350. render(this: Vue, h: CreateElement) {
  351. const defaultSlot = this.$scopedSlots['default']!({ msg: 'hi' })
  352. defaultSlot &&
  353. defaultSlot.forEach(vnode => {
  354. vnode.tag
  355. })
  356. return h('div', [
  357. defaultSlot,
  358. this.$scopedSlots['item']!({ msg: 'hello' })
  359. ])
  360. }
  361. }
  362. }
  363. })
  364. Vue.component('narrow-array-of-vnode-type', {
  365. render(h): VNode {
  366. const slot = this.$scopedSlots.default!({})
  367. if (typeof slot === 'string') {
  368. // <template slot-scope="data">bare string</template>
  369. return h('span', slot)
  370. } else if (Array.isArray(slot)) {
  371. // template with multiple children
  372. const first = slot[0]
  373. if (!Array.isArray(first) && typeof first !== 'string' && first) {
  374. return first
  375. } else {
  376. return h()
  377. }
  378. } else if (slot) {
  379. // <div slot-scope="data">bare VNode</div>
  380. return slot
  381. } else {
  382. // empty template, slot === undefined
  383. return h()
  384. }
  385. }
  386. })
  387. Vue.component('functional-component', {
  388. props: ['prop'],
  389. functional: true,
  390. inject: ['foo'],
  391. render(createElement, context) {
  392. context.props
  393. context.children
  394. context.slots()
  395. context.data
  396. context.parent
  397. context.scopedSlots
  398. context.listeners.click
  399. return createElement('div', {}, context.children)
  400. }
  401. })
  402. Vue.component('functional-component-object-inject', {
  403. functional: true,
  404. inject: {
  405. foo: 'foo',
  406. bar: Symbol(),
  407. baz: { from: 'baz' },
  408. qux: { default: 1 },
  409. quux: { from: 'quuz', default: () => ({ value: 1 }) }
  410. },
  411. render(h) {
  412. return h('div')
  413. }
  414. })
  415. Vue.component('functional-component-check-optional', {
  416. functional: true
  417. })
  418. Vue.component('functional-component-multi-root', {
  419. functional: true,
  420. render(h) {
  421. return [
  422. h('tr', [h('td', 'foo'), h('td', 'bar')]),
  423. h('tr', [h('td', 'lorem'), h('td', 'ipsum')])
  424. ]
  425. }
  426. })
  427. Vue.component('async-component', (resolve, reject) => {
  428. setTimeout(() => {
  429. resolve(Vue.component('component'))
  430. }, 0)
  431. return new Promise(resolve => {
  432. resolve({
  433. functional: true,
  434. render(h: CreateElement) {
  435. return h('div')
  436. }
  437. })
  438. })
  439. })
  440. Vue.component('functional-component-v-model', {
  441. props: ['foo'],
  442. functional: true,
  443. model: {
  444. prop: 'foo',
  445. event: 'change'
  446. },
  447. render(createElement, context) {
  448. return createElement('input', {
  449. on: {
  450. input: new Function()
  451. },
  452. domProps: {
  453. value: context.props.foo
  454. }
  455. })
  456. }
  457. })
  458. Vue.component('async-es-module-component', () => import('./es-module'))
  459. Vue.component('directive-expression-optional-string', {
  460. render(createElement) {
  461. return createElement('div', {
  462. directives: [
  463. {
  464. name: 'has-expression',
  465. value: 2,
  466. expression: '1 + 1'
  467. },
  468. {
  469. name: 'no-expression',
  470. value: 'foo'
  471. }
  472. ]
  473. })
  474. }
  475. })