Hello there,
I have a following problem, which I would like to solve with Reason
I would like to build an abstract ReasonReact UI builder, which knows how to render bool, number, string, etc object property type into appropriate ReasonReact components.. So once I have this high-level ReasonReact UI builder, I will not have to writes renderers for each type in my app for UI.
Say, I have such type:
let personType = { alive: bool, name: string, lastname: string, age: number };
And I have a lot of instances of this type:
let person1:personType = resultOfSumFunc(); let person2:personType = resultOfSumFunc();
I would like to use ReasonReact to build UI which shows all properties of these objects (person1, person2).
For example, if object has property of type bool, then ReasonReact should render <Checkbox/>
component, if number - <NumberPicker/>
, string - <Label/>
(component names do not matter).
Currently, I hard code property names in interface builder like this (pseudo ReasonReact code):
<Person>
<Label value=person1.name/>
<NumberPicker initialValue=person1.age/>
<Checkbox label=person1.name checked=person1.alive/>
</Person>
You see, that property names are hard coded, and if I extend later my personType with another property:
let personType = { married: bool, alive: bool, name: string, lastname: string, age: number };
I will have to add to ReasonReact renderer:
<Person> <Label value=person1.name/> <NumberPicker initialValue=person1.age/> <Checkbox label=person1.name checked=person1.alive/> <Checkbox label="Married" checked=person1.married/> </Person>
What I want to achieve:
let mainComponent = BuildReactFromObject(person1);
BuildReactFromObject just iterates over properties of person1 (of any type) instance and adds appropriate UI components to it’s render result:
(pseudo code):
render: (self) =>
<div>
for (let prop in self.props.instance) {
switch prop {
| String(s) => <Label value=s/>
| Bool(b) => <Checkbox checkeed=b/>
/* ...etc */
|> ReasonReact.arrayToElement
}
}
</div>