Skip to content

Commit 8938b9e

Browse files
committed
docs(form): show how to allow end users to remove or reorder array items
1 parent 19545c8 commit 8938b9e

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { FormSchema } from '@limetech/lime-elements';
2+
3+
export interface MissionPlan {
4+
name?: string;
5+
objective?: string;
6+
active?: boolean;
7+
}
8+
9+
export interface ArrayItemControlsFormData {
10+
tasks?: string[];
11+
missions?: MissionPlan[];
12+
}
13+
14+
export const baseSchema: FormSchema<ArrayItemControlsFormData> = {
15+
type: 'object',
16+
properties: {
17+
tasks: {
18+
type: 'array',
19+
title: 'Weekly tasks',
20+
items: {
21+
type: 'string',
22+
title: 'Task',
23+
},
24+
lime: {},
25+
},
26+
missions: {
27+
type: 'array',
28+
title: 'Mission plans',
29+
description: 'Object items render inside collapsible sections.',
30+
items: {
31+
type: 'object',
32+
title: 'Mission',
33+
properties: {
34+
name: {
35+
type: 'string',
36+
title: 'Name',
37+
},
38+
objective: {
39+
type: 'string',
40+
title: 'Objective',
41+
},
42+
active: {
43+
type: 'boolean',
44+
title: 'Active',
45+
default: true,
46+
},
47+
},
48+
},
49+
lime: {},
50+
},
51+
},
52+
};

src/components/form/form.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { mapValues } from 'lodash-es';
4242
* @exampleComponent limel-example-form-span-fields
4343
* @exampleComponent limel-example-custom-error-message
4444
* @exampleComponent limel-example-server-errors
45+
* @exampleComponent limel-example-form-array-item-controls
4546
* @exampleComponent limel-example-form-with-help
4647
* @exampleComponent limel-example-form-row-layout
4748
*/

0 commit comments

Comments
 (0)