|
| 1 | +@page "/" |
| 2 | +@using Newtonsoft.Json.Linq |
| 3 | +@using Orbyss.Blazor.JsonForms.Context.Models |
| 4 | + |
| 5 | + |
| 6 | +<h1>Syncfusion Json Form!</h1> |
| 7 | + |
| 8 | +<label for="languageselector"> |
| 9 | + Change language |
| 10 | +</label> |
| 11 | +<select id="languageselector" @bind="selectedLanguage"> |
| 12 | + <option value="">-- Select Language --</option> |
| 13 | + @foreach (var lan in languages) |
| 14 | + { |
| 15 | + <option value="@lan">@lan</option> |
| 16 | + } |
| 17 | +</select> |
| 18 | + |
| 19 | +@if (string.IsNullOrWhiteSpace(value)) |
| 20 | +{ |
| 21 | + <CascadingValue Value="@selectedLanguage"> |
| 22 | + |
| 23 | + <JsonForm InitOptions="@options" |
| 24 | + OnSubmit="@OnSubmit" /> |
| 25 | + |
| 26 | + </CascadingValue> |
| 27 | +} |
| 28 | +else |
| 29 | +{ |
| 30 | + <p>Submitted form data:</p> |
| 31 | + <p>@value</p> |
| 32 | +} |
| 33 | + |
| 34 | +@code { |
| 35 | + string? value; |
| 36 | + |
| 37 | + const string jsonSchema = "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"required\":[\"name\",\"address\",\"status\",\"dateOfBirth\",\"requestedOn\"],\"properties\":{\"name\":{\"type\":\"string\"},\"dateOfBirth\":{\"type\":\"string\",\"format\":\"date\"},\"address\":{\"type\":\"object\",\"required\":[\"street\",\"number\"],\"properties\":{\"street\":{\"type\":\"string\",\"minLength\":5},\"number\":{\"type\":\"string\",\"maxLength\":5}}},\"requestedOn\":{\"type\":\"string\",\"format\":\"datetime\"},\"status\":{\"type\":\"string\",\"enum\":[\"Pending\",\"Approved\",\"Rejected\"]}}}"; |
| 38 | + const string uiSchema = "{\"type\":\"VerticalLayout\",\"elements\":[{\"type\":\"Control\",\"scope\":\"#/properties/name\"},{\"type\":\"Control\",\"scope\":\"#/properties/dateOfBirth\"},{\"type\":\"Control\",\"scope\":\"#/properties/address/properties/street\"},{\"type\":\"Control\",\"scope\":\"#/properties/address/properties/number\"},{\"type\":\"Control\",\"scope\":\"#/properties/status\"},{\"type\":\"Control\",\"scope\":\"#/properties/requestedOn\"}]}"; |
| 39 | + const string translationSchemaJson = "{\"resources\":{\"en\":{\"translation\":{\"name\":{\"label\":\"Name\"},\"dateOfBirth\":{\"label\":\"Date of birth\"},\"status\":{\"label\":\"Status\",\"Pending\":\"Pending\",\"Approved\":\"Approved\",\"Rejected\":\"Rejected\"},\"address\":{\"label\":\"Address\",\"street\":{\"label\":\"Street\",\"error\":{\"minimumLength\":\"Custom message: street must have more than 5 characters\"}},\"number\":{\"label\":\"Street number\",\"error\":{\"maximumLength\":\"Custom message: street number can have no more than 5 characters\"}}},\"requestedOn\":{\"label\":\"Requested on\"}}},\"nl\":{\"translation\":{\"name\":{\"label\":\"Naam\"},\"dateOfBirth\":{\"label\":\"Geboortedatum\"},\"status\":{\"label\":\"Status\",\"Pending\":\"In afwachting\",\"Approved\":\"Goedgekeurd\",\"Rejected\":\"Afgekeurd\"},\"address\":{\"label\":\"Adres\",\"street\":{\"label\":\"Straat\",\"error\":{\"minimumLength\":\"Custom bericht: Straat moet minimaal 5 karaketers bevatten\"}},\"number\":{\"label\":\"Straatnummer\",\"error\":{\"maximumLength\":\"Custom bericht: Straat nummer mag maximaal 5 karaketers bevatten\"}}},\"requestedOn\":{\"label\":\"Aangevraag op\"}}}}}"; |
| 40 | + |
| 41 | + private static readonly JsonFormContextInitOptions options = new(jsonSchema, uiSchema, translationSchemaJson); |
| 42 | + |
| 43 | + string? selectedLanguage = ""; |
| 44 | + string[] languages = |
| 45 | + [ |
| 46 | + "nl", |
| 47 | + "en" |
| 48 | + ]; |
| 49 | + |
| 50 | + Task OnSubmit(JToken value) |
| 51 | + { |
| 52 | + this.value = $"{value}"; |
| 53 | + return InvokeAsync(StateHasChanged); |
| 54 | + } |
| 55 | + |
| 56 | + protected override void OnInitialized() |
| 57 | + { |
| 58 | + base.OnInitialized(); |
| 59 | + } |
| 60 | +} |
0 commit comments