Skip to content

Commit

Permalink
Complete refactor. Add comments and reorganize files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Q committed Dec 9, 2019
1 parent 99e32b5 commit cad1644
Show file tree
Hide file tree
Showing 22 changed files with 412 additions and 259 deletions.
41 changes: 10 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@
"dependencies": {
"@itsjonq/elm": "^0.0.6",
"@itsjonq/is": "^0.0.2",
"deep-equal": "^1.1.1",
"memoize-one": "^5.1.1",
"react-useportal": "^1.0.13",
"reselect": "4.0.0",
"tinycolor2": "^1.4.1",
"unistore": "^3.5.1"
},
Expand Down
5 changes: 3 additions & 2 deletions src/Controls.js → src/components/Controls.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import usePortal from 'react-useportal';
import { View } from '@itsjonq/elm';
import { updateField } from './store';
import { useControls } from './useControls';

import { updateField } from '../store';
import { useControls } from '../hooks';
import { Field } from './Field';

export function BaseControls(props) {
Expand Down
3 changes: 2 additions & 1 deletion src/Field.js → src/components/Field.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import { View } from '@itsjonq/elm';
import { createUniqueIdFactory } from './utils';

import { createUniqueIdFactory } from '../utils';

const fieldId = createUniqueIdFactory('Field');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { cy } from '@itsjonq/cyan';
import { BaseControls as Controls } from '../index';
import { BaseControls as Controls } from '../../index';

describe('Controls', () => {
test('should render', () => {
Expand Down
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Controls';
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useControls';
64 changes: 64 additions & 0 deletions src/hooks/useControls.js
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,
};
}, {});
}
4 changes: 2 additions & 2 deletions src/index.js
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';
78 changes: 0 additions & 78 deletions src/knobs.js

This file was deleted.

1 change: 1 addition & 0 deletions src/knobs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './knobs';
Loading

0 comments on commit cad1644

Please sign in to comment.