-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move EnvVar component to a separate file
- Loading branch information
Showing
2 changed files
with
59 additions
and
50 deletions.
There are no files selected for viewing
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,58 @@ | ||
import React from 'react'; | ||
import { Button } from "@patternfly/react-core/dist/esm/components/Button"; | ||
import { FormGroup } from "@patternfly/react-core/dist/esm/components/Form"; | ||
import { Grid } from "@patternfly/react-core/dist/esm/layouts/Grid"; | ||
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput"; | ||
import { TrashIcon } from '@patternfly/react-icons'; | ||
import cockpit from 'cockpit'; | ||
|
||
const _ = cockpit.gettext; | ||
|
||
const handleEnvValue = (key, value, idx, onChange, additem, itemCount, companionField) => { | ||
// Allow the input of KEY=VALUE separated value pairs for bulk import only if the other | ||
// field is not empty. | ||
if (value.includes('=') && !companionField) { | ||
const parts = value.trim().split(" "); | ||
let index = idx; | ||
for (const part of parts) { | ||
const [envKey, ...envVar] = part.split('='); | ||
if (!envKey || !envVar) { | ||
continue; | ||
} | ||
|
||
if (index !== idx) { | ||
additem(); | ||
} | ||
onChange(index, 'envKey', envKey); | ||
onChange(index, 'envValue', envVar.join('=')); | ||
index++; | ||
} | ||
} else { | ||
onChange(idx, key, value); | ||
} | ||
}; | ||
|
||
export const EnvVar = ({ id, item, onChange, idx, removeitem, additem, itemCount }) => | ||
( | ||
<Grid hasGutter id={id}> | ||
<FormGroup className="pf-m-6-col-on-md" label={_("Key")} fieldId={id + "-key-address"}> | ||
<TextInput id={id + "-key"} | ||
value={item.envKey || ''} | ||
onChange={(_, value) => handleEnvValue('envKey', value, idx, onChange, additem, itemCount, item.envValue)} /> | ||
</FormGroup> | ||
<FormGroup className="pf-m-6-col-on-md" label={_("Value")} fieldId={id + "-value-address"}> | ||
<TextInput id={id + "-value"} | ||
value={item.envValue || ''} | ||
onChange={(_, value) => handleEnvValue('envValue', value, idx, onChange, additem, itemCount, item.envKey)} /> | ||
</FormGroup> | ||
<FormGroup className="pf-m-1-col-on-md remove-button-group"> | ||
<Button variant='plain' | ||
className="btn-close" | ||
id={id + "-btn-close"} | ||
size="sm" | ||
aria-label={_("Remove item")} | ||
icon={<TrashIcon />} | ||
onClick={() => removeitem(idx)} /> | ||
</FormGroup> | ||
</Grid> | ||
); |
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