diff --git a/src/grid/__test__/index.test.jsx b/src/grid/__test__/index.test.jsx index e89089fc3..205260f07 100644 --- a/src/grid/__test__/index.test.jsx +++ b/src/grid/__test__/index.test.jsx @@ -1,7 +1,7 @@ import { mount } from '@vue/test-utils'; import { describe, it, expect } from 'vitest'; -import Grid from '../grid.vue'; -import GridItem from '../grid-item.vue'; +import Grid from '../grid'; +import GridItem from '../grid-item'; import Badge from '../../badge/badge'; const prefix = 't'; diff --git a/src/grid/grid-item.tsx b/src/grid/grid-item.tsx new file mode 100644 index 000000000..82199e8a9 --- /dev/null +++ b/src/grid/grid-item.tsx @@ -0,0 +1,73 @@ +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 props from './grid-item-props'; +import { useTNodeJSX } from '../hooks/tnode'; +import TImage from '../image'; +import TBadge from '../badge'; + +const { prefix } = config; +const name = `${prefix}-grid-item`; + +export default defineComponent({ + name, + components: { TImage, TBadge }, + props, + setup(props, context) { + 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.value, [`${name}--surround`]: border.value && gutter.value }, + ]); + + return () => { + const renderImage = () => + realImage.value ? : renderTNodeJSX('image'); + + return ( +
+
+ {props.badge ? {renderImage()} : 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 62% rename from src/grid/grid.vue rename to src/grid/grid.tsx index b2bd54e6f..4d498c112 100644 --- a/src/grid/grid.vue +++ b/src/grid/grid.tsx @@ -1,32 +1,15 @@ - - - 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';