react multiple modal use import for file - reactjs

I have a grid with 4 buttons, it buttons open different modal
how I separate modal from different file and import
ex: modal client, modal followup, modal calendar
In my grid button, I call different modal
want in a different file because I can use in different grid
grid button
import Client from './clientModal';
import Followup from './followup';
import Calendar from './calendar';

modal form
import {
Button,
ModalBody,
ModalFooter,
ModalHeader,
Col,
Form,
FormGroup,
Input,
Label
} from "reactstrap";
const ModalClients = props => {
this.props = {
modalClient: false,
firstName: null
};
return (
<Form
method="POST"
onChange={this.handleChange}
onSubmit={this.onCreate}
encType="multipart/form-data"
>
<ModalHeader toggleModal={this.toggleClient} className="blue-header">
<i className="cui-people"></i>Clients
</ModalHeader>
<ModalBody>
<FormGroup row className="my-0">
<Col xs="8">
<FormGroup>
<Label htmlFor="firstName">First Name</Label>
<Input
onChange={this.handleChange}
name="firstName"
type="text"
id="firstName"
value={this.props.firstName || ""}
placeholder="Enter First Name"
/>
</FormGroup>
</ModalBody>
<ModalFooter>
<Button color="primary" type="submit" onClick={this.toggleClient}>
<i className="cis-check-double-alt"></i> Save
</Button>
<Button
color="secondary"
onClick={() => {
this.toggleClient();
this.resetForm();
}}
>
Cancel
</Button>
</ModalFooter>
</Form>
);
};
export default ModalClients;
file grid with buttons
import ClientModal from './clientModal';
<Modal
isOpen={this.state.info}
toggle={this.toggleInfo}
backdrop="static"
keyboard={false}
className={"modal-lg " + this.props.className}
>
<ClientModal />
</Modal>
buttons
<Row>
<Button className="btn btn-primary" onClick={() => this.toggleInfo(id)}>
<i className="cis-comment-bubble-edit"></i>
</Button>
<Button
className="btn btn-success"
onClick={() => this.toggleClient(id)}
>
</Row>

Related

How to hide a Button, Only if Value is there in Input tag in React

I am working on a React project, In my project I have one button If I click that button one
model is appearing. In that Model I have two Input tags and three buttons.
Here comes the login, In UI If Input tags have a Value then I have to hide Blue button in UI.
And If In UI If Input tags have no Value then I have to hide Red Buuton in UI this time I have
to show Blue button.
This is App.js
import React, { useState } from 'react';
import './App.css';
import Form from './Form/Form';
function App() {
const [show, setShow] = useState(false)
return (
<div className="App">
<button className='btn btn-danger'
onClick={() => setShow(true)}>Click here</button>
{ show && <Form></Form>}
</div>
);
}
export default App;
This is Form.js
import React, { useState } from 'react';
import './Form.css';
import {
Row,
Col,
Button,
ButtonGroup,
Card,
CardHeader,
CardSubtitle,
CardBody,
CardText,
Container,
Modal,
ModalBody,
ModalFooter,
ModalHeader,
FormGroup,
Label,
Input,
UncontrolledButtonDropdown,
DropdownToggle,
DropdownMenu,
DropdownItem
} from 'reactstrap';
const Form = () => {
const fun = () => {
}
return (
<div>
<Row>
<Col md="6" sm="6" xs="6">
<Modal
isOpen>
<ModalHeader >Add Role</ModalHeader>
<ModalBody>
<FormGroup>
<Label for="exampleName">Name</Label>
<Input
type="text"
name="name"
placeholder="Enter Your Name"
value='Tom'
/>
</FormGroup>
<FormGroup>
<Label for="exampleEmail">Description</Label>
<Input
type="textarea"
name="description"
placeholder="Enter Description"
value='React developer'
/>
</FormGroup>
</ModalBody>
<ModalFooter>
<Button color="secondary">
Cancel
</Button>
<Button className='one' color="primary">
Blue
</Button>
<Button color='danger'>Red</Button>
</ModalFooter>
</Modal>
</Col>
</Row>
</div>
)
}
export default Form
If you feel I am not clear with my doubt please put a comment.
you can check truthy value. If input has value, show 1 button and hide the other
const Form = () => {
const [value1, setValue1] = useState('');
const [value2, setValue2] = useState('');
return (
<div>
<Input
type="text"
name="name"
placeholder="Enter Your Name"
value={value1}
onChange={e => setValue1(e.target.value)}
/>
<Input
type="text"
name="value2"
placeholder="Enter Your Email"
value={value2}
onChange={e => setValue2(e.target.value)}
/>
{(!value1 || !value2) && <Button>Blue</Button>} // Show button if there is some value
{(value1 && value2) && <Button>Rad</Button>} // Show button if field is empty
</div>
);
}

