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 Nov 5, 2021
1 parent 45626c4 commit e14c13f
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/components/Text/RNText.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
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;
}

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

/**
* @ignore
*/
Expand All @@ -23,7 +35,23 @@ export const setTextProps = (
set children(text: string | number | Array<string | number>) {
text = Array.isArray(text) ? text.join('') : text;

widget.setText(text);
// Empty Text fields should not write "undefined"
if (text === undefined) {
widget.textOfChildren = "";
} else {
widget.textOfChildren = `${text}`;
}
widget.setText(widget.textOfChildren);
},
set elideMode(mode: TextElideMode) {
if (oldProps.elideMode !== mode && widget.resizeListener !== undefined) {
widget.removeEventListener(WidgetEventTypes.Resize, widget.resizeListener);
}
if (oldProps.elideMode !== mode && mode !== undefined) {
widget.resizeListener = elideTextListener(widget, mode);
widget.addEventListener(WidgetEventTypes.Resize, widget.resizeListener);
widget.resizeListener();
}
},
set wordWrap(shouldWrap: boolean) {
widget.setWordWrap(shouldWrap);
Expand All @@ -34,7 +62,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 All @@ -46,6 +74,9 @@ export const setTextProps = (
* @ignore
*/
export class RNText extends QLabel implements RNWidget {
textOfChildren?: string;
resizeListener?: () => void;

setProps(newProps: TextProps, oldProps: TextProps): void {
setTextProps(this, newProps, oldProps);
}
Expand Down

0 comments on commit e14c13f

Please sign in to comment.