Skip to content

Commit

Permalink
Validate field in Add remote File
Browse files Browse the repository at this point in the history
  • Loading branch information
guergana committed May 7, 2024
1 parent e3c555e commit 8596817
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion client/components/Parts/Dialogs/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import InputAdornment from '@mui/material/InputAdornment'
import TextField from '@mui/material/TextField'
import ConfirmDialog, { ConfirmDialogProps } from './Confirm'
import * as helpers from '../../../helpers'

export interface InputDialogProps extends Omit<ConfirmDialogProps, 'onConfirm'> {
value?: string
Expand All @@ -14,10 +15,23 @@ export interface InputDialogProps extends Omit<ConfirmDialogProps, 'onConfirm'>
export default function InputDialog(props: InputDialogProps) {
const { value: initValue, prefix, placholder, spellcheck, onConfirm, ...rest } = props
const [value, setValue] = React.useState(initValue || '')
const handleConfirm = () => onConfirm && onConfirm(value)
const [textFieldError, setTextFieldError] = React.useState(false)
const [errorHelperText, setErrorHelperText] = React.useState('')
const handleConfirm = () => {
if(value !== '' && helpers.isUrlValid(value)){
setTextFieldError(false)
return onConfirm && onConfirm(value)
} else {
setTextFieldError(true)
if(value === '') { setErrorHelperText('Empty field!') }
else if(!helpers.isUrlValid(value)) { setErrorHelperText('Invalid URL') }
}
}
return (
<ConfirmDialog {...rest} onConfirm={handleConfirm}>
<TextField
error={textFieldError}
helperText={textFieldError ? errorHelperText : ' '}
autoFocus
fullWidth
size="small"
Expand Down
1 change: 1 addition & 0 deletions client/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './menu'
export * from './portal'
export * from './report'
export * from './table'
export * from './validateURL'
8 changes: 8 additions & 0 deletions client/helpers/validateURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function isUrlValid(str: string) {
try {
new URL(str)
return true
} catch (err) {
return false
}
}

0 comments on commit 8596817

Please sign in to comment.