fix: type rendering

This commit is contained in:
cha0s 2020-06-20 03:02:46 -05:00
parent ef28829e19
commit b3c5e760cc
3 changed files with 60 additions and 32 deletions

View File

@ -19,7 +19,7 @@ const Steps = (props) => {
else { else {
stepsList = contextStepsList( stepsList = contextStepsList(
context, context,
value.steps ? typeFromSteps(context, value.steps) : typeFromLiteral(value.value), value.steps ? typeFromSteps(context, value.steps) : typeFromLiteral(value),
); );
} }
return ( return (
@ -67,7 +67,7 @@ const Steps = (props) => {
type={ type={
arg.steps arg.steps
? typeFromSteps(context, arg.steps) ? typeFromSteps(context, arg.steps)
: typeFromLiteral(arg.value) : typeFromLiteral(arg)
} }
value={arg} value={arg}
/> />

View File

@ -1,24 +1,31 @@
import {Context} from '@avocado/behavior' import {Context} from '@avocado/behavior'
export function typeFromLiteral(v) { export function typeFromLiteral(v) {
if ('undefined' === typeof v) { console.log('tfl', v);
if ('literal' === v.type) {
const {value} = v;
if ('undefined' === typeof value) {
return 'undefined'; return 'undefined';
} }
if (null === v) { if (null === value) {
return 'null'; return 'null';
} }
if ('number' === typeof v) { if ('number' === typeof value) {
return 'number'; return 'number';
} }
if ('string' === typeof v) { if ('string' === typeof value) {
return 'string'; return 'string';
} }
if ('boolean' === typeof v) { if ('boolean' === typeof value) {
return 'bool'; return 'bool';
} }
if (v.length && 2 === v.length && v instanceof Array) { if (value.length && 2 === value.length && value instanceof Array) {
return 'vector'; return 'vector';
} }
}
if ('actions' === v.type && v.traversals) {
return 'actions';
}
return 'object'; return 'object';
} }

View File

@ -13,12 +13,28 @@ import String from './string.type-renderer';
import {contextStepsList} from './steps-lists'; import {contextStepsList} from './steps-lists';
import {typeFits, typeFromSteps} from './typing'; import {typeFits, typeFromSteps} from './typing';
let TypeRenderers;
const ensureTypeRenderers = () => {
if (!TypeRenderers) {
const {all: allTypeRenderers} = require('./type-renderers.scwp');
TypeRenderers = Object.values(allTypeRenderers()).reduce((r, M) => {
const {default: {type, component}} = M;
return {...r, [type]: component};
}, {});
}
};
const renderValue = (context, type, value) => { const renderValue = (context, type, value) => {
ensureTypeRenderers();
switch (value.type) { switch (value.type) {
case 'traversal': { case 'traversal': {
return <Steps context={context} steps={value.steps} type={type} value={value.value} />; return <Steps context={context} steps={value.steps} type={type} value={value.value} />;
} }
case 'literal': case 'actions': {
const Component = TypeRenderers['actions'];
return <Component context={context} value={value} />;
}
case 'literal': {
const stepsList = contextStepsList(context, type); const stepsList = contextStepsList(context, type);
const tierOptions = Object.keys(stepsList.reduce((r, optionSteps) => { const tierOptions = Object.keys(stepsList.reduce((r, optionSteps) => {
if (!optionSteps[0] || !optionSteps[0].key) { if (!optionSteps[0] || !optionSteps[0].key) {
@ -39,23 +55,28 @@ const renderValue = (context, type, value) => {
</select> </select>
{ {
(() => { (() => {
switch (type) { const Component = TypeRenderers[type];
case 'bool': return Component
return <Bool.component value={value.value} />; ? <Component value={value.value} />
case 'number': : null;
return <Number.component value={value.value} />; // switch (type) {
case 'any': // case 'bool':
case 'string': // return <Bool.component value={value.value} />;
return <String.component value={value.value} />; // case 'number':
case 'object': // return <Number.component value={value.value} />;
return <ObjectType.component value={value.value} />; // case 'any':
default: // case 'string':
return null; // return <String.component value={value.value} />;
} // case 'object':
// return <ObjectType.component value={value.value} />;
// default:
// return null;
// }
})() })()
} }
</div> </div>
); );
}
default: default:
return null; return null;
} }