Limenius / liform-react

Generate forms from JSON Schema to use with React (& redux-form)
https://limenius.github.io/liform-react/
MIT License
174 stars 40 forks source link

Using Format to genarate form widget #21

Closed gastonche closed 6 years ago

gastonche commented 7 years ago

I have been taking a look at json-schema and liform-react. I know it is possible to specify a format for a field. The current implementation of liform-react utilises the type property of every schema-property to determine the widget to make use of. However the use the format property together with the type property will make the choosing of appropriate widget more effective. For example,

"properties": {
                    "contents": {
                        "description": "content (plain-text only)", 
                        "format": "textarea", 
                        "propertyOrder": 3, 
                        "type": "string"
                    }, 
                    "mode": {
                        "default": "0644", 
                        "description": "filesystem permissions", 
                        "maxLength": 4, 
                        "minLength": 3, 
                        "pattern": "^[0-7]*$", 
                        "propertyOrder": 2, 
                        "type": "string"
                    }, 
                    "path": {
                        "description": "filesystem path", 
                        "propertyOrder": 1, 
                        "type": "string"
                    }
                }

from the above schema, we can see that using the type property of content, path and mode will give us an appropriate widget, however what if we did have a textarea widget and we made us of the format property of content to choose the widget. That'd more efficient. My proposal is that we check for the format property and return if a valid widget is found for it, else we fallback to using the type property. This double checking will help increase our widget choice efficiency.

nacmartin commented 7 years ago

We can already do this. If you have a property

 'color': { 'type':'string', 'widget': 'color', 'title': 'In which color' },

It will look for the widget color. If it was

 'color': { 'type':'string', 'title': 'In which color' },

It would use the widget string instead.

gastonche commented 7 years ago

I was following the json-schema standards following this docs, and i believe it's going to be more standards to enable the format property too for this check given that most people may already have followed those standards and setup their schema with the format property.

nacmartin commented 7 years ago

Ok, it may be good idea:

If widget is present, use widget.

Otherwise: If format is present, then use format.

Otherwise: use type.

Would that be ok?

gastonche commented 7 years ago

Yes, that will be okay, I will clean up the code I have written for this and submit a PR by the end of the week.