-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(kubernetes): namespace selector component (#7012)
create a namespace selector component which can be reused. optional creatable functionality to support cases where SpEL is allowable.
- Loading branch information
1 parent
53cb422
commit 8e97d59
Showing
2 changed files
with
45 additions
and
13 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
38 changes: 38 additions & 0 deletions
38
app/scripts/modules/kubernetes/src/v2/pipelines/stages/deployManifest/NamespaceSelector.tsx
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,38 @@ | ||
import * as React from 'react'; | ||
import Select, { Creatable, Option } from 'react-select'; | ||
import { get, find, map } from 'lodash'; | ||
import { IAccountDetails } from '@spinnaker/core'; | ||
|
||
export interface INamespaceSelectorProps { | ||
onChange: (namespace: string) => void; | ||
accounts: IAccountDetails[]; | ||
selectedAccount: string; | ||
selectedNamespace: string; | ||
createable?: boolean; | ||
} | ||
|
||
export class NamespaceSelector extends React.Component<INamespaceSelectorProps> { | ||
public defaultProps = { createable: false }; | ||
|
||
private getNamespaceOptions(): Array<Option<string>> { | ||
const { accounts, selectedAccount, selectedNamespace } = this.props; | ||
const selectedAccountDetails = find(accounts, a => a.name === selectedAccount); | ||
const namespaces = get(selectedAccountDetails, 'namespaces', []); | ||
const options = map(namespaces, n => ({ label: n, value: n })); | ||
// only create a value for selectedNamespace if it contains a SPeL expression | ||
if (this.props.createable && selectedNamespace.includes('${')) { | ||
options.push({ label: selectedNamespace, value: selectedNamespace }); | ||
} | ||
return options; | ||
} | ||
|
||
public render() { | ||
const componentProps = { | ||
clearable: false, | ||
options: this.getNamespaceOptions(), | ||
value: this.props.selectedNamespace, | ||
onChange: (option: Option) => this.props.onChange(option.value.toString()), | ||
}; | ||
return this.props.createable ? <Creatable {...componentProps} /> : <Select {...componentProps} />; | ||
} | ||
} |