Skip to content

Commit

Permalink
Detect 0 as number as well (#793)
Browse files Browse the repository at this point in the history
* Detect 0 as number as well

Signed-off-by: Aaron Chong <aaronchongth@gmail.com>

* Display error when fields are left empty during submit

Signed-off-by: Aaron Chong <aaronchongth@gmail.com>

---------

Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth authored Oct 9, 2023
1 parent c9c4570 commit 99ecd31
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/react-components/lib/lifts/lift-request-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export const LiftRequestDialog = ({
onRequestSubmit,
onClose,
}: LiftRequestDialogProps): JSX.Element => {
const [doorState, setDoorState] = React.useState(availableDoorModes[0]);
const [requestType, setRequestType] = React.useState(availableRequestTypes[0]);
const [doorState, setDoorState] = React.useState<number | null>(availableDoorModes[0]);
const [requestType, setRequestType] = React.useState<number | null>(availableRequestTypes[0]);
const [destination, setDestination] = React.useState(currentLevel);

// Error states
Expand All @@ -93,6 +93,14 @@ export const LiftRequestDialog = ({
const isFormValid = () => {
let isValid = true;
cleanUpError();
if (requestType === null) {
setRequestTypeError('Request type cannot be empty');
isValid = false;
}
if (doorState === null) {
setDoorStateError('Door state cannot be empty');
isValid = false;
}
if (!destination) {
setDestinationError('Destination cannot be empty');
isValid = false;
Expand All @@ -104,7 +112,10 @@ export const LiftRequestDialog = ({
const handleLiftRequest = (event: React.FormEvent) => {
event.preventDefault();
if (isFormValid()) {
onRequestSubmit && onRequestSubmit(event, doorState, requestType, destination);
onRequestSubmit &&
doorState !== null &&
requestType !== null &&
onRequestSubmit(event, doorState, requestType, destination);
onClose();
}
};
Expand Down Expand Up @@ -161,7 +172,7 @@ export const LiftRequestDialog = ({
<div className={classes.divForm}>
<Autocomplete
getOptionLabel={(option) => requestModeToString(option)}
onChange={(_, value) => setRequestType((value as number) || availableRequestTypes[0])}
onChange={(_, value) => setRequestType(value as number)}
options={availableRequestTypes}
renderInput={(params) => (
<TextField
Expand Down

0 comments on commit 99ecd31

Please sign in to comment.