Skip to content

Commit

Permalink
add reset() api
Browse files Browse the repository at this point in the history
  • Loading branch information
doanndd committed Sep 8, 2016
1 parent 9131df6 commit 38872c0
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -88,6 +88,9 @@ Manage theme components to allow updating content while changing the theme. `com
8. **Component**
Get current theme component like `theme.Container` or `import {Container} from 'react-native-theme'`

9. **reset()**
Reset all themes data

## Example

[See this example](https://github.com/apentle/react-native-theme-example)
6 changes: 6 additions & 0 deletions __tests__/index-test.js
Expand Up @@ -241,4 +241,10 @@ describe('theme', () => {
theme.addComponents(extraRed, 'red');
expect(theme.Button).toBe(extraRed.Button);
});

it('theme.reset(): reset theme data', () => {
theme.active();
theme.reset();
expect(theme.styles).toEqual({});
});
});
1 change: 1 addition & 0 deletions __tests__/layout-test.js
Expand Up @@ -91,5 +91,6 @@ describe('layout', () => {
}
]
});
layout.reset();
});
});
72 changes: 70 additions & 2 deletions index.js
Expand Up @@ -9,7 +9,12 @@ var _components = {};
var _current = 'default';
var _rootComponent = null;

// Utilities Functions
/**
* styleHasFunction - check style object has function
*
* @param {object} style object
* @returns {boolean}
*/
function styleHasFunction(style) {
for (var key in style) {
if (typeof style[key] === 'function') {
Expand All @@ -23,6 +28,12 @@ function styleHasFunction(style) {
return false;
}

/**
* platformSpecific - allow platforms specific styles
*
* @param {object} styles input styles object
* @returns {object} styles object for a specific platform
*/
function platformSpecific(styles) {
const result = {};
for (var key in styles) {
Expand All @@ -41,6 +52,12 @@ function platformSpecific(styles) {
return result;
}

/**
* defineComponent - private define theme component
*
* @param {object} type component's type
* @returns {undefined}
*/
function defineComponent(type) {
Object.defineProperty(Theme, type, {
get: function() {
Expand All @@ -57,9 +74,14 @@ function defineComponent(type) {

// Theme class
const Theme = {
/**
* styles - get styles depend on theme
*
* @returns {object} StyleSheet object
*/
get styles() {
if (_current === 'default') {
if (_themes[_current] !== undefined) {
if (_themes.default !== undefined) {
return _themes.default;
}
_proxies.default = {};
Expand All @@ -74,10 +96,22 @@ const Theme = {
return _proxies[_current];
},

/**
* name - get current theme name
*
* @returns {string}
*/
get name() {
return _current;
},

/**
* add - add style for a theme
*
* @param {object} styles styles to add
* @param {string} name = 'default' theme's name
* @returns {number}
*/
add(styles, name = 'default') {
// Check styles is processed
var processed = 2;
Expand Down Expand Up @@ -131,6 +165,12 @@ const Theme = {
return 0;
},

/**
* active - active a theme by name
*
* @param {string} name = 'default' theme's name
* @returns {undefined}
*/
active(name = 'default') {
if (name !== _current && _themes[name] !== undefined) {
_current = name;
Expand All @@ -145,6 +185,12 @@ const Theme = {
}
},

/**
* setRoot - set a component to be root of theme
*
* @param {object} root component
* @returns {undefined}
*/
setRoot(root) {
if (typeof root === 'undefined') {
_rootComponent = null;
Expand All @@ -155,6 +201,12 @@ const Theme = {
}
},

/**
* css - transform mixed styles in to RN styles object
*
* @param {object} styles mixed styles
* @returns {object} react native styles
*/
css(styles) {
if (typeof styles === 'string' || Array.isArray(styles)) {
if (typeof styles === 'string') {
Expand Down Expand Up @@ -187,6 +239,13 @@ const Theme = {
return styles;
},

/**
* addComponents - add components to a theme
*
* @param {object} components components object {name: component}
* @param {string} name = 'default' theme's name
* @returns {undefined}
*/
addComponents(components, name = 'default') {
if (typeof components !== 'object') {
console.warn('Expected argument to be an object.');
Expand All @@ -204,6 +263,15 @@ const Theme = {
Theme[type] !== undefined || defineComponent(type);
}
},

/**
* reset - reset themes data
*/
reset() {
_themes = {};
_proxies = {};
_components = {};
},
};

module.exports = Theme;
26 changes: 19 additions & 7 deletions layout.js
Expand Up @@ -2,10 +2,15 @@

const React = require('react');

const layouts = {};
const _layouts = {};


/**
* Add modify function for each layout with key
* addModify - allow plugins to modify components
*
* @param {object} layout modifies layout object
* @param {string} keyPrefix = '' prefix key for layout_key to apply
* @returns {undefined}
*/
function addModify(layout, keyPrefix = '') {
if (typeof layout !== 'object') {
Expand All @@ -19,17 +24,24 @@ function addModify(layout, keyPrefix = '') {
var modifies = layout[layoutKey];
if (typeof modifies === 'function') {
var key = keyPrefix + layoutKey;
if (layouts[key] === undefined) {
layouts[key] = [modifies];
if (_layouts[key] === undefined) {
_layouts[key] = [modifies];
} else {
layouts[key].push(modifies);
_layouts[key].push(modifies);
}
} else if (typeof modifies === 'object') {
addModify(modifies, keyPrefix + layoutKey + '_');
}
}
}

/**
* reset - reset layout modifies object
*/
addModify.reset = function() {
_layouts = {};
}

if (typeof global._createRNElement === 'undefined') {
global._createRNElement = function(type, config, child) {
// Process children
Expand All @@ -53,8 +65,8 @@ if (typeof global._createRNElement === 'undefined') {
var layoutContext = config && config.layoutContext;
var params = [result];
var applyModify = function (key) {
if (layouts[key] !== undefined) {
var modifies = layouts[key];
var modifies = _layouts[key];
if (modifies !== undefined) {
for (var i = 0, modifiesLength = modifies.length; i < modifiesLength; i++) {
modifies[i].apply(layoutContext, params);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "react-native-theme",
"version": "0.1.3",
"version": "0.1.4",
"description": "Theme manager for react native project",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 38872c0

Please sign in to comment.