How to write condition to Input tag in React

I am working on React project, In that project I have a scenario that is I have to write
Condition for Input tag. The Condition wants to be like this, In my form the Input tag type is
Number, and its min Value is 1001 and max value is 1500. So now what I want is If I type number
Less than 1001 then it should not take that number in Input tag. Someone please help me how to
Write logic like this.
This is Form.js
import React from 'react';
import './aum-company-modal.css';
import { Button, Col, Modal, ModalBody, ModalFooter, ModalHeader, Row, FormGroup, Label, Input, } from 'reactstrap';
const AumCompanyModal = () => {
return (
<Row>
<Col md="6" sm="6" xs="6">
<Modal isOpen
>
<ModalHeader >Add new</ModalHeader>
<ModalBody>
<Row>
<Col md="12" sm="12" xs="12">
<FormGroup>
<Label for="exampleName">Min Value</Label>
<Input
type="text"
name="roleName"
placeholder="Enter minimum value"
value='1000'
/>
</FormGroup>
<FormGroup>
<Label for="exampleName">Max Value</Label>
<Input
type="number"
name="roleName"
placeholder="Enter maximum value"
min='1001' max='1500'
/>
</FormGroup>
</Col>
</Row>
</ModalBody>
<ModalFooter>
<Button color="secondary">
Cancel
</Button>
<Button type="submit" color="primary">
Submit
</Button>
</ModalFooter>
</Modal>
</Col>
</Row>
)
}
export default AumCompanyModal
You input does not have a value property so I would suggest you to make an state to use it as value and the set an onChange which is a function that checks that just like this:
const [ inputValue, setInputValue ] = useState(1000);
handleChange = (e) => {
if (e.target.value > 999 && e.target.value < 1501) {
setInputValue(e.target.value);
}
}
<Input
type="number"
name="roleName"
placeholder="Enter maximum value"
min='1001' max='1500'
onChange={handleChange}
value={inputValue}
/>

How to open the file upload dialogue box When I click the Icon

I am working on a React project, In that I have a card, In the card I have react icon so what
I am trying to do is If I click the react icon, then file upload dialogue box has to open
please someone help to achieve this I am using Reactstrap for this
This is my code Form.js
import React, { useState, useRef } from 'react';
import './Form.css';
import { MdCloudUpload } from 'react-icons/md';
import { Row,Col,Button,Modal,ModalBody,ModalFooter } from 'reactstrap'
const Form = () => {
const inputFile = useRef(null)
const onButtonClick = () => {
inputFile.current.click();
};
return (
<Row>
<Col md="6" sm="6" xs="6">
<Modal isOpen={true}
>
<ModalBody>
<Row>
<Col md="4" sm="4" xs="4">
<div className="image-upload">
<input type='file' id='file' ref={inputFile} style={{ display: 'none' }}/>
<MdCloudUpload onClick={onButtonClick} className=' icon'></MdCloudUpload>
</div>
</Col>
<Col md="8" sm="8" xs="8">
</Col>
</Row>
</ModalBody>
<ModalFooter>
<Button color="secondary">
Cancel
</Button>
<Button type="submit" color="primary">
Submit
</Button>
</ModalFooter>
</Modal>
</Col>
</Row>
)
}
export default Form
there is two options:
use side library like react-dropzone
just add <input type="file" /> and trigger change event it when icon is clicked
here is an example
import React, { useState } from "react";
import "./Form.css";
import { MdCloudUpload } from "react-icons/md";
const Form = () => {
const onIconClick = () => {
const input = document.getElementById('file-input');
if (input) {
input.click();
}
};
return (
<Row>
<Col md="6" sm="6" xs="6">
<Modal isOpen={true}>
<ModalBody>
<Row>
<Col md="4" sm="4" xs="4">
<div className="image-upload">
<MdCloudUpload
className="icon"
onClick={onIconClick}
/>
<input
type="file"
id="file-input"
style={{ display: 'none' }}
/>
</div>
</Col>
<Col md="8" sm="8" xs="8" />
</Row>
</ModalBody>
<ModalFooter>
<Button color="secondary">Cancel</Button>
<Button type="submit" color="primary">
Submit
</Button>
</ModalFooter>
</Modal>
</Col>
</Row>
);
};
export default Form;

