Change properties of a bootstrap button conditionally - reactjs

I have a bootstrap dropdown button that I wish to disable if two ids meet. This is what the button currently looks like.
<>
<DropdownButton
title={fixtureDetails.homeTeam?.abbreviation + " End"}
variant="outline-primary"
className="ml-2"
size="sm"
disabled
>
This is what i'm trying to achieve;
<>
<DropdownButton
title={fixtureDetails.homeTeam?.abbreviation + " End"}
variant="outline-primary"
className="ml-2"
size="sm"
{ previousTeamId() == homeTeam ? disabled : "" }
>

I thinks you could do something like this
<DropdownButton
title={fixtureDetails.homeTeam?.abbreviation + " End"}
variant="outline-primary"
className="ml-2"
size="sm"
isDisabled ={ previousTeamId() == homeTeam ? true : false}
/>
and then bind that props with your button component to disabled it.
<button disabled={PaymentResponse.isDisabled}>
Check
</button>

Related

Add tooltip that only appears if button is disabled

I have this Button in my React application:
<Button
onClick={this.showReissue}
primary={true}
fullWidth={false}
disabled={this.state.disableButton}
>
this.state.disableButton is a boolean value.
Is there any way I can add a tooltip that is only active if the button is disabled?
I guess it would be something like this
<Tooltip title={disableButton == true ? "" : "test"}>
<Button
variant="outlined"
onClick={showReissue}
primary={true}
fullWidth={false}
disabled={disableButton}
>
Test
</Button>
</Tooltip>
here is a link to try it (I used MUI but the logic is the same)
https://codesandbox.io/s/exciting-germain-dumokw?file=/src/App.js:373-651
I get inspired from this answer
just add the same conditional to the tittle attribute like so
<Button
onClick={this.showReissue}
primary={true}
fullWidth={false}
disabled={this.state.disableButton}
title={this.state.disableButton ? "your tooltip":""}>

How to add tooltip to a disabled button using ReactStrap

I have a button which gets enable and disable based on condition, I want to add tooltip to a disable button. Which I am not getting how to do using reactstrap.
<Col sm={6}>
<Button
type="button"
id = {`button-${companyId}`}
disabled={
this.company[companyId] &&
this.company[companyId].length
? false
: true
}
title = {this.company[company.id].length < 1? // As a makeover i Used title
"Not Active" : ""}
style={{ marginLeft: "-16px" }}
>
</Button>
User list
{this.company[companyId].length < 1 ? (
<UncontrolledTooltip
target={`button-${companyId}`}
placement="bottom"
fade={false}
>
"Active"
</UncontrolledTooltip>
) : null}
</Col>
But this does not add tooltip to the button. Can anyone please suggest what I am doing wrong here.
I recomend you to use condition like this ;
<Button
color="secondary"
size="lg"
disabled={(this.company[companyId] && this.company[companyId].length) ? false: true}>
Button
</Button>
Or disabled={!(this.company[companyId] && this.company[companyId].length)}>

How to add href to a button in react material-ui stepper

