I've created a redux-form and i want to add className to each Field to customize them with css.
The code for each field is:
<Form onSubmit={handleSubmit(requestAccountsFilter)}>
<FormGroup row>
<Field
id="symbol"
name="symbol"
type="text"
component={inputField}
placeholder="Enter Product Here"
/>
<Field id="side" name="side" component={inputField} type="select">
<option value={null}>Any</option>
<option value="Buy">Buy</option>
<option value="Sell">Sell</option>
</Field>
<Field id="status" name="status" component={inputField} type="select">
<option value={null}>Any</option>
<option value="Working">Working</option>
<option value="Completed">Completed</option>
</Field>
<Button name="submit-btn" className="filter-submit-btn" color="danger" type="submit">
Submit
</Button>
</FormGroup>
</Form>
I've added a className tag but i see that neither the placeholder i've added is shown nor the className. How can i customize each field?
<Field
type="text"
className="myClass"
component={InputField}
placeholder="Type here..."
/>
and your custom InputField should be something like
(I've taken this example from http://redux-form.com/6.5.0/examples/submitValidation/)
export const InputField = ({ input, type, placeholder, className, meta: { touched, error } }) => (
<div>
<input {...input} placeholder={placeholder} type={type} className={className}/>
{meta.touched && meta.error && <span>{meta.error}</span>}
</div>
)
or a better approach, if too many props are there, You can use object destructuring
export const InputField = (field) => (
<div>
<input {...field.input} {...field} />
{field.meta.touched && field.meta.error && <span className="error">{field.meta.error}</span>}
</div>
)
{...field} will extract all props that you have passed in Field.
You can take a look at this official redux-form example: http://redux-form.com/6.5.0/examples/react-widgets/ to get more idea.
Hope it helps :)
You can use object destructuring method to set className.
<Field
type="text"
input={{className:'red'}}
component={InputField}
placeholder="Type here..."
/>
I realize that you are using a custom renderer by saying component={InputField}, but for others coming here (since I can't find it in the docs): if you are using one of the built-in renderers like component="input" or component="select", you can just add className and the renderer will apply it, e.g.:
<Field name="foo" component="select" className="form-control">
</Field>
By definition, whatever you pass to the Field component should be passed to the InputField component as input prop. So, your InputField Component should look something like this:
<div>
<InputField {...field.input} > {field.children} </InputField>
{field.meta.touched && field.meta.error && <span className="error">
{field.meta.error}</span>}
</div>
Related
I am trying to implement inline css on an input element in a react project. The style attribute that I added in the jsx is not rendering. I have checked the App.css and index.css files and there is nothing that would override this. Does anyone know why this isn't working? Any help would be appreciated.
Edit: This has been solved, after reading the comments from jnpdx, Shivam, and Sayog, I was able to edit the FormInput file and it's working now. Thanks everyone for your help!!
return (
<div id="recipe">
<form onSubmit={handleSubmit}>
<FormInput style={{width: "250px"}} type="text" name="Name" handleChange={handleChange} />
<FormInput style={{width: "250px"}} type="number" name="time" display="Prep Time (in minutes)" handleChange={handleChange} />
<input type="submit" value="Create Recipe" />
</form>
</div>
)
I have a form which needs its css Display to be set to block when I click on a certain button. When I click on the Add button id="add" it should set the css style block for id="modalContent" div.
I've just started react and am completely new to it. I read something about ref but couldn't completely understand how to go through with it.
AddFormMod.tsx
import React from 'react';
import './App.css';
function AddForm(){
return (
<div id="modalContent" className="modal-content">
<h1 id="headerAdd">ADD NEW CONTACT</h1>
<form action="#" id="myForm">
<label className="required label" ><b>Name: </b></label><br />
<input className="form-fields type1" type="text" id="name" name="name" required></input><br/><br/>
<label className="required label" ><b>Email:</b> </label><br/>
<input className="form-fields type1 " type="email" id="email" name="mail" required></input><br/><br/>
<label className="required label" ><b>Mobile:</b> </label>
<label className="label" id="landlinelabel" ><b>Landline:</b></label><br/>
<input className="form-fields" type="tel" id="mobile" name="mobile" pattern="^\d{10}$" required></input>
<input className="form-fields" type="tel" id="landline" name="landline" ></input><br/><br/>
<label className="label" ><b>Website</b></label><br/>
<input className="form-fields type1" type="text" id="website" name="website" ></input><br/><br/>
<label className="label"><b>Address:</b> </label><br/>
<textarea className="addressstyle form-fields" id="address1" name="address1" rows={9} cols={74}></textarea>
<input className = "buttonstyle" type="submit" value="Add" id="adddetails" ></input>
<input className = "buttonstyle" type="button" value="Cancel" id="candetails"></input>
</form>
</div>
);
}
export default AddForm;
App.tsx
import React from 'react';
import './App.css';
import AddForm from './AddFormMod';
function App() {
return (
<p id="title"> Address Book </p>
);
}
function AddHome(){
return (
<div>
<button id="home" >HOME</button>
<button id="add" onClick={} >+ADD</button>
</div>
);
}
function ContactBar(){
return (
<div>
<p id="contacts">CONTACTS</p>
</div>
);
}
export { App , AddHome, ContactBar };
One approach to achieve the result you want, is to utilize conditional rendering. For example, when you click the "add"-button in your AddHome component, you can set a state variable to render the AddForm-component:
function AddHome(){
const [shouldRenderForm, setShouldRenderForm] = useState(false);
return (
<div>
<button id="home" >HOME</button>
<button id="add" onClick={() => setShouldRenderForm(true)} >+ADD</button>
{shouldRenderForm && <AddForm />}
</div>
);
}
I'm also guessing you want to "close the form" after submit or via a close button inside the AddForm-component. To achieve this, simply pass a prop to the component, for the AddForm-component to call to close the form:
// ... in the add AddHome component:
{shouldRenderForm && <AddForm closeForm={() => setShouldRenderForm(false)} />}
// ... in AddForm component:
type AddFormProps = { closeForm: () => void };
function AddForm({ closeForm }: AddFormProps) {
// ... i.e. on a button to close the form
<button type="button" onClick={() => closeForm()}>Cancel</button>
}
Checkout a working example in this sandbox.
I have these inputs in my twig file :
<input type="text" name="txtNom" id="txtNom" value="{{user.nom}}" />
<input type="text" name="txtPrenom" id="txtPrenom" value="{{user.prenom}}" />
<input type="radio" name="rbSexe" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe" id="rbFemme" onclick="changeGender(this.id);" />
So, for calling those inputs in my Controller, I use the name attribute, for the first two it's okay :
$utilisateur->setNom($request->get('txtNom'));
$utilisateur->setPrenom($request->get('txtPrenom'));
but those with radio type have the same name, so how can I call specific one of them?
$utilisateur->setSexe(?????????);
I solved the problem :
I give the inputs a value, and make the name looks like an array:
<input type="radio" name="rbSexe[]" value="Homme" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe[]" value="Femme" id="rbFemme" onclick="changeGender(this.id);" />
and for call it in Controller, I use this :
$s = $request->get('rbSexe',0)[0];
I'm designing dynamic forms for grails and I was wondering if it was at possible to set the widget constraint dynamically?
Setting the widget constraint is not the way to attack this. I suppose the simplest method would be to choose the tag you'd like to display based upon some condition using an "if" tag in your view or template. For instance:
Say you have a Book
class Book {
String name
}
If you use Grails generate-view for this class, Grails will produce a template called _form that looks like the following:
<div class="fieldcontain ${hasErrors(bean: bookInstance, field: 'name', 'error')} ">
<label for="name">
<g:message code="book.name.label" default="Name" />
</label>
<g:textField name="name" value="${bookInstance?.name}"/>
</div>
You can test your condition in this form and change the view:
<div class="fieldcontain ${hasErrors(bean: bookInstance, field: 'name', 'error')} ">
<label for="name">
<g:message code="book.name.label" default="Name" />
</label>
<g:if test="$yourCondition">
<g:textField name="name" value="${bookInstance?.name}"/>
</g:if>
<g:else>
<g:textArea name="name" value="${bookInstance?.name}"/>
</g:else>
</div>
Now the view will display a textField or textArea field based upon $yourCondition.
I would like to display a form that lets a user enter up to 10 rows of information. If they need to go over, I'm going to use an "add additional row" button that will add one row at a time. What will my Model class look like for something like this? When I use javascript to add a new row, how can I tie that new row into the Model as well?
This article from Phil Haack shows you how to bind to collections. You'll need to use the javascript to create the new row with the correct names.
Probably this rows contains related values, so you can give the same name to all theses inputs in the html, and declare that you action receive an array of values.
Assume that you have this
<form method="post" action="/Controller/YourAction">
<input type="text" name="row" value="1" />
<input type="text" name="row" value="2" />
<input type="text" name="row" value="3" />
<input type="text" name="row" value="4" />
<input type="text" name="row" value="5" />
<input type="text" name="row" value="6" />
<input type="submit" />
</form>
all that you need to do is declare this inside your Controller
public ActionResult YourAction(int[] row)
{
//put your code here
}
and you will have all the values inside the row array
You may take a look at the following blog post which explains how to achieve exactly that. It uses a custom helper (Html.BeginCollectionItem) which allows to use non sequential as collection indexes instead of numbers which makes adding/deleting new items much easier.