import React from 'react'; import type { Option } from 'react-select'; import Select from 'react-select'; import { noop } from '@spinnaker/core'; import type { IManifestLabelSelector } from '../IManifestLabelSelector'; import { LABEL_KINDS } from '../IManifestLabelSelector'; import './labelEditor.less'; export interface ILabelEditorProps { labelSelectors: IManifestLabelSelector[]; onLabelSelectorsChange: (labelSelectors: IManifestLabelSelector[]) => void; } const LabelKeyOptions: Array> = LABEL_KINDS.map((kind) => ({ label: kind, value: kind })); export default class LabelEditor extends React.Component { public static defaultProps: Partial = { labelSelectors: [], onLabelSelectorsChange: noop, }; private static convertValueStringToArray = (value: string): string[] => { return value.split(',').map((v: string) => v.trim()); }; private handleChange = (idx: number, property: keyof IManifestLabelSelector, value: string | string[]): void => { this.props.onLabelSelectorsChange( this.props.labelSelectors.map((ls, i) => { if (idx !== i) { return ls; } return { ...ls, [property]: value, }; }), ); }; private removeField = (idx: number): void => { this.props.onLabelSelectorsChange( this.props.labelSelectors.filter((_ls, i) => { return idx !== i; }), ); }; private addField = (): void => { return this.props.onLabelSelectorsChange( this.props.labelSelectors.concat([ { key: '', kind: 'EQUALS', values: [], }, ]), ); }; public render() { return ( {this.props.labelSelectors.map((selector, idx) => ( ))}
Key Kind Value(s)
this.handleChange(idx, 'key', e.target.value)} /> this.handleChange(idx, 'values', LabelEditor.convertValueStringToArray(e.target.value)) } placeholder="Comma-separated values" value={selector.values.join(', ')} type="text" />
); } }