Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(comp:text): text component should inherit attrs #1731

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/components/text/demo/Maxheight.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 20
title:
zh: 最大高度
en: Max height
---

## zh

可以通过样式覆盖修改最大高度,实现内容纵向滚动

## en

Overwrite style of text component to make text content vertially scrollalbe with max height.
27 changes: 27 additions & 0 deletions packages/components/text/demo/Maxheight.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts" setup>
const text = Array.from(new Array(80))
.map(() => '@idux是一套企业级中后台UI组件库, 致力于提供高效愉悦的开发体验。')
.join()
</script>

<template>
<div class="demo-text-max-height">
<IxText :tooltip="false" :ellipsis="{ rows: 4, expandable: true }">
{{ text }}
<template #expandIcon="{ expanded }">
{{ expanded ? '收起' : '展开' }}
</template>
</IxText>
</div>
</template>

<style lang="less">
.demo-text-max-height {
width: 600px;
.ix-text-inner {
display: inline-block;
max-height: 300px;
overflow-y: auto;
}
}
</style>
4 changes: 1 addition & 3 deletions packages/components/text/src/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@

export default defineComponent({
name: 'IxText',
inheritAttrs: false,
props: textProps,
setup(props, { attrs, slots }) {
setup(props, { slots }) {
const common = useGlobalConfig('common')
const config = useGlobalConfig('text')
const mergedPrefixCls = computed(() => `${common.prefixCls}-text`)
Expand Down Expand Up @@ -108,74 +107,73 @@
)
}

return () => {
const { tag: Tag, tooltip } = props
const prefixCls = mergedPrefixCls.value
const isNative = tooltip === 'native'
const titleSlot = slots.title || slots.default

const hasExpandIcon = !!slots.expandIcon || !!expandIconRenderer.value
const expandNode =
expandable.value && hasExpandIcon ? (
<div class={`${prefixCls}-expand-icon`} onClick={toggleExpanded}>
{(slots.expandIcon ?? expandIconRenderer.value)!({ expanded: expanded.value })}
</div>
) : undefined

const contentNodes = slots.default?.()

let node = (
<Tag
ref={innerRef}
class={`${prefixCls}-inner`}
title={isNative && isEllipsis.value ? getStringBySlot(titleSlot) : undefined}
onClick={expandable.value && !hasExpandIcon ? toggleExpanded : undefined}
{...attrs}
>
{!isSimple.value && isEllipsis.value && !expanded.value && measureStatus.value === 'none'
? renderClampedContent(contentNodes)
: contentNodes}
{!isSimple.value && isEllipsis.value && !expanded.value && renderEllipsisNode()}
</Tag>
)

if (isEllipsis.value && !expanded.value && tooltip && !isNative) {
const tooltipProps = isObject(tooltip) ? tooltip : {}
node = (
<IxTooltip {...tooltipProps} v-slots={{ title: titleSlot }}>
{node}
</IxTooltip>
)
}

const classes = normalizeClass({
[prefixCls]: true,
[`${prefixCls}-simple`]: isSimple.value,
[`${prefixCls}-ellipsis`]: isEllipsis.value,
[`${prefixCls}-expandable`]: expandable.value,
[`${prefixCls}-has-expand-icon`]: hasExpandIcon,
})

onRender(contentNodes)

if (measureStatus.value !== 'none') {
onMeasureRender()
}

return (
<div ref={contentRef} class={classes}>
{node}
{slots.suffix?.()}
{renderCopyNode()}
{isEllipsis.value && expandNode}
{measureStatus.value !== 'none' && renderMeasureElement()}
{measureStatus.value === 'preparing' && renderRowMeasureElement()}
</div>
)
}
},
})

Check notice on line 176 in packages/components/text/src/Text.tsx

View check run for this annotation

codefactor.io / CodeFactor

packages/components/text/src/Text.tsx#L110-L176

Complex Method
function getStringBySlot(slot: Slot | undefined) {
const validNode = getFirstValidNode(slot?.())
if (!validNode) {
Expand Down