-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete refactor. Add comments and reorganize files
- Loading branch information
Jon Q
committed
Dec 9, 2019
1 parent
99e32b5
commit cad1644
Showing
22 changed files
with
412 additions
and
259 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
2 changes: 1 addition & 1 deletion
2
src/__tests__/ControlPanel.test.js → src/components/__tests__/Controls.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Controls'; |
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 @@ | ||
export * from './useControls'; |
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,64 @@ | ||
import { useRef, useEffect, useState } from 'react'; | ||
|
||
import { store, getFields, resetFields } from '../store'; | ||
import * as knobs from '../knobs'; | ||
|
||
/** | ||
* A special hook that "connects" the store with a rendered component. | ||
* The hook callback arguments provide knobs that allow for the user | ||
* to add fields to the store. | ||
*/ | ||
export function useControls() { | ||
const [fields, setFields] = useState(getFields()); | ||
const ref = useRef(false); | ||
|
||
useEffect(() => { | ||
const updateState = () => setFields(getFields()); | ||
|
||
/** | ||
* Forces an update during the initial render phase. | ||
* This is required after the initial connection to the store. | ||
* Otherwise, the added fields do not show on first render. | ||
*/ | ||
if (!ref.current) { | ||
ref.current = true; | ||
updateState(); | ||
} | ||
|
||
/** | ||
* A subscription is established to the store to "connect" to store | ||
* state together with this hook state. | ||
*/ | ||
store.subscribe(updateState); | ||
|
||
return () => { | ||
/** | ||
* The default behaviour is to remove all fields within the store | ||
* the moment the component is unmounted from view. | ||
*/ | ||
resetFields(); | ||
/** | ||
* The subscription is removed from the store on unmount. | ||
*/ | ||
store.unsubscribe(updateState); | ||
}; | ||
}, [ref, setFields]); | ||
|
||
const attributes = mapStateToProps(fields); | ||
|
||
return { ...knobs, fields, attributes }; | ||
} | ||
|
||
/** | ||
* Remaps the collection of fields into a Object of key/value pairs. | ||
* @param {Array<Object>} state The fields. | ||
* @returns {Object} | ||
*/ | ||
function mapStateToProps(state) { | ||
return state.reduce((props, item) => { | ||
return { | ||
...props, | ||
[item.prop]: item.value, | ||
}; | ||
}, {}); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './useControls'; | ||
export * from './Controls'; | ||
export * from './components'; | ||
export * from './hooks'; | ||
export * from './knobs'; | ||
export * from './store'; |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export * from './knobs'; |
Oops, something went wrong.