Skip to content

Commit 747f906

Browse files
committed
feat(form): add help to sections
Before, it was possible to add `help` only to form fields. But now consumers can add `help` to section headers, and collapsible sections in forms.
1 parent f039acc commit 747f906

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

src/components/form/examples/help-form-schema.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export interface HelpFormData {
88
email?: string;
99
source?: Source;
1010
};
11+
additionalInfo?: {
12+
phone?: string;
13+
jobTitle?: string;
14+
teamSize?: number;
15+
};
1116
}
1217

1318
export const schema: FormSchema<HelpFormData> = {
@@ -16,6 +21,11 @@ export const schema: FormSchema<HelpFormData> = {
1621
address: {
1722
type: 'object',
1823
title: 'Book a demo',
24+
lime: {
25+
help: {
26+
value: 'Fill in your details below and we will get back to you within **24 hours**.',
27+
},
28+
},
1929
properties: {
2030
name: {
2131
type: 'string',
@@ -92,5 +102,45 @@ export const schema: FormSchema<HelpFormData> = {
92102
},
93103
},
94104
},
105+
additionalInfo: {
106+
type: 'object',
107+
title: 'Additional Information',
108+
description:
109+
'Optional details that help us _tailor_ our demo to your needs',
110+
properties: {
111+
phone: {
112+
type: 'string',
113+
title: 'Phone Number',
114+
description: 'Your direct phone number',
115+
lime: {
116+
help: {
117+
value: 'We may call you to confirm the demo appointment.',
118+
},
119+
},
120+
},
121+
jobTitle: {
122+
type: 'string',
123+
title: 'Job Title',
124+
description: 'Your current role or position',
125+
},
126+
teamSize: {
127+
type: 'integer',
128+
title: 'Team Size',
129+
description: 'How many people are in your team?',
130+
lime: {
131+
help: {
132+
value: 'This helps us understand the scale of your organization and recommend the right plan.',
133+
},
134+
},
135+
},
136+
},
137+
lime: {
138+
collapsible: true,
139+
collapsed: false,
140+
help: {
141+
value: 'These optional fields allow us to **personalize** your demo experience.',
142+
},
143+
},
144+
},
95145
},
96146
};

src/components/form/help/help.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@ import { FormSchema } from '../form.types';
44
/**
55
*
66
* @param schema
7+
* @param extraProps - additional props to pass to the limel-help element
78
*/
8-
export function getHelpComponent(schema: FormSchema) {
9+
export function getHelpComponent(
10+
schema: FormSchema,
11+
extraProps?: Record<string, unknown>
12+
) {
913
const help = schema.lime?.help;
1014

1115
if (!help) {
1216
return;
1317
}
1418

1519
if (typeof help === 'string') {
16-
return React.createElement('limel-help', { value: help });
20+
return React.createElement('limel-help', {
21+
value: help,
22+
...extraProps,
23+
});
1724
}
1825

1926
const helpProps = help;
2027

21-
return React.createElement('limel-help', helpProps);
28+
return React.createElement('limel-help', { ...helpProps, ...extraProps });
2229
}

src/components/form/templates/object-field.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import {
33
FormLayoutType,
44
LimeLayoutOptions,
55
FormLayoutOptions,
6+
FormSchema,
67
} from '../form.types';
78
import { renderDescription, renderTitle } from './common';
89
import { GridLayout } from './grid-layout';
910
import { RowLayout } from './row-layout';
1011
import { LimeObjectFieldTemplateProps, ObjectFieldProperty } from './types';
1112
import { JSONSchema7 } from 'json-schema';
13+
import { getHelpComponent } from '../help';
1214

1315
export const ObjectFieldTemplate = (props: LimeObjectFieldTemplateProps) => {
1416
const id = props.idSchema.$id;
@@ -27,14 +29,31 @@ function renderFieldWithTitle(props: LimeObjectFieldTemplateProps) {
2729
return React.createElement(
2830
React.Fragment,
2931
{},
30-
renderTitle(props.title),
32+
renderSectionHeader(props),
3133
renderDescription(props.description),
3234
renderProperties(props.properties, props.schema)
3335
);
3436
}
3537

38+
function renderSectionHeader(props: LimeObjectFieldTemplateProps) {
39+
const help = getHelpComponent(props.schema as FormSchema);
40+
if (!help) {
41+
return renderTitle(props.title);
42+
}
43+
44+
return React.createElement(
45+
React.Fragment,
46+
{},
47+
renderTitle(props.title),
48+
help
49+
);
50+
}
51+
3652
function renderCollapsibleField(props: LimeObjectFieldTemplateProps) {
3753
const defaultOpen = !isCollapsed(props.schema);
54+
const helpElement = getHelpComponent(props.schema as FormSchema, {
55+
slot: 'header',
56+
});
3857

3958
return React.createElement(
4059
'limel-collapsible-section',
@@ -46,6 +65,7 @@ function renderCollapsibleField(props: LimeObjectFieldTemplateProps) {
4665
),
4766
'is-open': defaultOpen,
4867
},
68+
helpElement,
4969
renderDescription(props.description),
5070
renderProperties(props.properties, props.schema)
5171
);

0 commit comments

Comments
 (0)