Skip to content

Commit

Permalink
Amending props comparison. Fixes #545
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgef committed Apr 21, 2020
1 parent a078717 commit 79e6ad7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-simple-keyboard",
"version": "2.2.83",
"version": "2.3.0",
"description": "React.js Virtual Keyboard",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
12 changes: 9 additions & 3 deletions src/lib/components/Keyboard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useRef } from "react";
import Keyboard from "simple-keyboard";
import { parseProps, propsChanged } from "../services/Utilities";
import { parseProps, changedProps } from "../services/Utilities";
import "simple-keyboard/build/css/index.css";

const KeyboardReact = props => {
Expand All @@ -20,14 +20,20 @@ const KeyboardReact = props => {
props.keyboardRef && props.keyboardRef(keyboardRef.current);
}

const updatedProps = changedProps(previousProps.current, props);

/**
* Only trigger render if props changed
*/
if (propsChanged(previousProps.current, props)) {
if (updatedProps.length) {
let keyboard = keyboardRef.current;
previousProps.current = props;
keyboard.setOptions(parseProps(props));
props.debug && console.log("ReactSimpleKeyboard: Rendered");
props.debug &&
console.log(
"ReactSimpleKeyboard - Re-render due to updated props:",
updatedProps
);
}
}, [initRef, cssClass, previousProps, props]);

Expand Down
19 changes: 12 additions & 7 deletions src/lib/services/Utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ export const parseProps = props => ({
theme: `simple-keyboard ${props.theme || "hg-theme-default"}`
});

export const propsChanged = (prevProps, props) => {
const cleanProps = sourceObj =>
JSON.stringify({
...sourceObj,
stateToIgnore: null
});
const cleanProps = sourceObj => ({
...sourceObj,
keyboardRef: null,
stateToIgnore: null
});

export const changedProps = (prevProps, props) => {
const cleanedProps = cleanProps(props);
const cleanedPrevProps = cleanProps(prevProps);

return cleanProps(props) !== cleanProps(prevProps);
return Object.keys(cleanedProps).filter(
propName => cleanedProps[propName] !== cleanedPrevProps[propName]
);
};

0 comments on commit 79e6ad7

Please sign in to comment.