-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
model to manage field creation (#3100)
- Loading branch information
1 parent
c94f59d
commit cf22edf
Showing
34 changed files
with
727 additions
and
537 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
58 changes: 29 additions & 29 deletions
58
frontend/src/concepts/connectionTypes/fields/BooleanFormField.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,36 +1,36 @@ | ||
import * as React from 'react'; | ||
import { Checkbox } from '@patternfly/react-core'; | ||
import { BooleanField } from '~/concepts/connectionTypes/types'; | ||
import DataFormFieldGroup from '~/concepts/connectionTypes/fields/DataFormFieldGroup'; | ||
import { FieldProps } from '~/concepts/connectionTypes/fields/types'; | ||
|
||
type Props = { | ||
field: BooleanField; | ||
isPreview?: boolean; | ||
value?: boolean; | ||
onChange?: (value: boolean) => void; | ||
const BooleanFormField: React.FC<FieldProps<BooleanField>> = ({ | ||
id, | ||
field, | ||
mode, | ||
onChange, | ||
value, | ||
'data-testid': dataTestId, | ||
}) => { | ||
const isPreview = mode === 'preview'; | ||
return ( | ||
<Checkbox | ||
aria-readonly={isPreview} | ||
id={id} | ||
name={id} | ||
data-testid={dataTestId} | ||
label={field.properties.label} | ||
aria-label={field.properties.label || field.name} | ||
isDisabled={field.properties.defaultReadOnly} | ||
isChecked={ | ||
isPreview || field.properties.defaultReadOnly ? !!field.properties.defaultValue : !!value | ||
} | ||
onChange={ | ||
isPreview || field.properties.defaultReadOnly || !onChange | ||
? () => undefined | ||
: (_e, v) => onChange(v) | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
const BooleanFormField: React.FC<Props> = ({ field, isPreview, onChange, value }) => ( | ||
<DataFormFieldGroup field={field} isPreview={!!isPreview} renderDefaultValue={false}> | ||
{(id) => ( | ||
<Checkbox | ||
aria-readonly={isPreview} | ||
id={id} | ||
name={id} | ||
label={field.properties.label} | ||
aria-label={field.properties.label || field.name} | ||
isDisabled={field.properties.defaultReadOnly} | ||
isChecked={ | ||
isPreview || field.properties.defaultReadOnly ? !!field.properties.defaultValue : !!value | ||
} | ||
onChange={ | ||
isPreview || field.properties.defaultReadOnly || !onChange | ||
? () => undefined | ||
: (_e, v) => onChange(v) | ||
} | ||
/> | ||
)} | ||
</DataFormFieldGroup> | ||
); | ||
|
||
export default BooleanFormField; |
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
51 changes: 18 additions & 33 deletions
51
frontend/src/concepts/connectionTypes/fields/DataFormFieldGroup.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,41 +1,26 @@ | ||
import * as React from 'react'; | ||
import { FormGroup } from '@patternfly/react-core'; | ||
import { FormGroup, GenerateId } from '@patternfly/react-core'; | ||
import { ConnectionTypeDataField } from '~/concepts/connectionTypes/types'; | ||
import FormGroupText from '~/components/FormGroupText'; | ||
import UnspecifiedValue from '~/concepts/connectionTypes/fields/UnspecifiedValue'; | ||
import { defaultValueToString } from '~/concepts/connectionTypes/utils'; | ||
|
||
type Props<T extends ConnectionTypeDataField> = { | ||
field: T; | ||
isPreview: boolean; | ||
type Props = { | ||
field: ConnectionTypeDataField; | ||
children: (id: string) => React.ReactNode; | ||
renderDefaultValue?: boolean; | ||
}; | ||
|
||
const DataFormFieldGroup = <T extends ConnectionTypeDataField>({ | ||
field, | ||
isPreview, | ||
children, | ||
renderDefaultValue = true, | ||
}: Props<T>): React.ReactNode => { | ||
const id = `${field.type}-${field.envVar}`; | ||
return ( | ||
<FormGroup | ||
label={field.name} | ||
fieldId={id} | ||
data-testid={`field ${field.type} ${field.envVar}`} | ||
// do not mark read only fields as required | ||
isRequired={field.required && !field.properties.defaultReadOnly} | ||
> | ||
{field.properties.defaultReadOnly && renderDefaultValue ? ( | ||
<FormGroupText id={id}> | ||
{defaultValueToString(field) ?? (isPreview ? <UnspecifiedValue /> : '-')} | ||
</FormGroupText> | ||
) : ( | ||
children(id) | ||
)} | ||
</FormGroup> | ||
); | ||
}; | ||
const DataFormFieldGroup: React.FC<Props> = ({ field, children }): React.ReactNode => ( | ||
<GenerateId> | ||
{(id) => ( | ||
<FormGroup | ||
label={field.name} | ||
fieldId={id} | ||
data-testid={`field ${field.type} ${field.envVar}`} | ||
// do not mark read only fields as required | ||
isRequired={field.required && !field.properties.defaultReadOnly} | ||
> | ||
{children(id)} | ||
</FormGroup> | ||
)} | ||
</GenerateId> | ||
); | ||
|
||
export default DataFormFieldGroup; |
24 changes: 24 additions & 0 deletions
24
frontend/src/concepts/connectionTypes/fields/DefaultValueTextRenderer.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,24 @@ | ||
import * as React from 'react'; | ||
import FormGroupText from '~/components/FormGroupText'; | ||
import { FieldMode } from '~/concepts/connectionTypes/fields/types'; | ||
import UnspecifiedValue from '~/concepts/connectionTypes/fields/UnspecifiedValue'; | ||
import { ConnectionTypeDataField } from '~/concepts/connectionTypes/types'; | ||
import { defaultValueToString } from '~/concepts/connectionTypes/utils'; | ||
|
||
type Props = { | ||
id: string; | ||
field: ConnectionTypeDataField; | ||
mode?: FieldMode; | ||
children: React.ReactNode; | ||
}; | ||
|
||
const DefaultValueTextRenderer: React.FC<Props> = ({ id, field, mode, children }) => | ||
mode !== 'default' && field.properties.defaultReadOnly ? ( | ||
<FormGroupText id={id}> | ||
{defaultValueToString(field) ?? (mode === 'preview' ? <UnspecifiedValue /> : '-')} | ||
</FormGroupText> | ||
) : ( | ||
children | ||
); | ||
|
||
export default DefaultValueTextRenderer; |
Oops, something went wrong.