Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c359d02
commit 198686f
Showing
8 changed files
with
187 additions
and
29 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
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
24 changes: 19 additions & 5 deletions
24
frontend/packages/knative-plugin/src/components/add/event-sources/EventSourcesSelector.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 |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import * as React from 'react'; | ||
import { useFormikContext, FormikValues } from 'formik'; | ||
import { ItemSelectorField } from '@console/shared'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import { NormalizedEventSources } from '../import-types'; | ||
import { getEventSourceData } from '../../../utils/create-eventsources-utils'; | ||
|
||
interface EventSourcesSelectorProps { | ||
eventSourceList: NormalizedEventSources; | ||
} | ||
|
||
const EventSourcesSelector: React.FC<EventSourcesSelectorProps> = ({ eventSourceList }) => ( | ||
<FormSection title="Type" fullWidth> | ||
<ItemSelectorField itemList={eventSourceList} name="type" /> | ||
</FormSection> | ||
); | ||
const EventSourcesSelector: React.FC<EventSourcesSelectorProps> = ({ eventSourceList }) => { | ||
const { setFieldValue, setFieldTouched, validateForm } = useFormikContext<FormikValues>(); | ||
const handleItemChange = React.useCallback( | ||
(item: string) => { | ||
const name = `data.${item.toLowerCase()}`; | ||
setFieldValue(name, getEventSourceData(item.toLowerCase())); | ||
setFieldTouched(name, true); | ||
validateForm(); | ||
}, | ||
[setFieldValue, setFieldTouched, validateForm], | ||
); | ||
return ( | ||
<FormSection title="Type" fullWidth> | ||
<ItemSelectorField itemList={eventSourceList} name="type" onSelect={handleItemChange} /> | ||
</FormSection> | ||
); | ||
}; | ||
|
||
export default EventSourcesSelector; |
65 changes: 65 additions & 0 deletions
65
frontend/packages/knative-plugin/src/components/add/event-sources/SinkBindingSection.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,65 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { useFormikContext, FormikValues } from 'formik'; | ||
import { TextInputTypes, FormGroup } from '@patternfly/react-core'; | ||
import { InputField, getFieldId } from '@console/shared'; | ||
import { NameValueEditor } from '@console/internal/components/utils/name-value-editor'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
|
||
const SinkBindingSection: React.FC = () => { | ||
const { values, setFieldValue } = useFormikContext<FormikValues>(); | ||
const initVal = values?.data?.sinkbinding?.subject?.selector?.matchLabels || {}; | ||
const initialValueResources = !_.isEmpty(initVal) | ||
? _.map(initVal, (key, val) => [val, key]) | ||
: [['', '']]; | ||
const [nameValue, setNameValue] = React.useState(initialValueResources); | ||
const handleNameValuePairs = React.useCallback( | ||
({ nameValuePairs }) => { | ||
let updatedNameValuePairs = {}; | ||
_.forEach(nameValuePairs, ([name, value]) => { | ||
if (value.length) { | ||
updatedNameValuePairs = { ...updatedNameValuePairs, [name]: value }; | ||
return updatedNameValuePairs; | ||
} | ||
return updatedNameValuePairs; | ||
}); | ||
setNameValue(nameValuePairs); | ||
setFieldValue('data.sinkbinding.subject.selector.matchLabels', updatedNameValuePairs); | ||
}, | ||
[setFieldValue], | ||
); | ||
const fieldId = getFieldId(values.type, 'subject-matchLabels'); | ||
return ( | ||
<FormSection title="Subject"> | ||
<InputField | ||
data-test-id="sinkbinding-apiversion-field" | ||
type={TextInputTypes.text} | ||
name="data.sinkbinding.subject.apiVersion" | ||
label="apiVersion" | ||
helpText="apiVersion i.e apps/v1" | ||
required | ||
/> | ||
<InputField | ||
data-test-id="sinkbinding-kind-field" | ||
type={TextInputTypes.text} | ||
name="data.sinkbinding.subject.kind" | ||
label="Kind" | ||
helpText="kind i.e Deployment" | ||
required | ||
/> | ||
<FormGroup fieldId={fieldId} label="Match Labels"> | ||
<NameValueEditor | ||
nameValuePairs={nameValue} | ||
valueString="Value" | ||
nameString="Name" | ||
addString="Add Values" | ||
readOnly={false} | ||
allowSorting={false} | ||
updateParentData={handleNameValuePairs} | ||
/> | ||
</FormGroup> | ||
</FormSection> | ||
); | ||
}; | ||
|
||
export default SinkBindingSection; |
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
40 changes: 40 additions & 0 deletions
40
...ges/knative-plugin/src/components/add/event-sources/__tests__/SinkBindingSection.spec.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,40 @@ | ||
import * as React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { InputField } from '@console/shared'; | ||
import { NameValueEditor } from '@console/internal/components/utils/name-value-editor'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import SinkBindingSection from '../SinkBindingSection'; | ||
|
||
jest.mock('formik', () => ({ | ||
useField: jest.fn(() => [{}, {}]), | ||
useFormikContext: jest.fn(() => ({ | ||
setFieldValue: jest.fn(), | ||
setFieldTouched: jest.fn(), | ||
validateForm: jest.fn(), | ||
values: { | ||
type: 'SinkBinding', | ||
}, | ||
})), | ||
getFieldId: jest.fn(), | ||
})); | ||
describe('SinkBindingSection', () => { | ||
it('should render FormSection', () => { | ||
const wrapper = shallow(<SinkBindingSection />); | ||
expect(wrapper.find(FormSection)).toHaveLength(1); | ||
expect(wrapper.find(FormSection).props().title).toBe('Subject'); | ||
}); | ||
|
||
it('should render NameValueEditor', () => { | ||
const wrapper = shallow(<SinkBindingSection />); | ||
const nameValueEditorField = wrapper.find(NameValueEditor); | ||
expect(nameValueEditorField).toHaveLength(1); | ||
expect(nameValueEditorField.props().nameString).toBe('Name'); | ||
expect(nameValueEditorField.props().valueString).toBe('Value'); | ||
}); | ||
|
||
it('should render InputFields', () => { | ||
const wrapper = shallow(<SinkBindingSection />); | ||
const inputFieldFieldItems = wrapper.find(InputField); | ||
expect(inputFieldFieldItems).toHaveLength(2); | ||
}); | ||
}); |
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
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