From 4c468ffd74042d8dded184b83f62827d3835b019 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Mon, 21 Sep 2020 18:42:40 +0800 Subject: [PATCH] types(Layout): use tsx --- src/col/{index.js => index.tsx} | 23 ++++++++++++------- src/row/{index.js => index.tsx} | 39 +++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 19 deletions(-) rename src/col/{index.js => index.tsx} (60%) rename src/row/{index.js => index.tsx} (67%) diff --git a/src/col/index.js b/src/col/index.tsx similarity index 60% rename from src/col/index.js rename to src/col/index.tsx index c83d95856..b6751bee7 100644 --- a/src/col/index.js +++ b/src/col/index.tsx @@ -1,28 +1,35 @@ -import { computed } from 'vue'; +import { computed, PropType } from 'vue'; import { createNamespace } from '../utils'; import { useParent } from '../composition/use-relation'; -import { ROW_KEY } from '../row'; +import { ROW_KEY, RowProvide } from '../row'; const [createComponent, bem] = createNamespace('col'); export default createComponent({ props: { - span: [Number, String], offset: [Number, String], tag: { - type: String, + type: String as PropType, default: 'div', }, + span: { + type: [Number, String], + default: 0, + }, }, setup(props, { slots }) { - const { parent, index } = useParent(ROW_KEY, () => +props.span); + const { parent, index } = useParent(ROW_KEY, () => +props.span); const style = computed(() => { - const { spaces } = parent || {}; + if (!parent) { + return; + } - if (spaces && spaces.value && spaces.value[index.value]) { - const { left, right } = spaces.value[index.value]; + const { spaces } = parent; + + if (spaces && spaces.value && spaces.value[index!.value]) { + const { left, right } = spaces.value[index!.value]; return { paddingLeft: left ? `${left}px` : null, paddingRight: right ? `${right}px` : null, diff --git a/src/row/index.js b/src/row/index.tsx similarity index 67% rename from src/row/index.js rename to src/row/index.tsx index 7aed35600..05354eae1 100644 --- a/src/row/index.js +++ b/src/row/index.tsx @@ -1,17 +1,35 @@ -import { provide, computed, reactive } from 'vue'; +import { provide, computed, reactive, PropType, ComputedRef } from 'vue'; import { createNamespace } from '../utils'; const [createComponent, bem] = createNamespace('row'); -export const ROW_KEY = 'van-row'; +export const ROW_KEY = 'vanRow'; + +export type RowSpaces = { left?: number; right: number }[]; + +export type RowChild = () => number; + +export type RowProvide = { + spaces: ComputedRef; + children: RowChild[]; +}; + +export type RowAlign = 'top' | 'center' | 'bottom'; + +export type RowJustify = + | 'start' + | 'end' + | 'center' + | 'space-around' + | 'space-between'; export default createComponent({ props: { - type: String, - align: String, - justify: String, + type: String as PropType<'flex'>, + align: String as PropType, + justify: String as PropType, tag: { - type: String, + type: String as PropType, default: 'div', }, gutter: { @@ -21,10 +39,10 @@ export default createComponent({ }, setup(props, { slots }) { - const children = reactive([]); + const children = reactive([]); const groups = computed(() => { - const groups = [[]]; + const groups: number[][] = [[]]; let totalSpan = 0; children.forEach((getSpan, index) => { @@ -43,13 +61,12 @@ export default createComponent({ const spaces = computed(() => { const gutter = Number(props.gutter); + const spaces: RowSpaces = []; if (!gutter) { - return; + return spaces; } - const spaces = []; - groups.value.forEach((group) => { const averagePadding = (gutter * (group.length - 1)) / group.length;