-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core/forms): Extract a controlled MapEditorInput component
Retains the old API in MapEditor.tsx
- Loading branch information
1 parent
29d470d
commit 0c547a8
Showing
3 changed files
with
204 additions
and
149 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './mapEditor/MapEditor'; | ||
export * from './mapEditor/MapEditorInput'; | ||
export * from './mapEditor/MapPair'; |
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
158 changes: 158 additions & 0 deletions
158
app/scripts/modules/core/src/forms/mapEditor/MapEditorInput.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,158 @@ | ||
import * as React from 'react'; | ||
import { isNil, isFunction, isString } from 'lodash'; | ||
|
||
import { IPipeline } from 'core/domain'; | ||
import { createFakeReactSyntheticEvent, IFormInputProps, IValidator } from '../../presentation/forms'; | ||
import { IMapPair, MapPair } from './MapPair'; | ||
|
||
export interface IMapEditorInputProps extends IFormInputProps { | ||
addButtonLabel?: string; | ||
hiddenKeys?: string[]; | ||
keyLabel?: string; | ||
label?: string; | ||
labelsLeft?: boolean; | ||
errors: IMapEditorModel; | ||
valueLabel?: string; | ||
valueCanContainSpel?: boolean; | ||
pipeline?: IPipeline; | ||
} | ||
|
||
export interface IMapEditorModel { | ||
[key: string]: string; | ||
} | ||
|
||
const duplicateKeyPattern = /^__MapEditorDuplicateKey__\d+__/; | ||
|
||
// Convert the controlled component value (an object) to a list of IMapPairs | ||
function objectToTuples(model: IMapEditorModel, errors: IMapEditorModel): IMapPair[] { | ||
model = model || {}; | ||
errors = errors || {}; | ||
return Object.keys(model).map(key => { | ||
const keyWithoutMagicString = key.split(duplicateKeyPattern).pop(); | ||
return { key: keyWithoutMagicString, value: model[key], error: errors[key] }; | ||
}); | ||
} | ||
|
||
// Convert a list of IMapPairs to an object with keys and values | ||
// Prepend any duplicate keys with a magic string | ||
function tuplesToObject(pairs: IMapPair[]): IMapEditorModel { | ||
return pairs.reduce( | ||
(acc, pair, idx) => { | ||
// Cannot have duplicate keys in an object, so prepend a magic string to the key | ||
const key = isNil(acc[pair.key]) ? pair.key : `__MapEditorDuplicateKey__${idx}__${pair.key}`; | ||
return { ...acc, [key]: pair.value }; | ||
}, | ||
{} as IMapEditorModel, | ||
); | ||
} | ||
|
||
function validator(values: IMapEditorModel): IMapEditorModel { | ||
return Object.keys(values || {}).reduce((acc, key) => { | ||
return duplicateKeyPattern.exec(key) ? { ...acc, [key]: 'Duplicate key' } : acc; | ||
}, {}); | ||
} | ||
|
||
export function MapEditorInput({ | ||
addButtonLabel = 'Add Field', | ||
errors = {}, | ||
hiddenKeys = [], | ||
keyLabel = 'Key', | ||
label, | ||
labelsLeft = false, | ||
name, | ||
onChange, | ||
value, | ||
valueLabel = 'Value', | ||
valueCanContainSpel = false, | ||
validation, | ||
pipeline, | ||
}: IMapEditorInputProps) { | ||
const rowProps = { keyLabel, valueLabel, labelsLeft }; | ||
|
||
const columnCount = labelsLeft ? 5 : 3; | ||
const tableClass = label ? '' : 'no-border-top'; | ||
const isParameterized = isString(value); | ||
const backingModel = !isString(value) ? objectToTuples(value, errors) : null; | ||
|
||
// Register/unregister validator, if a validation prop was supplied | ||
React.useEffect(() => { | ||
if (validation && isFunction(validation.addValidator)) { | ||
validation.addValidator((validator as any) as IValidator); | ||
} | ||
|
||
return () => { | ||
if (validation && isFunction(validation.removeValidator)) { | ||
validation.removeValidator((validator as any) as IValidator); | ||
} | ||
}; | ||
}, []); | ||
|
||
const handleChanged = () => { | ||
onChange(createFakeReactSyntheticEvent({ name, value: tuplesToObject(backingModel) })); | ||
}; | ||
|
||
const handlePairChanged = (newPair: IMapPair, index: number) => { | ||
backingModel[index] = newPair; | ||
handleChanged(); | ||
}; | ||
|
||
const handleDeletePair = (index: number) => { | ||
backingModel.splice(index, 1); | ||
handleChanged(); | ||
}; | ||
|
||
const handleAddPair = () => { | ||
backingModel.push({ key: '', value: '' }); | ||
handleChanged(); | ||
}; | ||
|
||
return ( | ||
<div> | ||
{label && ( | ||
<div className="sm-label-left"> | ||
<b>{label}</b> | ||
</div> | ||
)} | ||
|
||
{isParameterized && <input className="form-control input-sm" value={value as string} />} | ||
{!isParameterized && ( | ||
<table className={`table table-condensed packed tags ${tableClass}`}> | ||
<thead> | ||
{!labelsLeft && ( | ||
<tr> | ||
<th>{keyLabel}</th> | ||
<th>{valueLabel}</th> | ||
<th /> | ||
</tr> | ||
)} | ||
</thead> | ||
<tbody> | ||
{backingModel | ||
.filter(p => !hiddenKeys.includes(p.key)) | ||
.map((pair, index) => ( | ||
<MapPair | ||
key={index} | ||
{...rowProps} | ||
onChange={x => handlePairChanged(x, index)} | ||
onDelete={() => handleDeletePair(index)} | ||
pair={pair} | ||
valueCanContainSpel={valueCanContainSpel} | ||
pipeline={pipeline} | ||
/> | ||
))} | ||
</tbody> | ||
<tfoot> | ||
<tr> | ||
<td colSpan={columnCount}> | ||
<button type="button" className="btn btn-block btn-sm add-new" onClick={handleAddPair}> | ||
<span className="glyphicon glyphicon-plus-sign" /> | ||
{addButtonLabel} | ||
</button> | ||
</td> | ||
</tr> | ||
</tfoot> | ||
</table> | ||
)} | ||
</div> | ||
); | ||
} |