Skip to content

Commit

Permalink
Merge branch 'refs/heads/execute-screen-work'
Browse files Browse the repository at this point in the history
  • Loading branch information
omergunr100 committed Jun 18, 2024
2 parents 9b994e5 + e12ca80 commit 089f299
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import Typography from "@mui/material/Typography";
import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "@mui/material/Checkbox";

export const BooleanInput = ({field, defaultValue, onChange, title, required}: InputProps) => {
const [value, setValue] = useState<boolean>(defaultValue);
export const BooleanInput = ({defaultValue, setValue, title, required}: InputProps) => {
const [localValue, setLocalValue] = useState<boolean>(defaultValue);

const handleOnChange = (e: ChangeEvent<HTMLInputElement>) => {
const value = e.target.checked;
onChange(field, value);
setValue(value);
const val = e.target.checked;
setValue(val);
setLocalValue(val);
}

return (
<Box sx={{ mb: 2 }}>
<FormControlLabel
control={
<Checkbox
checked={value}
checked={localValue}
onChange={handleOnChange}
required={required}
color="primary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import TextField from "@mui/material/TextField";

export const NumberInput = ({field, defaultValue, onChange, title, required}: InputProps) => {
const [value, setValue] = useState<number | undefined>(defaultValue);
export const NumberInput = ({defaultValue, setValue, title, required}: InputProps) => {
const [localValue, setLocalValue] = useState<number | undefined>(defaultValue);
const [error, setError] = useState<string | null>(null);

const handleOnChange = (e: ChangeEvent<HTMLInputElement>) => {
const value = Number(e.target.value);
setValue(value)
if (value && !isNaN(value)) {
onChange(field, value);
const val = Number(e.target.value);
setLocalValue(val)
if (val && !isNaN(val)) {
setValue(val);
setError(null)
}
else {
setValue(undefined);
setError("Invalid number")
}
}
Expand All @@ -28,7 +29,7 @@ export const NumberInput = ({field, defaultValue, onChange, title, required}: In
<TextField
id="number-input"
type="number"
value={value}
value={localValue}
onChange={handleOnChange}
error={!!error}
helperText={error}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import Select, {SelectChangeEvent} from "@mui/material/Select";
import MenuItem from "@mui/material/MenuItem";
import FormControl from "@mui/material/FormControl";

export const OptionsInput = ({ field, defaultValue, onChange, title, required, options, disabled }: InputProps) => {
const [value, setValue] = useState<string | undefined>(defaultValue);
export const OptionsInput = ({defaultValue, setValue, title, required, options}: InputProps) => {
const [localValue, setLocalValue] = useState<string | undefined>(defaultValue);

const handleOnChange = (e: SelectChangeEvent<string>) => {
const value = e.target.value as string;
onChange(field, value);
setValue(value);
const val = e.target.value as string;
setValue(val);
setLocalValue(val);
};

return (
<Box sx={{ mb: 2 }}>
<Typography variant="body1" mr={1}>
{title}
</Typography>
<FormControl fullWidth variant="outlined" required={required} disabled={disabled}>
<FormControl fullWidth variant="outlined" required={required}>
<Select
value={value ?? ''}
value={localValue ?? ''}
onChange={handleOnChange}
>
{options.map(option => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ import {InputAdornment} from "@mui/material";
import IconButton from "@mui/material/IconButton";
import DeleteIcon from "@mui/icons-material/Delete";

export const StringInput = ({ field, defaultValue, onChange, title, required, isArr }: InputProps) => {
const [value, setValue] = useState<string | undefined>(defaultValue);
const [values, setValues] = useState<string[]>(defaultValue ? [defaultValue as string] : ['']);
export const StringInput = ({defaultValue, setValue, title, required, isArr }: InputProps) => {
const [localValue, setLocalValue] = useState<string | undefined>(defaultValue);
const [localValues, setLocalValues] = useState<string[]>(defaultValue ? [defaultValue as string] : ['']);

const handleSingleChange = (e: ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value;
onChange(field, newValue);
setValue(newValue);
setLocalValue(newValue);
};

const handleArrayChange = (index: number) => (e: ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value;
const updatedValues = [...values];
const updatedValues = [...localValues];
updatedValues[index] = newValue;
setValues(updatedValues);
onChange(field, updatedValues);
setLocalValues(updatedValues);
setValue(updatedValues);
};

const handleAddField = () => {
setValues([...values, '']);
setLocalValues([...localValues, '']);
};

const handleRemoveField = (index: number) => () => {
const updatedValues = values.filter((_, i) => i !== index);
setValues(updatedValues);
onChange(field, updatedValues);
const updatedValues = localValues.filter((_, i) => i !== index);
setLocalValues(updatedValues);
setValue(updatedValues);
};

return (
Expand All @@ -46,15 +46,15 @@ export const StringInput = ({ field, defaultValue, onChange, title, required, is
<TextField
id="string-input"
type="string"
value={value}
value={localValue}
onChange={handleSingleChange}
required={required}
fullWidth
variant="outlined"
/>
) : (
<>
{values.map((value, index) => (
{localValues.map((value, index) => (
<TextField
key={index}
id={`string-input-${index}`}
Expand Down
20 changes: 8 additions & 12 deletions netbuddy.client/src/screens/sequence/execution/inputs/UrlInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import TextField from "@mui/material/TextField";

export const UrlInput = ({field, defaultValue, onChange, title, required}: InputProps) => {
const [value, setValue] = useState<string | undefined>(defaultValue);
export const UrlInput = ({defaultValue, setValue, title, required}: InputProps) => {
const [localValue, setLocalValue] = useState<string | undefined>(defaultValue);
const [error, setError] = useState<string | null>(null);

const handleOnChange = (e: ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
setValue(value)

if(!value) {
setError(null)
return
}
const val = e.target.value;
setLocalValue(val);

const urlPattern = /^(http|https):\/\/[^ "]+$/;
if (value.match(urlPattern)) {
onChange(field, value);
if (val.match(urlPattern)) {
setValue(val);
setError(null)
}
else {
setValue(undefined);
setError("Invalid URL")
}
}
Expand All @@ -35,7 +31,7 @@ export const UrlInput = ({field, defaultValue, onChange, title, required}: Input
<TextField
id="url-input"
type="url"
value={value}
value={localValue}
onChange={handleOnChange}
error={!!error}
helperText={error}
Expand Down

0 comments on commit 089f299

Please sign in to comment.