File tree Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -361,6 +361,76 @@ var AwesomeProject = React.createClass({
361361});
362362```
363363
364+ ## Dynamic forms example: how to change a form based on selection
365+
366+ Say I have an iospicker, depending on which option is selected in this picker I want the next component to either be a checkbox or a textbox:
367+
368+ ``` js
369+ const Country = t .enums ({
370+ ' IT' : ' Italy' ,
371+ ' US' : ' Unisted States'
372+ }, ' Country' );
373+
374+ var AwesomeProject = React .createClass ({
375+
376+ // returns the suitable type based on the form value
377+ getType (value ) {
378+ if (value .country === ' IT' ) {
379+ return t .struct ({
380+ country: Country,
381+ rememebrMe: t .Boolean
382+ });
383+ } else if (value .country === ' US' ) {
384+ return t .struct ({
385+ country: Country,
386+ name: t .String
387+ });
388+ } else {
389+ return t .struct ({
390+ country: Country
391+ });
392+ }
393+ },
394+
395+ getInitialState () {
396+ const value = {};
397+ return { value, type: this .getType (value) };
398+ },
399+
400+ onChange (value ) {
401+ // recalculate the type only if strictly necessary
402+ const type = value .country !== this .state .value .country ?
403+ this .getType (value) :
404+ this .state .type ;
405+ this .setState ({ value, type });
406+ },
407+
408+ onPress () {
409+ var value = this .refs .form .getValue ();
410+ if (value) {
411+ console .log (value);
412+ }
413+ },
414+
415+ render () {
416+
417+ return (
418+ < View style= {styles .container }>
419+ < t .form .Form
420+ ref= " form"
421+ type= {this .state .type }
422+ value= {this .state .value }
423+ onChange= {this .onChange }
424+ / >
425+ < TouchableHighlight style= {styles .button } onPress= {this .onPress } underlayColor= ' #99d9f4' >
426+ < Text style= {styles .buttonText }> Save< / Text >
427+ < / TouchableHighlight>
428+ < / View>
429+ );
430+ }
431+ });
432+ ```
433+
364434# Types
365435
366436### Required field
You can’t perform that action at this time.
0 commit comments