diff --git a/src/grid/grid-item.tsx b/src/grid/grid-item.tsx new file mode 100644 index 000000000..8611122bf --- /dev/null +++ b/src/grid/grid-item.tsx @@ -0,0 +1,77 @@ +import { defineComponent, getCurrentInstance, computed, inject } from 'vue'; +import isObject from 'lodash/isObject'; +import isString from 'lodash/isString'; +import isFunction from 'lodash/isFunction'; + +import config from '../config'; +import gridItemProps from './grid-item-props'; +import { useTNodeJSX } from '../hooks/tnode'; + +const { prefix } = config; +const name = `${prefix}-grid-item`; + +export default defineComponent({ + name, + props: gridItemProps, + setup(props, context) { + const internalInstance = getCurrentInstance(); + const renderTNodeJSX = useTNodeJSX(); + const { column, border, align, gutter } = inject('grid'); + + const rootStyle = computed(() => { + const percent = column.value > 0 ? `${100 / +column.value}%` : 0; + const style: Record = { + textAlign: ['center', 'left'].includes(align.value) ? align.value : 'center', + }; + if (percent !== 0) { + style.flexBasis = percent; + } + return style; + }); + + const size = computed(() => { + if (column.value > 4 || !column.value) return 'small'; + return column.value < 4 ? 'large' : 'middle'; + }); + + const realImage = computed(() => { + if (isString(props.image)) return { src: props.image }; + if (isObject(props.image) && !isFunction(props.image) && !context.slots.image) { + return props.image; + } + return null; + }); + + const gridItemClasses = computed(() => [ + `${name}`, + `${name}--${props.layout}`, + { [`${name}--bordered`]: border, [`${name}--surround`]: border && gutter }, + ]); + + return () => { + const renderImage = () => + realImage.value ? : renderTNodeJSX('image'); + + return ( +
+
+ {props.badge ? ( + + {realImage.value ? : renderTNodeJSX('image')} + + ) : ( + renderImage() + )} +
+ +
+
{renderTNodeJSX('text')}
+
+ {renderTNodeJSX('description')} +
+
+
+ ); + }; + }, +}); diff --git a/src/grid/grid-item.vue b/src/grid/grid-item.vue deleted file mode 100644 index 9afec3dbf..000000000 --- a/src/grid/grid-item.vue +++ /dev/null @@ -1,96 +0,0 @@ - - - diff --git a/src/grid/grid.vue b/src/grid/grid.tsx similarity index 71% rename from src/grid/grid.vue rename to src/grid/grid.tsx index b2bd54e6f..6be3994c6 100644 --- a/src/grid/grid.vue +++ b/src/grid/grid.tsx @@ -1,20 +1,3 @@ - - - diff --git a/src/grid/index.ts b/src/grid/index.ts index b0cb83d36..c93bac818 100644 --- a/src/grid/index.ts +++ b/src/grid/index.ts @@ -1,5 +1,5 @@ -import LocalGrid from './grid.vue'; -import LocalGridItem from './grid-item.vue'; +import LocalGrid from './grid'; +import LocalGridItem from './grid-item'; import { withInstall, WithInstallType } from '../shared'; import { TdGridItemProps, TdGridProps } from './type';