Skip to content

Instantly share code, notes, and snippets.

@appsparkler
Last active January 19, 2021 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appsparkler/b99ca3010976d17870c86178e4493de4 to your computer and use it in GitHub Desktop.
Save appsparkler/b99ca3010976d17870c86178e4493de4 to your computer and use it in GitHub Desktop.
Conditionally setting a value
const getIsDisabled = ({fromDate, toDate, selectedKey) => {
let isDisabled = true; // set the initial value to true
if(selectedKey === 'valueA') {
isDisabled = false;
} else if(selectedKey === 'valueB') {
if(fromDate) {
const isfromDateValid = moment(fromDate).isValid();
const isfromDateGreaterThanOrEqualToToday = moment().diff(fromDate) <= 0;
if(isfromDateValid && isfromDateGreaterThanOrEqualToToday) {
isDisabled = false
}
}
if(toDate) {
const isToDateValid = moment(toDate).isValid();
const isToDateGreaterThanOrEqualToFromDate = moment(fromDate)
.diff(toDate) <= 0;
if(isToDateValid && isToDateGreaterThanOrEqualToFromDate) {
isDisabled = false
}
}
}
return isDisabled;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment