-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#375 Make country renderer and region renderer as custom field render…
…er components
- Loading branch information
1 parent
a397e63
commit 8768af1
Showing
9 changed files
with
113 additions
and
56 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,49 @@ | ||
import { | ||
ConfigMultiline, | ||
ConfigTextInput, | ||
ConfigSelectInput, | ||
} from '../components/common/Form'; | ||
import { _emptyFunc } from '../utils'; | ||
import { FieldType } from '../utils/field'; | ||
|
||
export class FieldRenderer { | ||
rendererList = []; | ||
|
||
constructor() { | ||
this.#registerDefaultRenderers(); | ||
} | ||
|
||
register(renderer, sortOrder = 100, conditionCallback = _emptyFunc()) { | ||
const newRenderer = { | ||
renderer, | ||
sortOrder, | ||
canRenderField: conditionCallback, | ||
}; | ||
this.rendererList = [...this.rendererList, newRenderer].sort( | ||
(renderer1, renderer2) => renderer1.sortOrder - renderer2.sortOrder | ||
); | ||
} | ||
|
||
getRendererForField(field) { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const renderer of this.rendererList) { | ||
if (renderer.canRenderField(field)) { | ||
return renderer.renderer; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
#registerDefaultRenderers() { | ||
this.register(ConfigTextInput, 350, (field) => | ||
FieldType.isText(field.type) | ||
); | ||
this.register(ConfigSelectInput, 400, (field) => | ||
FieldType.isSelect(field.type) | ||
); | ||
this.register(ConfigMultiline, 300, (field) => | ||
FieldType.isMultiline(field.type) | ||
); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ts/address/components/CountryRenderer.jsx → ...nents/countryRenderer/CountryRenderer.jsx
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
3 changes: 3 additions & 0 deletions
3
src/reactapp/src/fieldRenderers/components/countryRenderer/condition.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,3 @@ | ||
export default function countryRendererCondition(field) { | ||
return field.code === 'country_id'; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/reactapp/src/fieldRenderers/components/countryRenderer/index.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,4 @@ | ||
import CountryRenderer from './CountryRenderer'; | ||
|
||
export default CountryRenderer; | ||
export { default as countryRendererCondition } from './condition'; |
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,31 @@ | ||
import CountryRenderer, { | ||
countryRendererCondition, | ||
} from './components/countryRenderer'; | ||
import RegionRenderer, { regionRendererCondition } from './regionRenderer'; | ||
|
||
/** | ||
* Holds custom renderers. | ||
* | ||
* renderer: React component that will be used to render the field. | ||
* | ||
* sortOrder: This defines the priority of the renderer component. All registered | ||
* components have a sort order associated with it. When a field looks for | ||
* a renderer, it loops through all the registered renderers and find a | ||
* suitable renderer that can render the field based on the "condition". | ||
* Lower the sort order, higher the chance to use the renderer. | ||
* | ||
* condition: This is a callback which determines whether the renderer can be | ||
* used to render the field. | ||
*/ | ||
export default [ | ||
{ | ||
renderer: CountryRenderer, | ||
sortOrder: 100, | ||
condition: countryRendererCondition, | ||
}, | ||
{ | ||
renderer: RegionRenderer, | ||
sortOrder: 200, | ||
condition: regionRendererCondition, | ||
}, | ||
]; |
9 changes: 6 additions & 3 deletions
9
...nts/address/components/RegionRenderer.jsx → ...nderers/regionRenderer/RegionRenderer.jsx
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function regionRendererCondition(field) { | ||
return field.code === 'region'; | ||
} |
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,5 @@ | ||
import RegionRenderer from './RegionRenderer'; | ||
|
||
export default RegionRenderer; | ||
|
||
export { default as regionRendererCondition } from './condition'; |
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,53 +1,12 @@ | ||
import { | ||
ConfigMultiline, | ||
ConfigTextInput, | ||
ConfigSelectInput, | ||
} from '../components/common/Form'; | ||
import { _emptyFunc } from '../utils'; | ||
import { FieldType } from '../utils/field'; | ||
import RegionRenderer from '../components/address/components/RegionRenderer'; | ||
import CountryRenderer from '../components/address/components/CountryRenderer'; | ||
|
||
class FieldRenderer { | ||
rendererList = []; | ||
|
||
constructor() { | ||
this.#registerDefaultRenderers(); | ||
} | ||
|
||
register(renderer, sortOrder = 100, conditionCallback = _emptyFunc()) { | ||
const newRenderer = { | ||
renderer, | ||
sortOrder, | ||
canRenderField: conditionCallback, | ||
}; | ||
this.rendererList = [...this.rendererList, newRenderer].sort( | ||
(renderer1, renderer2) => renderer1.sortOrder - renderer2.sortOrder | ||
); | ||
} | ||
|
||
getRendererForField(field) { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const renderer of this.rendererList) { | ||
if (renderer.canRenderField(field)) { | ||
return renderer.renderer; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
#registerDefaultRenderers() { | ||
this.register(CountryRenderer, 10, (field) => field.code === 'country_id'); | ||
this.register(RegionRenderer, 20, (field) => field.code === 'region'); | ||
this.register(ConfigTextInput, 30, (field) => FieldType.isText(field.type)); | ||
this.register(ConfigSelectInput, 10, (field) => | ||
FieldType.isSelect(field.type) | ||
); | ||
this.register(ConfigMultiline, 10, (field) => | ||
FieldType.isMultiline(field.type) | ||
); | ||
} | ||
} | ||
import customRenderers from './customRenderers'; | ||
import { FieldRenderer } from './FieldRenderer'; | ||
|
||
export const addressFieldRenderer = new FieldRenderer(); | ||
|
||
/** | ||
* Here adds custom field renderers for address form. | ||
*/ | ||
customRenderers.forEach((customrenderer) => { | ||
const { renderer, sortOrder, condition } = customrenderer; | ||
addressFieldRenderer.register(renderer, sortOrder, condition); | ||
}); |