Skip to content

Commit

Permalink
Merge pull request #714 from shoutem/release/5.1.0
Browse files Browse the repository at this point in the history
Release/5.1.0 - master
  • Loading branch information
sstimac authored May 5, 2022
2 parents 80687dd + 194dfba commit bc9a8c7
Show file tree
Hide file tree
Showing 10 changed files with 2,351 additions and 2,201 deletions.
5 changes: 0 additions & 5 deletions components/ListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ class ListView extends PureComponent {
// data to display
mappedProps.data = data;

// key extractor
if (!keyExtractor) {
mappedProps.keyExtractor = (item, index) => index.toString();
}

// sections for SectionList
if (sections) {
mappedProps.sections = sections;
Expand Down
6 changes: 3 additions & 3 deletions examples/RestaurantsApp/package-lock.json

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

6 changes: 3 additions & 3 deletions examples/create-react-native-app/package-lock.json

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

7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export {
resolveFontStyle,
};

export {
ThemeVariableResolver,
defaultResolver as variableResolver,
resolveVariable,
createScopedResolver,
} from './services';

// Components
export { ActionSheet } from './components/ActionSheet';
export { View } from './components/View';
Expand Down
8 changes: 7 additions & 1 deletion init.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Theme } from '@shoutem/theme';
import getThemeStyle from './theme';
import {
defaultResolver as variableResolver,
} from './services';
import getThemeStyle, { defaultThemeVariables } from './theme';

function setDefaultThemeStyle() {
variableResolver.setVariables(defaultThemeVariables)

const theme = getThemeStyle();

Theme.setDefaultThemeStyle(theme);
}

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shoutem/ui",
"version": "5.0.1",
"version": "5.1.0",
"description": "Styleable set of components for React Native applications",
"scripts": {
"lint": "eslint .",
Expand Down
6 changes: 6 additions & 0 deletions services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {
ThemeVariableResolver,
defaultResolver,
resolveVariable,
createScopedResolver,
} from './variableResolver';
73 changes: 73 additions & 0 deletions services/variableResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import _ from 'lodash';
import autoBind from 'auto-bind';

const THEME_EXTENSION_SCOPE = 'shoutem.theme';

export class ThemeVariableResolver {
constructor(variables) {
this.variables = { ...variables };

autoBind(this);
}

setVariables(variables) {
this.variables = { ...variables };
}

getScopedVariable(scope, variable) {
if (!this.variables || _.isEmpty(this.variables)) {
throw new Error(
'Theme variable resolver not yet initialised. Please call `setVariables` prior to this function call',
);
}

const scopePath = _.split(scope, '.');
const variablePath = _.split(variable, '.');

return _.get(
this.variables,
[...scopePath, ...variablePath],
_.get(this.variables, variablePath)
);
}

// Currently supports 2 function signatures
// resolveVariable(variableName) -> Tries to get the variable under the root scope ( generic theme values )
// resolveVariable(scope, variableName) -> Tries to get the variable under specific scope
// Variable name can be stringified object path or a regular string, i.e -> text.color
resolveVariable(...params) {
if (arguments.length === 1) {
const variablePath = _.split(params[0], '.');

return _.get(this.variables, variablePath);
}

if (arguments.length === 2) {
return this.getScopedVariable(params[0], params[1]);
}

throw new Error(
"Invalid theme variable resolution call. Please use full signature \n-> resolveVariable(scope, variableName),\n or the shorthand if you're getting a variable from shoutem.theme extension \n->resolveVariable(variableName)",
);
}

createScopedResolver(scope) {
return (...params) => {
if (params.length === 1) {
return this.getScopedVariable(scope, params[0]);
}
if (params.length === 2) {
return this.getScopedVariable(params[0], params[1]);
}

return undefined;
};
}
}

// Expose both, the resolver service, and the default resolver working with
// shoutem default theme. This way the logic can be used for multiple themes/resolvers, or
// if needed, you can override the default theme variables using the setVariables method
export const defaultResolver = new ThemeVariableResolver();
export const resolveVariable = defaultResolver.resolveVariable;
export const createScopedResolver = defaultResolver.createScopedResolver;
Loading

0 comments on commit bc9a8c7

Please sign in to comment.