-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christopher Baker
committed
Jun 28, 2018
1 parent
a45b45c
commit fa76736
Showing
3 changed files
with
62 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import canDiff from 'can-diff'; | ||
|
||
function controlled(nextProps) { | ||
// always use the latest props, overwiting existing data | ||
return nextProps; | ||
} | ||
|
||
function uncontrolled(nextProps, lastProps) { | ||
// use props on initial render (when lastProps is null) | ||
if (lastProps === null) { | ||
return nextProps; | ||
} | ||
|
||
// otherwise do nothing | ||
return null; | ||
} | ||
|
||
function changes(nextProps, lastProps) { | ||
// use props on initial render (when lastProps is null) | ||
if (lastProps === null) { | ||
return nextProps; | ||
} | ||
|
||
const patches = canDiff.map(lastProps, nextProps); | ||
if (!patches.length) { | ||
return null; | ||
} | ||
|
||
const diff = {}; | ||
for (const { key, type, value } of patches) { | ||
if (type === 'set' || type === 'add') { | ||
diff[key] = value; | ||
} | ||
} | ||
|
||
return diff; | ||
} | ||
|
||
export { | ||
controlled, | ||
uncontrolled, | ||
changes, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters