Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate textfield input in "Add remote File" #374

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('Enter an URL') }
else if(!helpers.isUrlValid(value)) { setErrorHelperText('Enter a valid 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
}
}
Loading