|
| 1 | +const template = ` |
| 2 | +<div id="example-eight" class="container mb-3 mt-3"> |
| 3 | + <h1>vue-form-json-schema</h1> |
| 4 | + <h3> |
| 5 | + Example #8 |
| 6 | + <small class="text-muted">Registration form example using <a href="https://composition-api.vuejs.org/">Vue's composition API</a></small> |
| 7 | + </h3> |
| 8 | +
|
| 9 | + <form @submit="onSubmit" novalidate> |
| 10 | + <vue-form-json-schema |
| 11 | + v-model="mixinState.model" |
| 12 | + :schema="mixinState.schema" |
| 13 | + :ui-schema="mixinState.uiSchema" |
| 14 | + :options="mixinState.options" |
| 15 | + v-on:state-change="onChangeState" |
| 16 | + v-on:validated="onValidated" |
| 17 | + ref="form" |
| 18 | + ></vue-form-json-schema> |
| 19 | +
|
| 20 | + <div class="form-group"> |
| 21 | + <button type="submit" class="btn btn-primary">Submit form</button> |
| 22 | + </div> |
| 23 | +
|
| 24 | + <transition name="fade" mode="out-in"> |
| 25 | + <div |
| 26 | + v-if="mixinState.submitted && mixinState.success" |
| 27 | + class="alert alert-success" |
| 28 | + key="success" |
| 29 | + >Form submitted successfully!</div> |
| 30 | + <div |
| 31 | + v-if=" |
| 32 | + mixinState.submitted && |
| 33 | + !mixinState.success && |
| 34 | + mixinState.state.vfjsErrors && |
| 35 | + mixinState.state.vfjsErrors.length > 0 |
| 36 | + " |
| 37 | + class="alert alert-danger" |
| 38 | + key="has-errors" |
| 39 | + >Form has errors, please fix and resubmit!</div> |
| 40 | + <div |
| 41 | + v-if=" |
| 42 | + mixinState.submitted && |
| 43 | + !mixinState.success && |
| 44 | + mixinState.state.vfjsErrors && |
| 45 | + mixinState.state.vfjsErrors.length === 0 |
| 46 | + " |
| 47 | + class="alert alert-success" |
| 48 | + key="has-no-errors" |
| 49 | + >Form errors have been corrected. You can now resubmit the form.</div> |
| 50 | + </transition> |
| 51 | + </form> |
| 52 | +
|
| 53 | + <hr> |
| 54 | +
|
| 55 | + <h4>Valid</h4> |
| 56 | + <div>{{mixinState.valid}}</div> |
| 57 | +
|
| 58 | + <h4>Model</h4> |
| 59 | + <pretty-print :value="mixinState.model"></pretty-print> |
| 60 | +
|
| 61 | + <h4>Options</h4> |
| 62 | + <pretty-print :value="mixinState.options"></pretty-print> |
| 63 | +
|
| 64 | + <h4>Schema</h4> |
| 65 | + <pretty-print :value="mixinState.schema"></pretty-print> |
| 66 | +
|
| 67 | + <h4>UI Schema</h4> |
| 68 | + <pretty-print :value="mixinState.uiSchema"></pretty-print> |
| 69 | +
|
| 70 | + <h4>State</h4> |
| 71 | + <pretty-print :value="mixinState.state"></pretty-print> |
| 72 | +</div> |
| 73 | +`; |
| 74 | + |
| 75 | +window.Vue.use(window.vueCompositionApi.default); |
| 76 | + |
| 77 | +window.Vue.component('example-eight', { |
| 78 | + name: 'example-eight', |
| 79 | + template, |
| 80 | + setup() { |
| 81 | + const { mixinState } = window.useFormFields(); |
| 82 | + |
| 83 | + function onChangeState(value) { |
| 84 | + mixinState.state = value; |
| 85 | + } |
| 86 | + |
| 87 | + function getDataFromAPI() { |
| 88 | + return new Promise((resolve, reject) => { |
| 89 | + setTimeout(() => { |
| 90 | + resolve({ |
| 91 | + firstName: 'Firstname', |
| 92 | + lastName: 'Lastname', |
| 93 | + age: 18, |
| 94 | + }); |
| 95 | + }, 1000); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + function onValidated(value) { |
| 100 | + mixinState.valid = value; |
| 101 | + } |
| 102 | + |
| 103 | + function onSubmit(e) { |
| 104 | + e.preventDefault(); |
| 105 | + |
| 106 | + mixinState.submitted = true; |
| 107 | + mixinState.options = { |
| 108 | + ...mixinState.options, |
| 109 | + showValidationErrors: true, |
| 110 | + }; |
| 111 | + |
| 112 | + if (mixinState.valid) { |
| 113 | + mixinState.success = true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + window.vueCompositionApi.onMounted(() => { |
| 118 | + getDataFromAPI().then((response) => { |
| 119 | + mixinState.model = { ...response }; |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + return { |
| 124 | + mixinState, |
| 125 | + onChangeState, |
| 126 | + onValidated, |
| 127 | + onSubmit, |
| 128 | + }; |
| 129 | + }, |
| 130 | +}); |
| 131 | + |
| 132 | +window.Vue.config.productionTip = false; |
| 133 | + |
| 134 | +/* eslint-disable no-new */ |
| 135 | +new window.Vue({ |
| 136 | + el: '#app', |
| 137 | + template: '<example-eight />', |
| 138 | +}); |
0 commit comments