-
Notifications
You must be signed in to change notification settings - Fork 123
MIAW custom pre-chat example #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
0264edc
afd603b
dd9ccb8
7c83a71
8e68745
f47e6ae
3f4f188
535e235
dd3752c
5118b65
b09323f
fbffe0e
ad1befd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| :host { | ||
| display: flex; | ||
| flex-direction: column; | ||
| flex: 1 1 auto; | ||
| overflow: hidden; | ||
| background: #FFFFFF; | ||
| padding: 2em; | ||
| } | ||
|
|
||
| lightning-button { | ||
| padding-top: 2em; | ||
| text-align: center; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <template> | ||
| <template class="slds-m-around_medium" for:each={fields} for:item="field"> | ||
| <c-custom-prechat-form-field key={field.name} | ||
|
||
| field-info={field}> | ||
| </c-custom-prechat-form-field> | ||
| </template> | ||
| <lightning-button label={startConversationLabel} | ||
| title={startConversationLabel} | ||
| onclick={handleStartConversation} | ||
| class="slds-m-left_x-small"> | ||
| </lightning-button> | ||
| </template> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { track, api, LightningElement } from "lwc"; | ||
|
|
||
| export default class CustomPrechatForm extends LightningElement { | ||
| @api configuration = {}; | ||
|
|
||
| startConversationLabel; | ||
|
|
||
| get prechatForm() { | ||
| const forms = this.configuration.forms || []; | ||
| return forms.find(form => form.formType === "PreChat") || {}; | ||
| } | ||
|
|
||
| get prechatFormFields() { | ||
| return this.prechatForm.formFields || []; | ||
| } | ||
|
|
||
| get fields() { | ||
| let fields = JSON.parse(JSON.stringify(this.prechatFormFields)); | ||
| this.addChoiceListValues(fields); | ||
| return fields.sort((fieldA, fieldB) => fieldA.order - fieldB.order); | ||
| } | ||
|
|
||
| connectedCallback() { | ||
| this.startConversationLabel = "Start Conversation"; | ||
| } | ||
|
|
||
| addChoiceListValues(fields) { | ||
| for (let field of fields) { | ||
| if (field.type === "ChoiceList") { | ||
| const valueList = this.configuration.choiceListConfig.choiceList.find(list => list.choiceListId === field.choiceListId) || {}; | ||
| field.choiceListValues = valueList.choiceListValues || []; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| isValid() { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking of renaming this to |
||
| let isFormValid = true; | ||
| this.template.querySelectorAll("c-custom-prechat-form-field").forEach(formField => { | ||
| if (!formField.reportValidity()) { | ||
| isFormValid = false; | ||
| } | ||
| }); | ||
| return isFormValid; | ||
| } | ||
|
|
||
| handleStartConversation() { | ||
| const prechatData = {}; | ||
| if (this.isValid()) { | ||
| this.template.querySelectorAll("c-custom-prechat-form-field").forEach(formField => { | ||
| prechatData[formField.name] = String(formField.value); | ||
| }); | ||
|
|
||
| this.dispatchEvent(new CustomEvent( | ||
| "prechatsubmit", | ||
| { | ||
| detail: { value: prechatData } | ||
| } | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>55.0</apiVersion> | ||
| <isExposed>true</isExposed> | ||
| <targets> | ||
| <target>lightningSnapin__MessagingPreChat</target> | ||
| </targets> | ||
| </LightningComponentBundle> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <template> | ||
| <template lwc:if={isTypeChoiceList}> | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ChoiceLists can be rendered using |
||
| <lightning-combobox key={fieldInfo.name} | ||
| label={fieldInfo.labels.display} | ||
| options={choiceListOptions} | ||
| value={choiceListDefaultValue} | ||
| required={fieldInfo.required}> | ||
| </lightning-combobox> | ||
| </template> | ||
| <template lwc:else> | ||
| <lightning-input key={fieldInfo.name} | ||
| type={type} | ||
| label={fieldInfo.labels.display} | ||
| max-length={fieldInfo.maxLength} | ||
| required={fieldInfo.required}> | ||
| </lightning-input> | ||
| </template> | ||
| </template> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { track, api, LightningElement } from "lwc"; | ||
|
|
||
| export default class CustomPrechatFormField extends LightningElement { | ||
| choiceListDefaultValue; | ||
|
|
||
| @api fieldInfo = {}; | ||
|
|
||
| @api | ||
| get name() { | ||
| return this.fieldInfo.name; | ||
| } | ||
|
|
||
| @api | ||
| get value() { | ||
| const lightningCmp = this.isTypeChoiceList ? this.template.querySelector("lightning-combobox") : this.template.querySelector("lightning-input"); | ||
| return this.isTypeCheckbox ? lightningCmp.checked : lightningCmp.value; | ||
| } | ||
|
|
||
| @api | ||
| reportValidity() { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. required for validating each formField on Start Conversation click. A customer can choose their own way of validating the form but I just thought we can show them an example. |
||
| const lightningCmp = this.isTypeChoiceList ? this.template.querySelector("lightning-combobox") : this.template.querySelector("lightning-input"); | ||
| return lightningCmp.reportValidity(); | ||
| } | ||
|
|
||
| get type() { | ||
| switch (this.fieldInfo.type) { | ||
| case "Phone": | ||
| return "tel"; | ||
| case "Text": | ||
| case "Email": | ||
| case "Number": | ||
| case "Checkbox": | ||
| case "ChoiceList": | ||
| return this.fieldInfo.type.toLowerCase(); | ||
| default: | ||
| return "text"; | ||
| } | ||
| } | ||
|
|
||
| get isTypeCheckbox() { | ||
| return this.type === "Checkbox".toLowerCase(); | ||
| } | ||
|
|
||
| get isTypeChoiceList() { | ||
| return this.type === "ChoiceList".toLowerCase(); | ||
| } | ||
|
|
||
| get choiceListOptions() { | ||
| let choiceListOptions = []; | ||
| const choiceListValues = [...this.fieldInfo.choiceListValues]; | ||
| choiceListValues.sort((valueA, valueB) => valueA.order - valueB.order); | ||
| for (const listValue of choiceListValues) { | ||
| if (listValue.isDefaultValue) { | ||
| this.choiceListDefaultValue = listValue.choiceListValueName; | ||
| } | ||
| choiceListOptions.push({ label: listValue.label, value: listValue.choiceListValueName }); | ||
| } | ||
| return choiceListOptions; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>55.0</apiVersion> | ||
| <isExposed>true</isExposed> | ||
| <targets> | ||
| </targets> | ||
| </LightningComponentBundle> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just some basic styling here. planning to just add a note about
configuration.brandingso that customers can consume the branding information from config as they seem fit.