-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converts Catalogs > Catalog > Add/Edit to react forms
- Loading branch information
Showing
8 changed files
with
166 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Grid } from 'patternfly-react'; | ||
import MiqFormRenderer from '../../forms/data-driven-form'; | ||
import createSchema from './create-form-schema'; | ||
|
||
|
||
class CatalogForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
const { catalogItems } = this.props; | ||
this.state = { | ||
schema: createSchema(catalogItems), | ||
initialValues: { | ||
name: props.catalogName, | ||
description: props.catalogDescription, | ||
duallist: {}, | ||
}, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
} | ||
|
||
render() { | ||
const { catalogName, catalogId } = this.props; | ||
const cancelUrl = `/catalog/st_catalog_edit/${catalogId}?button=cancel`; | ||
return ( | ||
<Grid fluid> | ||
<MiqFormRenderer | ||
initialValues={this.state.initialValues} | ||
schema={this.state.schema} | ||
onSubmit={console.log} | ||
onCancel={() => miqAjaxButton(cancelUrl)} | ||
onReset={() => add_flash(__('All changes have been reset'), 'warn')} | ||
canReset={!!catalogName} | ||
buttonsLabels={{ | ||
submitLabel: catalogName ? __('Save') : __('Add'), | ||
}} | ||
/> | ||
</Grid> | ||
); | ||
} | ||
} | ||
|
||
CatalogForm.propTypes = { | ||
catalogName: PropTypes.string, | ||
catalogDescription: PropTypes.string, | ||
catalogItems: PropTypes.shape({ | ||
[PropTypes.string]: PropTypes.string, | ||
}), | ||
catalogId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
}; | ||
|
||
CatalogForm.defaultProps = { | ||
catalogName: undefined, | ||
catalogDescription: undefined, | ||
catalogItems: {}, | ||
catalogId: undefined, | ||
}; | ||
|
||
export default CatalogForm; |
48 changes: 48 additions & 0 deletions
48
app/javascript/components/catalog-form/create-form-schema.js
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,48 @@ | ||
import { componentTypes, validatorTypes } from '@data-driven-forms/react-form-renderer'; | ||
|
||
function createSchema(options = {}) { | ||
const fields = [{ | ||
component: componentTypes.SUB_FORM, | ||
title: __('Basic Info'), | ||
fields: [{ | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'name', | ||
validate: [{ | ||
type: validatorTypes.REQUIRED, | ||
message: __("Name can't be blank"), | ||
}], | ||
label: __('Name'), | ||
maxLength: 40, | ||
autoFocus: true, | ||
}, { | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'description', | ||
label: __('Description'), | ||
maxLength: 60, | ||
}], | ||
}, { | ||
component: 'hr', | ||
}, { | ||
component: componentTypes.SUB_FORM, | ||
title: __('Assign Catalog Items'), | ||
fields: [ | ||
{ | ||
component: 'dual-list-select', | ||
leftTitle: __('Unassigned:'), | ||
rightTitle: __('Selected:'), | ||
leftId: 'available_fields', | ||
rightId: 'selected_fields', | ||
allToRight: false, | ||
moveLeftTitle: __('Move Selected buttons left'), | ||
moveRightTitle: __('Move Selected buttons right'), | ||
size: 8, | ||
assignFieldProvider: true, | ||
options, | ||
name: 'duallist', | ||
}, | ||
], | ||
}]; | ||
return { fields }; | ||
} | ||
|
||
export default createSchema; |
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,22 +1,18 @@ | ||
export const isEmpty = (obj) => { | ||
if (obj === '') { return true; } | ||
return (Object.getOwnPropertyNames(obj).length === 0); | ||
}; | ||
export const isEmpty = obj => (obj.length === 0); | ||
|
||
/** *********** HELPERS FOR FORMS *********** */ | ||
/* returns left values of dual list select */ | ||
export const leftValues = (options, value) => Object.keys(options).filter(key => !value[key]).reduce((acc, curr) => ({ | ||
...acc, | ||
[curr]: options[curr], | ||
}), {}); | ||
|
||
export const getKeys = value => (Array.isArray(value) ? value.map(({ key }) => key) : []); | ||
|
||
export const leftValues = (options, value) => options.filter(({ key }) => !getKeys(value).includes(key)); | ||
|
||
/* returns left submitted keys of dual list select */ | ||
export const leftSubmittedKeys = (originalOptions, value) => Object.keys(originalOptions).filter(e => !value[e]); | ||
export const leftSubmittedValues = (originalOptions, value) => originalOptions.filter(({ key }) => !getKeys(value).includes(key)); | ||
|
||
/* returns new left keys (added to left) of dual list select */ | ||
export const addedLeftKeys = (originalOptions, value, originalLeftValues) => ( | ||
leftSubmittedKeys(originalOptions, value).filter(e => !originalLeftValues[e]) | ||
export const addedLeftValues = (originalOptions, value, originalLeftValues) => ( | ||
leftSubmittedValues(originalOptions, value).filter(({ key }) => !getKeys(originalLeftValues).includes(key)) | ||
); | ||
|
||
/* return new right keys (added to right) of dual list select */ | ||
export const addedRightKeys = (value, originalRightValues) => Object.keys(value).filter(e => !originalRightValues[e]); | ||
export const addedRighValues = (value, originalRightValues) => value.filter(({ key }) => !getKeys(originalRightValues).includes(key)); |
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
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,9 +1,11 @@ | ||
import React from 'react'; | ||
import { formFieldsMapper } from '@data-driven-forms/pf3-component-mapper'; | ||
import DualListSelect from '../../components/dual-list-select'; | ||
import DualListSelect from '../../components/dual-list-select'; | ||
|
||
const fieldsMapper = { | ||
...formFieldsMapper, | ||
'dual-list-select': DualListSelect, | ||
hr: () => <hr />, | ||
}; | ||
|
||
export default fieldsMapper; |
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