I'm trying to redirect to a page when the user clicks on the "Book now" button at the end of a Stepper instead of displaying "Thank you for your interest". Can this be possible by adding an href="/booking" to a button at the last step? Or there is some other way by adding a function to handle this? Thank you in advance!
Here is the part of the code I'm referring to:
<React.Fragment>
{activeStep === steps.length ? (
<React.Fragment>
<Typography variant="h5" gutterBottom>
Thank you for your interest!
</Typography>
</React.Fragment>
) : (
<React.Fragment>
{this.getStepContent(activeStep)}
<div className={classes.buttons}>
{activeStep !== 0 && (
<Button onClick={this.handleBack} className={classes.button}>
Back
</Button>
)}
<Button
variant="contained"
color="primary"
onClick={this.handleNext}
className={classes.button}
>
{activeStep === steps.length - 1 ? 'Book Now' : 'Next'}
</Button>
</div>
</React.Fragment>
)}
</React.Fragment>
As mentioned in my comments, I'd give a solution based on the official docs code at: https://codesandbox.io/s/tnpyj?file=/demo.js
You can create a function like the one below to keep track of the activeStep.
const isLastStep = () => {
if (activeStep === steps.length) window.location.href = '\booking';
return false;
};
Now in your template you can call this function instead of manual checking of last step as below:
{isLastStep ? (
This is a dirty solution but doing it correctly as mentioned in the link above can make it look cleaner. I hope it helps.

How can I add a class to a button when a Formik Form isSubmitting?

How can I add a class to a button when a Formik Form isSubmitting?
I can see rendering different text if the form isSubmitting eg {isSubmitting ? "Please wait..." : "LOG IN"}
- but how can I add a class/ className to the button?
<button
type="submit"
className={`btn`}
onClick={() => {
api.submitForm();
}}
disabled={api.isSubmitting}
>
LOG IN
</button>
You can simply do
className={'btn ' + (isSubmitting ? 'btn-while-submitting' : '')}
Or using something like classnames
className={classNames('btn', {
['btn-while-submitting']: isSubmitting,
})}

Influence tab order of Material UI controls

I have an app built up with React and Material UI. Within one view it is possible to have several text fields and several buttons. Now, when I have the focus on one text field and then press Tab I cannot reliably anticipate which one of the controls will be the next one to get the focus. I want to first tab through all the text fields and then secondly tab through all the buttons.
<DialogContent>
<DialogContentText>
The username and password that were used are incorrect. Please provide the correct credentials in order to login to the API.
<Stepper activeStep={this.state.credentialsStep} orientation='vertical'>
{
this.steps.map((label, index) => (
<Step key={label}>
<StepLabel>{label}</StepLabel>
<StepContent>
<Typography>{this.stepContent[index]}</Typography>
{this.stepAction[index]}
<Grid container direction='row' className='m-t-26'>
<Button color='primary'
onClick={() => {
this.state.credentialsStep === 0 ? this.onClickCancel() : this.onClickBack();
}}>
{this.state.credentialsStep === 0 ? 'Cancel' : 'Back'}
</Button>
<Button variant='contained'
color='primary'
onClick={() => {
this.state.credentialsStep === this.steps.length - 1 ? this.onClickLogin() : this.onClickNext();
}}>
{this.state.credentialsStep === this.steps.length - 1 ? 'Login' : 'Next'}
</Button>
</Grid>
</StepContent>
</Step>
))
}
</Stepper>
</DialogContentText>
</DialogContent>
Is there a way to set the tab order of controls?
You can control this with the tabIndex property, but you may be better off to figure out how to have the elements appear in the source in the order you would want the focus to go.
I have found this resource handy: https://bitsofco.de/how-and-when-to-use-the-tabindex-attribute/
When to use a positive tabindex value
There is almost no reason to
ever use a positive value to tabindex, and it is actually considered
an anti-pattern. If you’re finding the need to use this value to
change the order in which elements become focusable, it is likely that
what you actually need to do is change the source order of the HTML
elements.
One of the problems with explicitly controlling tabindex order is that any elements with a positive value are going to come before any other focusable elements that you haven't explicitly put a tabindex on. This means that you could end up with very confusing focus order if you miss any elements that you would want in the mix.
If you want to have the button on the right come before the button on the left in the focus order, there are various CSS options that would allow the button on the right to come first in the source order.
If, however, you decide that explicitly specifying the tabindex is your best option, here is an example showing how to do this for TextField and Button:
import React from "react";
import ReactDOM from "react-dom";
import TextField from "#material-ui/core/TextField";
import Button from "#material-ui/core/Button";
function App() {
return (
<div className="App">
<TextField label="1" inputProps={{ tabIndex: "1" }} />
<br />
<TextField label="3" inputProps={{ tabIndex: "3" }} />
<br />
<TextField label="2" inputProps={{ tabIndex: "2" }} />
<br />
<Button tabIndex="5">Button 5</Button>
<Button tabIndex="4">Button 4</Button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
You may want to use the html attribute tabindex. This allows you to specify the order that tabbing will go through in your form. You can read more about it here and I've put a small example below, setting the tab index of your button to #1
<StepContent>
<Typography>{this.stepContent[index]}</Typography>
{this.stepAction[index]}
<Grid container direction="row" className="m-t-26">
<Button
tabIndex="1" // This will make the button the first tab index for the form.
color="primary"
onClick={() => {
this.state.credentialsStep === 0
? this.onClickCancel()
: this.onClickBack();
}}
>
{this.state.credentialsStep === 0 ? "Cancel" : "Back"}
</Button>
<Button
variant="contained"
color="primary"
onClick={() => {
this.state.credentialsStep === this.steps.length - 1
? this.onClickLogin()
: this.onClickNext();
}}
>
{this.state.credentialsStep === this.steps.length - 1 ? "Login" : "Next"}
</Button>
</Grid>
</StepContent>;
You can use a css trick to render the buttons in reverse order, but with css to reverse the buttons in UI.
<DialogContent>
<DialogContentText>
The username and password that were used are incorrect. Please provide the correct credentials in order to login to the API.
<Stepper activeStep={this.state.credentialsStep} orientation='vertical'>
{
this.steps.map((label, index) => (
<Step key={label}>
<StepLabel>{label}</StepLabel>
<StepContent>
<Typography>{this.stepContent[index]}</Typography>
{this.stepAction[index]}
<Grid container direction='row' className='m-t-26'>
// Box wrapper added <Box style={{ display: 'flex', flexDirection: 'row-reverse', justifyContent: 'flex-end' }}>
// First Button is now "Next in JSX <Button variant='contained'
color='primary'
onClick={() => {
this.state.credentialsStep === this.steps.length - 1 ? this.onClickLogin() : this.onClickNext();
}}>
{this.state.credentialsStep === this.steps.length - 1 ? 'Login' : 'Next'}
</Button>
<Button color='primary'
onClick={() => {
this.state.credentialsStep === 0 ? this.onClickCancel() : this.onClickBack();
}}>
{this.state.credentialsStep === 0 ? 'Cancel' : 'Back'}
</Button>
</Box>
</Grid>
</StepContent>
</Step>
))
}
</Stepper>
</DialogContentText>
</DialogContent>

Resources