-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(register-new-machine): making location required if category is o…
…f certain type (#17276) * Making location required if category is of certain type * making const --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
62ed00f
commit 15edb19
Showing
4 changed files
with
69 additions
and
19 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
libs/application/templates/aosh/register-new-machine/src/fields/LocationInputField/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { FieldBaseProps } from '@island.is/application/types' | ||
import { FC, useState } from 'react' | ||
import { useFormContext } from 'react-hook-form' | ||
import { coreErrorMessages } from '@island.is/application/core' | ||
import { AboutMachine } from '../../lib/dataSchema' | ||
import { Box } from '@island.is/island-ui/core' | ||
import { InputController } from '@island.is/shared/form-fields' | ||
import { useLocale } from '@island.is/localization' | ||
import { machine } from '../../lib/messages' | ||
|
||
export const LocationInputField: FC<React.PropsWithChildren<FieldBaseProps>> = ( | ||
props, | ||
) => { | ||
const { field, setBeforeSubmitCallback } = props | ||
const { formatMessage } = useLocale() | ||
const { watch } = useFormContext() | ||
const [displayError, setDisplayError] = useState<boolean>(false) | ||
const watchMachine = watch('machine.aboutMachine') as AboutMachine | ||
const location = watch(field.id) as string | ||
const categoryValue = 'Fólkslyftur og vörulyftur' | ||
|
||
setBeforeSubmitCallback?.(async () => { | ||
if ( | ||
watchMachine.category?.nameIs === categoryValue && | ||
location.length === 0 | ||
) { | ||
setDisplayError(true) | ||
return [false, ''] | ||
} | ||
return [true, null] | ||
}) | ||
|
||
return ( | ||
<Box paddingTop={2}> | ||
<InputController | ||
id={field.id} | ||
label={formatMessage(machine.labels.basicMachineInformation.location)} | ||
backgroundColor="blue" | ||
required={watchMachine.category?.nameIs === categoryValue} | ||
maxLength={255} | ||
onChange={() => setDisplayError(false)} | ||
error={ | ||
displayError && location.length === 0 | ||
? formatMessage(coreErrorMessages.defaultError) | ||
: undefined | ||
} | ||
/> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters