From c5f4b6f9822280a3e66e8dadda8de52d8c6a70f2 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Sat, 21 Oct 2017 11:08:26 -0700 Subject: [PATCH] Actually fix detection of Backspace and Delay elements d122bd1 attempted to fix detection of `Backspace` and `Delay` elements by checking their `type` instead of their `type.name` However, due to oversight, we were still performing the check with name inside Typist.jsx. This commit fixes that --- src/Typist.jsx | 20 +++++++------------- src/utils.js | 12 ++++++++++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Typist.jsx b/src/Typist.jsx index 43125d5..b779553 100644 --- a/src/Typist.jsx +++ b/src/Typist.jsx @@ -114,23 +114,17 @@ export default class Typist extends Component { typeLine = (line, lineIdx) => { if (!this.mounted) { return Promise.resolve(); } - // If `line` is not a string, it is either a `Backspace` or a `Delay` element. - // See `extractTextFromElement` - const isBackspaceOrDelayElement = typeof line !== 'string'; - let decoratedLine = line; const { onLineTyped } = this.props; - if (isBackspaceOrDelayElement) { - if (line.type && line.type.name === 'Backspace') { - if (line.props.delay > 0) { - this.introducedDelay = line.props.delay; - } - decoratedLine = String('🔙').repeat(line.props.count); - } else if (line.type && line.type.name === 'Delay') { - this.introducedDelay = line.props.ms; - decoratedLine = '⏰'; + if (utils.isBackspaceElement(line)) { + if (line.props.delay > 0) { + this.introducedDelay = line.props.delay; } + decoratedLine = String('🔙').repeat(line.props.count); + } else if (utils.isDelayElement(line)) { + this.introducedDelay = line.props.ms; + decoratedLine = '⏰'; } return new Promise((resolve, reject) => { diff --git a/src/utils.js b/src/utils.js index e371329..8e5f274 100644 --- a/src/utils.js +++ b/src/utils.js @@ -33,6 +33,14 @@ export function exclude(obj, keys) { return res; } +export function isBackspaceElement(element) { + return element && element.type === Backspace; +} + +export function isDelayElement(element) { + return element && element.type === Delay; +} + export function extractTextFromElement(element) { const stack = element ? [element] : []; const lines = []; @@ -40,7 +48,7 @@ export function extractTextFromElement(element) { while (stack.length > 0) { const current = stack.pop(); if (React.isValidElement(current)) { - if (current.type === Backspace || current.type === Delay) { + if (isBackspaceElement(current) || isDelayElement(current)) { // If it is a `Backspace` or `Delay` element, we want to keep it in our // `textLines` state. These will serve as markers when updating the // state of the text @@ -88,7 +96,7 @@ function cloneElementWithSpecifiedTextAtIndex(element, textLines, textIdx) { const isNonTypistElement = ( React.isValidElement(element) && - !(element.type === Delay || element.type === Backspace) + !(isBackspaceElement(element) || isDelayElement(element)) ); if (isNonTypistElement) {