Matelutex
Matelutex

Reputation: 2210

React - how to invoke popup window in my case?

I'm not a React expert yet thus I have a question for you - how to invoke my popup window from:

import {options, columns,convertToArray} from './consts'

    const index = () => {

        const {data, loading, error, performFetch} = fetchHook({path: "/xxx/yyy", fetchOnMount: true})

        return (
            <div className={classes.Container}>
                <h1>List of products</h1>
                <Divider className={classes.Divider} />
                <ProductTable data={convertToArray(data)} options={options} columns={columns}/>
            </div>
        )
    }

    export default index;

consts.js

export const actions = (productPropertyId, showModal) => {
    const productDetails = (productPropertyId) => {
    }

    const removeProduct = (productPropertyId, showModal) => {
    actions(productPropertyId, showModal);

    return (
        <div className={classes.actionsContainer}>
            <Button
                onClick={() => productDetails(productPropertyId)}
            > {"More"}
            </Button>
            <Button
                const removeProduct = (productPropertyId, showModal) => {
    actions(productPropertyId, showModal);
            >{"Remove"}
            </Button>
        </div>
    )
};


export const convertToArray = (productList) => {
    let products = []
    if (productList != null) {
        productList.map(product => {
           column1, column2, column3, actions(product.id)]
            products.push(prod)
        })
    }
    return products;
};

My popup is --> <FormDialog/> based on react Materials.

Is it possible to invoke popup in this place?

I have a react material table with some columns. The last column contains 2 buttons, one of them is "Remove" (row). Here I want to invoke my popup. Maybe I should rebuild my structure?

UPDATE

Below is my popup - I wonder how to run this popup from the place above:

const formDialog = (popupOpen) => {
    const [open, setOpen] = React.useState(false);

    const handleClickOpen = () => {
        setOpen(true);
    };

    const handleClose = () => {
        setOpen(false);
    };

    return (
        <div>
            {/*<Button variant="outlined" color="primary" onClick={handleClickOpen}>*/}
            {/*    Open alert dialog*/}
            {/*</Button>*/}
            <Dialog open={open} onClose={handleClose} aria-labelledby="form-dialog-title">
                <DialogTitle id="form-dialog-title">Subscribe</DialogTitle>
                <DialogContent>
                    <DialogContentText>
                        To subscribe to this website, please enter your email address here. We will send updates
                        occasionally.
                    </DialogContentText>
                    <TextField
                        autoFocus
                        margin="dense"
                        id="name"
                        label="Email Address"
                        type="email"
                        fullWidth
                    />
                </DialogContent>
                <DialogActions>
                    <Button onClick={handleClose} color="primary">
                        Cancel
                    </Button>
                    <Button onClick={handleClose} color="primary">
                        Subscribe
                    </Button>
                </DialogActions>
            </Dialog>
        </div>
    );
}

export default formDialog;

UPDATE 2

I updated my code taking into cosideration the tips from your response, see above. Can I add a parameter showModal in my export const actions = (productPropertyId, showModal) and then invoke this component with different showModal value? UNfortunately my popup doesn't appear when I click on Remove button :(

Upvotes: 0

Views: 576

Answers (1)

Celso Wellington
Celso Wellington

Reputation: 890

You can invoke it conditionally and controle it using some state variable. Like this:

const [removeModal, removeToggle] = useState(false);
return (<>
<div className={classes.actionsContainer}>
            <Button
                onClick={() => productDetails(productPropertyId)}
            > {"More"}
            </Button>
            <Button
                onClick={() => removeToggle(true)}
            >{"Remove"}
            </Button>
        </div>
{removeModal ? <YourComponent /> : ''}
</>
)

I'm using a react fragment there <></> just to position the modal div outside the main div, but you can also invoke it inside your main div as well (I usually do this and set the position: fixed so the modal/popup coud appear in top of everything).

Upvotes: 1

Related Questions