Does anyone know the way to pass value from LWC to Flow variable?
When clicking "Add Name" button, I want input value to be passed to Flow valuable. "Manually assign variables (advanced)" is valid on the screen setting. But the variable is null and never changes.
xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>50.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__FlowScreen</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__FlowScreen">
<property name="sampleName" type="String" label="sampleName"/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
js
import { LightningElement,api } from 'lwc';
import { FlowAttributeChangeEvent, FlowNavigationNextEvent } from 'lightning/flowSupport';
export default class sampleFlowComponent extends LightningElement {
#api
availableActions = [];
#api
sampleName;
handleAttributeChange() {
const attributeChangeEvent = new FlowAttributeChangeEvent('sampleName', this.sampleName);
this.dispatchEvent(attributeChangeEvent);
}
handleNext() {
if (this.availableActions.find(action => action === 'NEXT')) {
const navigateNextEvent = new FlowNavigationNextEvent();
this.dispatchEvent(navigateNextEvent);
}
}
}
html
<template>
<lightning-input
label="sampleName"
type="text">
</lightning-input>
<lightning-button
label="Add Name"
title="Add Name"
onclick={handleAttributeChange}>
</lightning-button>
<lightning-button
label="Go Next"
title="Go next"
onclick={handleNext}>
</lightning-button>
</template>
Any information would be helpful!
Add an onchange handler to the input to call:
onchange(event){
this.sampleName= event.target.value;
}
No need of a specific button.
Related
I am using Formspree to make a simple Contact form in my NextJS website. Formspree provides the following sample code snippet for React:
// Make sure to run npm install #formspree/react
// For more help visit https://formspr.ee/react-help
import React from 'react';
import { useForm, ValidationError } from '#formspree/react';
function ContactForm() {
const [state, handleSubmit] = useForm("YOUR_FORMSPREE_UNIQUE_KEY");
if (state.succeeded) {
return <p>Thanks for joining!</p>;
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="email">
Email Address
</label>
<input
id="email"
type="email"
name="email"
/>
<ValidationError
prefix="Email"
field="email"
errors={state.errors}
/>
<textarea
id="message"
name="message"
/>
<ValidationError
prefix="Message"
field="message"
errors={state.errors}
/>
<button type="submit" disabled={state.submitting}>
Submit
</button>
</form>
);
}
function App() {
return (
<ContactForm />
);
}
export default App;
Trouble is, if a user submits the form with empty fields, it still gets submitted. I searched the web, looked up some formspree implementations on YT and checked their website. But didn't find a solution on how to prevent a user from submitting with empty fields.
I implemented the simplest solution I could think of:
I modified the onSubmit of the form in the following way, adding a switch statement:
onSubmit = { (email.length>0 && message.length > 0) ? handleSubmit : errorAlert}
Added errorAlert function as follows:
function errorAlert() {
alert("cannot leave any field blank.")
}
This way, using a switch statement one can add whatever conditions and failsafes one wishes to add to the form submission text fields.
Try this method:
import { useForm } from "#formspree/react";
function ContactForm() {
const [state, handleSubmit] = useForm("your-form-id");
const manageSubmit = (e) => {
// other logic or error checking logic here
handleSubmit(e); // call the handleSubmit function with event (e)
}
if (state.succeeded) {
return <p>Thanks for joining!</p>;
}
return <form onSubmit={manageSubmit}></form>
}
I am working on a React project where in one of the components I am passing properties to another component through react-router-dom Link like so:
let updateEmpInfo;
...
<Button component={Link}
halfWidth
variant="contained"
color="primary"
preventDefault
onClick={getEmpInfo}
to={{pathname: "/update",
state:
{id: updateEmpInfo.id,
name: updateEmpInfo.name,
department: updateEmpInfo.department,
gender: updateEmpInfo.gender}
}}>Update information
</Button>
My questin is specifically about the "state" property. As you can see, I am passing several state parameters (id, name, department and gender). Since Link component is inside the render method, it requires the updateEmpInfo variable to be defined somewhere in the render method. I am trying to take input from user and based on their input set the value of all updateEmpInfo properties, after the component has rendered. All of them will be passed to the state property of Link. I am trying to do so in the getEmpInfo function. However, regardless of the input, the state property preserves all initial values that were set during the render. Is there a way to change updateEmpInfo properties based on the user input once the Link is clicked? I my question is clear enough. I will be happy to provide any additional info. Thank you in advance!
If I understood you correctly, the values passed are not how the user adjusted them.
I'd suppose it's an onChange-Listener missing to the InputFields or an onSubmit to the whole form respectively.
import { Component } from 'react';
import { withRouter } from 'react-router-dom';
class MyEmployeeForm extends Component {
constructor(props) {
super(props);
this.state = {
id: "",
name: "",
department: "",
gender: ""
};
}
onChange = (event) => {
this.setState({[event.target.name]: event.target.value});
}
onUpdate = (event) => {
event.preventDefault();
this.props.history.push({
pathname: '/update',
state: this.state});
}
render() {
<form>
<input type="text" name="id" placeholder="id" value={this.state.id} onChange={this.onChange} />
<input type="text" name="name" placeholder="name" value={this.state.name} onChange={this.onChange}/>
<input type="text" name="department" placeholder="department" value={this.state.department} onChange={this.onChange}/>
<input type="text" name="gender" placeholder="gender" value={this.state.gender} onChange={this.onChange}/>
</form>);
<Button component={Link}
halfWidth
variant="contained"
color="primary"
onClick={this.onUpdate}>
Update information
</Button>
}
}
export default withRouter(MyEmployeeForm);
see here Get form data in ReactJS ,
How to pass params with history.push/Link/Redirect in react-router v4?
I have built a simple component to display the contact information. But, its not showing in the page. no clue what is going on ?
I have checked apex controller is returning contact correctly. But, the component is not rendering with the received contact object.
<aura:application >
<aura:attribute name="contactId" type="String"/>
<c:PreferenceComponent contactId="{!v.contactId}"/>
</aura:application>
<aura:component controller="PreferenceComponentCtrlr">
<aura:attribute name="contactId" type="String"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<lightning:card variant="Narrow" title="{!v.contact.Name}"
iconName="standard:contact">
<p class="slds-p-horizontal_small">
{!v.contact.Phone}
</p>
<p class="slds-p-horizontal_small">
{!v.contact.MailingStreet}
</p>
</lightning:card>
</aura:component>
({
doInit : function(component, event, helper) {
var action = component.get("c.getContact");
action.setParams({
contactId : component.get("v.contactId")
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === 'SUCCESS'){
component.set("v.contact", response.getReturnValue());
}
});
$A.enqueueAction(action);
}
})
public class PreferenceComponentCtrlr {
#AuraEnabled
public static Contact getContact(Id contactId) {
System.debug('contactId - ' + contactId);
return [Select Id, Name, Phone, MailingStreet From Contact Where Id =: contactId LIMIT 1];
}
}
I found the answer, i need to add Contact attribute to the component,
<aura:attribute name="contact" type="Contact"/>
I'm relatively new to Redux and I have a form that has some radio inputs "Yes" or "No". Basically, I want to conditionally show another element which contains another redux form field, based on that radio input selection. Is there a straight forward to do this?
I'm trying to just check the formProps.site_visit value, but I get an error about it being undefined. For the record, I've greatly reduced the amount of code in this component for brevity sake.
export class RequestForm extends React.Component {
submit(formProps) {
const request = {
square_footage: formProps.get('square_footage'),
site_visit: formProps.get('site_visit'),
};
this.props.dispatch(createRequest(request));
}
// Redux Form Props.
const { handleSubmit, pristine, reset, submitting } = this.props
return (
<form className="page-form__wrapper">
<div className="page-form__block">
<div className="page-form__block">
<p> Is a site visit required to complete this request? </p>
<Field name="site_visit"
component={RadioButtonGroup}
>
<RadioButton value="true" label="Yes" />
<RadioButton value="false" label="No" />
</Field>
</div>
{this.formProps.site_visit === true &&
<div className="page-form__block">
<p> Estimate the total area of work in square feet </p>
<Field name="square_footage" component={TextField} hintText="Square Feet" />
</div>
}
</div>
</form>
);
}
Thanks in advance!
You'll need to use a formValueSelector
const selector = formValueSelector('formName');
function mapStateToProps(state, props) {
const isChecked = selector(state, 'checkboxField');
return { isChecked };
}
connect using mapStateToProps
the render method will look like this.
render() {
return (
{ this.props.isChecked && (
<div>
this only shows up if the checkboxField Field is checked
</div>
) }
);
}
edit:
looks like you're using reselect - I've never used createStructuredSelector and I don't 100% understand the documentation, but a possible solution might be:
const mMapStateToProps = createStructuredSelector({
request: selectRequestForm(),
user: selectUser()
});
const mapStateToProps = (state, props) => {
return {
isChecked: selector(state, 'checkboxField'),
... mMapStateToProps(state, props) // not sure if createStructuredSelector accepts a props param
}
};
that'll compose the two. I think you could also use createSelector with mMapStateToProps and the mapStateToProps I originally posted...
I am trying to access the touched property on my redux form, but for some reason when I print the field props I only the value instead of the object. What am I missing?
import { reduxForm, Field } from 'redux-form';
render() {
const { fields: { email, phone }, handleSubmit } = this.props;
console.log(email) //prints just the value "email" instead of the field object with the touched method, etc. When I do console.log(email.touched) I get undefined error.
return (
<form onSubmit={handleSubmit(this.onSubmit)}>
<Field name="email" component="input" type="email" { ...email } />
<Field name="phone" component="input" type="number" { ...phone } />
</form>
);
}
export default ReduxFormTest = reduxForm({
form: 'uniqueForm',
fields: ['email', 'phone']
})(TestClass);
There were breaking changes in redux-forms from v5 to v6. Previously you could do something similar to what you have to access the touched field. If you want to do something similar to see if there are errors on a field, you need to create your own component to pass to redux-form's Field component.
Your custom component
const CustomComponent = function(field) {
return (
<div>
<input
type={field.type}
{...field.input}
/>
{field.meta.touched && field.meta.error && <div className='error'>{field.meta.error}</div>}
</div>
)
}
Then using it with the Field component
<Field name="my-prop" component={CustomComponent} />
Also take a look at the migration guide, hope this helps!
You are confusing v5 syntax with v6 syntax. In v6, your decorated form component is no longer passed this.props.fields. Re-read the migration guide, like #tyler-iguchi said.