Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable required toggle when field has default value and is read only #3449

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,20 @@ export const ConnectionTypeDataFieldModal: React.FC<Props> = ({
</FormGroup>
<DataFieldPropertiesForm
field={newField}
onChange={(value) => setData('properties', value)}
onChange={(value) => {
setData('properties', value);
if (value.defaultReadOnly && newField.required) {
setData('required', false);
}
}}
onValidate={setPropertiesValid}
/>
<FormGroup fieldId="isRequired">
<Checkbox
id="isRequired"
data-testid="field-required-checkbox"
label="Field is required"
isDisabled={properties.defaultReadOnly}
isChecked={required || false}
onChange={(_ev, checked) => {
setData('required', checked);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { Checkbox, FormGroup } from '@patternfly/react-core';
import { Checkbox, FormGroup, HelperText, HelperTextItem } from '@patternfly/react-core';
import { ConnectionTypeDataField } from '~/concepts/connectionTypes/types';
import ConnectionTypeDataFormField from '~/concepts/connectionTypes/fields/ConnectionTypeDataFormField';
import DataFieldAdvancedPropertiesForm from '~/pages/connectionTypes/manage/advanced/DataFieldAdvancedPropertiesForm';
Expand Down Expand Up @@ -45,6 +45,12 @@ const DataFieldPropertiesForm = <T extends ConnectionTypeDataField>({
value={field.properties.defaultValue}
data-testid="field-default-value"
/>
<HelperText>
<HelperTextItem>
Do not enter sensitive information. Default values are visible to users with access to
the connection type.
</HelperTextItem>
</HelperText>
<Checkbox
id="defaultReadOnly"
label="Default value is read-only"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ const ManageConnectionTypeFieldsTableRow: React.FC<Props> = ({
aria-label="toggle field required"
isChecked={row.required || false}
data-testid="field-required"
onChange={() => onChange({ ...row, required: !row.required })}
isDisabled={row.properties.defaultReadOnly}
onChange={
!row.properties.defaultReadOnly
? () => onChange({ ...row, required: !row.required })
: undefined
}
/>
</Td>
<Td isActionCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,28 @@ describe('ConnectionTypeDataFieldModal', () => {

expect(submitButton).not.toBeDisabled();
});

it('should unset required if default value is read-only', () => {
const field: ShortTextField = {
type: 'short-text',
name: 'test',
envVar: 'test_envvar',
required: true,
properties: {
defaultValue: 'default value',
defaultReadOnly: false,
},
};
render(
<ConnectionTypeDataFieldModal onClose={onClose} onSubmit={onSubmit} isEdit field={field} />,
);
const requiredCheckbox = screen.getByTestId('field-required-checkbox');
expect(requiredCheckbox).toBeChecked();
const readOnlyCheckbox = screen.getByTestId('field-default-value-readonly-checkbox');
expect(readOnlyCheckbox).not.toBeChecked();
act(() => {
fireEvent.click(requiredCheckbox);
});
expect(requiredCheckbox).not.toBeChecked();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,20 @@ describe('ManageConnectionTypeFieldsTableRow', () => {
expect(hasValidationIssueMock).toHaveBeenCalledTimes(1);
expect(hasValidationIssueMock).toHaveBeenCalledWith(['fields', 0, 'envVar'], 'envVar_conflict');
});

it('should not be able to set required if default value is read-only', () => {
const field: ShortTextField = {
type: 'short-text',
name: 'test',
envVar: 'test_envvar',
properties: {
defaultValue: 'default value',
defaultReadOnly: true,
},
};
render(renderRow({ row: field, fields: [field] }));
const requiredSwitch = screen.getByTestId('field-required');
expect(requiredSwitch).not.toBeChecked();
expect(requiredSwitch).toBeDisabled();
});
});
Loading