Skip to content

Commit

Permalink
Add elideMode to Text component
Browse files Browse the repository at this point in the history
  • Loading branch information
NinZine committed Oct 14, 2021
1 parent 45626c4 commit 79fa540
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/components/Text/RNText.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { QLabel, NodeWidget, QLabelSignals, TextInteractionFlag } from '@nodegui/nodegui';
import { QLabel, NodeWidget, QLabelSignals, TextInteractionFlag, QFontMetrics, TextElideMode, WidgetEventTypes } from '@nodegui/nodegui';
import { ViewProps, setViewProps } from '../View/RNView';
import { RNWidget } from '../config';
import { throwUnsupported } from '../../utils/helpers';

export interface TextProps extends ViewProps<QLabelSignals> {
children?: string | number | Array<string | number>;
wordWrap?: boolean;
elideMode?: TextElideMode;
scaledContents?: boolean;
openExternalLinks?: boolean;
textInteractionFlags?: TextInteractionFlag;
Expand All @@ -23,7 +24,22 @@ export const setTextProps = (
set children(text: string | number | Array<string | number>) {
text = Array.isArray(text) ? text.join('') : text;

widget.setText(text);
if (this.elideMode !== undefined) {
const elideMode = this.elideMode;
const elidedText = () => {
const metrics = new QFontMetrics(widget.font());
const lines = widget.wordWrap() ? Math.floor(widget.size().height() / metrics.lineSpacing()) : 1;
return metrics.elidedText(`${text}`, elideMode, (widget.size().width() - metrics.maxWidth()) * lines);
}

widget.addEventListener(WidgetEventTypes.Resize, () => {
widget.setText(elidedText());
});

widget.setText(elidedText());
} else {
widget.setText(text);
}
},
set wordWrap(shouldWrap: boolean) {
widget.setWordWrap(shouldWrap);
Expand All @@ -34,7 +50,7 @@ export const setTextProps = (
set openExternalLinks(shouldOpenExternalLinks: boolean) {
widget.setProperty('openExternalLinks', shouldOpenExternalLinks);
},
set textInteractionFlags(interactionFlag: TextInteractionFlag){
set textInteractionFlags(interactionFlag: TextInteractionFlag) {
widget.setProperty('textInteractionFlags', interactionFlag);
}
};
Expand Down

0 comments on commit 79fa540

Please sign in to comment.