Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import React from 'react';
import { Scheduler } from '@jupyterlab/scheduler';
import CloseIcon from '@mui/icons-material/Close';
import IconButton from '@mui/material/IconButton';
import { i18nStrings } from '../../../../constants/common';
import { InputContainer } from '../../InputContainer';
import * as Styles from './styles';
const KEY_REGEX = new RegExp('[a-zA-Z_][a-zA-Z0-9_]*');
const VALUE_REGEX = new RegExp('[\\S\\s]*');
const errorMessageStrings = i18nStrings.ScheduleNoteBook.MainPanel.ErrorMessages.AdvancedOptions;
const widgetStrings = i18nStrings.ScheduleNoteBook.MainPanel.AdvancedOptions;
interface IEnvironmentVariable {
key: string;
value: string;
}
interface Props {
isDisabled: boolean;
index: number;
environmentParameters: IEnvironmentVariable[];
setEnvironmentParameters: (environmentParameters: IEnvironmentVariable[]) => void;
formErrors: Scheduler.ErrorsType;
setFormErrors: (errors: Scheduler.ErrorsType) => void;
}
const EnvironmentVariable: React.FunctionComponent<Props> = ({
isDisabled,
environmentParameters,
setEnvironmentParameters,
index,
formErrors,
setFormErrors,
}) => {
const currentEnvironmentVariable = environmentParameters[index];
const deleteKeyValue = (index: number) => {
const newArray = [...environmentParameters];
newArray.splice(index, 1);
setEnvironmentParameters(newArray);
setFormErrors({
...formErrors,
environmentVariablesError: '',
});
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const name = e.currentTarget.name;
const value = e.target.value;
const [field, i] = name.split('-') as [string, number];
const updateItem =
field === 'envKey'
? { key: value, value: environmentParameters[i].value }
: { key: environmentParameters[i].key, value: value };
const newArray = [...environmentParameters];
newArray.splice(i, 1, updateItem);
setEnvironmentParameters(newArray);
};
const handleBlur = () => {
const { key, value } = currentEnvironmentVariable;
Iif (key.length < 1 || value.length < 1) {
setFormErrors({
...formErrors,
environmentVariablesError: errorMessageStrings.EnvironmentVariableEmptyError,
});
return;
}
Iif (key.length > 512 || value.length > 512) {
setFormErrors({
...formErrors,
environmentVariablesError: errorMessageStrings.EnvironmentVariableLengthError,
});
return;
}
Iif (!KEY_REGEX.test(key) || !VALUE_REGEX.test(value)) {
setFormErrors({
...formErrors,
environmentVariablesError: errorMessageStrings.EnvironmentVariableFormatError,
});
return;
}
setFormErrors({
...formErrors,
environmentVariablesError: '',
});
};
return (
<div className={Styles.EnvironmentVariablesContainer}>
<InputContainer
className={Styles.EnvironmentVariablesInput}
readOnly={isDisabled}
name={`envKey-${index}`}
labelInfo={widgetStrings.Key}
value={environmentParameters[index].key}
onChange={handleChange}
onBlur={handleBlur}
/>
<InputContainer
className={Styles.EnvironmentVariablesInput}
readOnly={isDisabled}
name={`envValue-${index}`}
labelInfo={widgetStrings.Value}
value={environmentParameters[index].value}
onChange={handleChange}
onBlur={handleBlur}
/>
<div>
{!isDisabled && (
<IconButton
onClick={() => {
deleteKeyValue(index);
setFormErrors({
...formErrors,
environmentVariablesError: '',
});
}}
size="large">
<CloseIcon />
</IconButton>
)}
</div>
</div>
);
};
export { EnvironmentVariable, IEnvironmentVariable };
|