React - Form submit button in parent Modal

I have a Semantic UI Form:
import {Form} from 'semantic-ui-react';
<MyForm>
<Form onSubmit={_handleSubmit}>
<Form.Input name="myInput" label="My Label" value="" />
<Form.Group>
<Form.Button>Submit</Form.Button>
</Form.Group>
</Form>
</MyForm>
This form can be displayed inside a modal, or directly in a standard view in my app
My modal looks like this:
import {Button, Modal} from 'semantic-ui-react';
<Modal open={true} size="large" centered>
<Modal.Header>My Label</Modal.Header>
<Modal.Content>
<MyForm />
</Modal.Content>
<Modal.Actions>
<Button className="close-button">Cancel</Button>
{/* Insert submit button here*/}
</Modal.Actions>
</Modal>
This simple approach is working.
What I would like to do, is to have the submit button inside the Modal.Actions section when it's displayed in a modal, and keep it right after the input otherwise.
I don't know how to tell my form that the submit button is somewhere in its parent.
I finally managed to do it using a ref.
The idea is to create a ref in the form, pointing to the submit function and having a function in props to transmit this ref to my modal.
Modal:
import {Button, Modal} from 'semantic-ui-react';
const [submitFunc, setSubmitFunc] = useState();
const submitForm = () => {
if (submitFunc) {
submitFunc.current();
}
};
<Modal open={true} size="large" centered>
<Modal.Header>My Label</Modal.Header>
<Modal.Content>
<MyForm setSubmitFunc={setSubmitFunc} />
</Modal.Content>
<Modal.Actions>
<Button>Cancel</Button>
<Button onClick={submitForm}>Submit</Button>
</Modal.Actions>
</Modal>
Form:
function EditRecordForm({setSubmitFunc}) {
const submitRef = useRef(null);
useEffect(() => {
if (!!setSubmitFunc) {
setSubmitFunc(submitRef);
}
});
const handleSubmit = () => {
// Do whatever you need to retrieve form values and submit it
}
submitRef.current = handleSubmit;
return (
<MyForm>
<Form onSubmit={_handleSubmit}>
<Form.Input name="myInput" label="My Label" value="" />
<Form.Group>
<Form.Button>Submit</Form.Button>
</Form.Group>
</Form>
</MyForm>
)
}
What you can do is, you can associate the form with the button in the modal actions using a form id. Here is how you do it :-
Form:
<MyForm>
<Form id={'my-form'} onSubmit={_handleSubmit}>
{/*Form Elements}
</Form>
</MyForm>
Modal:
<Modal.Actions>
<Button>Cancel</Button>
<Button type={'submit'} form={'my-form'}>Submit</Button>
</Modal.Actions>
Following link is the tweet by the creator of chakr-ui telling the same method to join the form in a side drawer which needs to be connected to the button in the drawer footer.
https://twitter.com/thesegunadebayo/status/1330866834636201987?lang=en

ReduxForm inside Modal

I use ReactJS and want to add reduxForm to modal.
For modal I use reactstrap library that represents bootstrap 4 for React.
For form validation I use reduxForm
I have tasked add form modal with validation fields and I did that:
render() {
const {handleSubmit, fields: {
email
}} = this.props;
const FormEmail = ({touched, error}) => {
if (touched && error)
return (
<div>
<Input type="email" name="email" id="email"/>
<div className="error">{error}</div>
</div>
);
return (<Input type="email" name="email" id="email"/>);
}
return (
<div>
<Col>
<Col xs={{
size: 9
}}>
<ReportSettings/>
</Col>
<Col xs={{
size: 3
}}>
<Button outline color="primary" onClick={this.toggle}>Share</Button>
</Col>
</Col>
<Form onSubmit={handleSubmit(this.toggle)}>
<Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
<ModalHeader toggle={this.toggle}>Share</ModalHeader>
<ModalBody>
<FormGroup row>
<CheckBoxInputField data={this.state.measurements} handleFieldChange={this.handleFieldChange}/>
</FormGroup>
<FormGroup row>
<Label for="email" xs={2}>Email</Label>
<Col xs={10}>
<FormEmail {...email}/>
</Col>
</FormGroup>
<FormGroup row>
<Label for="message" xs={2}>Message</Label>
<Col xs={10}>
<Input type="textarea" name="message" id="message"/>
</Col>
</FormGroup>
</ModalBody>
<ModalFooter>
<Button action="submit" color="primary" value={true}>OK</Button>
<Button color="secondary" onClick={this.toggle} value={false}>Cancel</Button>
</ModalFooter>
</Modal>
</Form>
</div>
);
}
my toggle function:
toggle(e) {
e.preventDefault();
this.setState({
modal: !this.state.modal
});
if (e.target.value === 'true') {
const measurementsID = this.state.measurements.values.filter((measurement) => {
return measurement.checked;
}).map((measurement) => {
return measurement.value;
});
this.props.onShare(MeasurementsToBitMap(measurementsID));
}
}
validation function:
function validate(formProps) {
const errors = {};
if (!formProps.email) {
errors.email = 'Please enter an email';
}
return errors;
}
finally I export Component:
export default reduxForm({form: 'form', fields: ['email'], validate})(HeadMeasurement);
When I click on cancel button the modal close it works good, but when I click on OK button
and fields aren't valid error messages aren't showed otherwise when fields are valid modal isn't disappeared.
My question is how to combine reduxForm and Modal work together?
Thanks, Michael.
reactstrap inserts your modal definition at the bottom of the rendered html doc so wrapping <Form> around <Modal> doesn't work. You have to have your inside the . In your case since you are doing a submit action you will want it wrapped around both your footer and the body tags.
<Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
<Form onSubmit={handleSubmit(this.toggle)}>
<ModalHeader toggle={this.toggle}>Share</ModalHeader>
<ModalBody>
<FormGroup row>
<CheckBoxInputField data={this.state.measurements} handleFieldChange={this.handleFieldChange}/>
</FormGroup>
<FormGroup row>
<Label for="email" xs={2}>Email</Label>
<Col xs={10}>
<FormEmail {...email}/>
</Col>
</FormGroup>
<FormGroup row>
<Label for="message" xs={2}>Message</Label>
<Col xs={10}>
<Input type="textarea" name="message" id="message"/>
</Col>
</FormGroup>
</ModalBody>
<ModalFooter>
<Button action="submit" color="primary" value={true}>OK</Button>
<Button color="secondary" onClick={this.toggle} value={false}>Cancel</Button>
</ModalFooter>
</Form>
</Modal>
Also, did you bind your toggle to this in your constructor?
constructor(props) {
this.toggle = this.toggle.bind(this);
}
Alternatively, use arrow functions.
toggle = (e) => {
e.preventDefault();
this.setState({
modal: !this.state.modal
});
...
}

Resources