|
| 1 | +import { Component, h, Host, State } from '@stencil/core'; |
| 2 | +import { |
| 3 | + ArrayItemControlsFormData, |
| 4 | + baseSchema, |
| 5 | +} from './array-item-controls-schema'; |
| 6 | +import { FormSchema } from '@limetech/lime-elements'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Allowing users to reorder and remove form items |
| 10 | + * By default, users are allowed to manipulate the array of items in by reordering them, |
| 11 | + * or removing them. |
| 12 | + * |
| 13 | + * However, using `allowItemReorder` and `allowItemRemoval` |
| 14 | + * props, you can control whether these actions are allowed. |
| 15 | + * |
| 16 | + * @sourceFile array-item-controls-schema.ts |
| 17 | + */ |
| 18 | +@Component({ |
| 19 | + tag: 'limel-example-form-array-item-controls', |
| 20 | + shadow: true, |
| 21 | +}) |
| 22 | +export class FormArrayItemControlsExample { |
| 23 | + @State() |
| 24 | + private formData: ArrayItemControlsFormData = { |
| 25 | + tasks: ['Collect intel', 'Recruit allies', 'Refuel jet'], |
| 26 | + missions: [ |
| 27 | + { |
| 28 | + name: 'Secure HQ', |
| 29 | + objective: 'Fortify base defenses', |
| 30 | + active: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'Scout sector 9', |
| 34 | + objective: 'Gather intelligence on rivals', |
| 35 | + active: false, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }; |
| 39 | + |
| 40 | + @State() |
| 41 | + private allowItemReorder = true; |
| 42 | + |
| 43 | + @State() |
| 44 | + private allowItemRemoval = true; |
| 45 | + |
| 46 | + @State() |
| 47 | + private schema = this.createSchema(); |
| 48 | + |
| 49 | + public render() { |
| 50 | + return ( |
| 51 | + <Host> |
| 52 | + <limel-example-controls |
| 53 | + style={{ '--example-controls-column-layout': 'auto-fit' }} |
| 54 | + > |
| 55 | + <limel-checkbox |
| 56 | + label="allowItemReorder" |
| 57 | + checked={this.allowItemReorder} |
| 58 | + onChange={this.toggleAllowReorder} |
| 59 | + /> |
| 60 | + <limel-checkbox |
| 61 | + label="allowItemRemoval" |
| 62 | + checked={this.allowItemRemoval} |
| 63 | + onChange={this.toggleAllowRemoval} |
| 64 | + /> |
| 65 | + </limel-example-controls> |
| 66 | + <limel-form |
| 67 | + onChange={this.handleFormChange} |
| 68 | + value={this.formData} |
| 69 | + schema={this.schema} |
| 70 | + /> |
| 71 | + <limel-example-value value={this.formData} /> |
| 72 | + </Host> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private handleFormChange = (event) => { |
| 77 | + this.formData = event.detail; |
| 78 | + }; |
| 79 | + |
| 80 | + private toggleAllowReorder = () => { |
| 81 | + this.allowItemReorder = !this.allowItemReorder; |
| 82 | + this.schema = this.createSchema(); |
| 83 | + }; |
| 84 | + |
| 85 | + private toggleAllowRemoval = () => { |
| 86 | + this.allowItemRemoval = !this.allowItemRemoval; |
| 87 | + this.schema = this.createSchema(); |
| 88 | + }; |
| 89 | + |
| 90 | + private createSchema(): FormSchema<ArrayItemControlsFormData> { |
| 91 | + const properties = baseSchema.properties ?? {}; |
| 92 | + const tasks = properties.tasks!; |
| 93 | + const missions = properties.missions!; |
| 94 | + const taskOptions = tasks.lime ?? {}; |
| 95 | + const missionOptions = missions.lime ?? {}; |
| 96 | + |
| 97 | + return { |
| 98 | + ...baseSchema, |
| 99 | + properties: { |
| 100 | + ...properties, |
| 101 | + tasks: { |
| 102 | + ...tasks, |
| 103 | + lime: { |
| 104 | + ...taskOptions, |
| 105 | + allowItemReorder: this.allowItemReorder, |
| 106 | + allowItemRemoval: this.allowItemRemoval, |
| 107 | + }, |
| 108 | + }, |
| 109 | + missions: { |
| 110 | + ...missions, |
| 111 | + lime: { |
| 112 | + ...missionOptions, |
| 113 | + allowItemReorder: this.allowItemReorder, |
| 114 | + allowItemRemoval: this.allowItemRemoval, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }; |
| 119 | + } |
| 120 | +} |
0 commit comments