-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pass custom editors on the fly via
customEditorInterfaces
pro…
…perty (#1454) * feat: added custom editor interfaces property to jsonform * feat: added story and test for custom input editor * chores added a small comment * chores: fix lint * chore: improve docs * chore: copy class to test * chore: import missing moduel --------- Co-authored-by: silvester-pari <[email protected]>
- Loading branch information
1 parent
0efb0b8
commit d68e7e6
Showing
10 changed files
with
266 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* Custom editor interface for eox-jsonform | ||
*/ | ||
import { html } from "lit"; | ||
import { JSONEditor } from "@json-editor/json-editor/src/core.js"; | ||
|
||
class CustomEditorExample extends JSONEditor.AbstractEditor { | ||
build() { | ||
super.build(); | ||
|
||
// control | ||
this.control = document.createElement("div"); | ||
|
||
// Create a select element | ||
this.input = document.createElement("select"); | ||
this.input.setAttribute("id", this.path + "test"); | ||
this.input.classList.add("form-select"); | ||
|
||
const options = [ | ||
{ | ||
value: "true", | ||
label: "True", | ||
data: true, | ||
color: "green", | ||
}, | ||
{ | ||
value: "false", | ||
label: "False", | ||
data: false, | ||
color: "red", | ||
}, | ||
]; | ||
|
||
// build options | ||
options.forEach((option) => { | ||
const optionElement = document.createElement("option"); | ||
optionElement.setAttribute("value", option.value); | ||
optionElement.textContent = option.label; | ||
optionElement.style.background = option.color; | ||
optionElement.style.color = "white"; | ||
if (this.defaults.startVals[this.key] === option.data) { | ||
optionElement.selected = true; | ||
} | ||
this.input.appendChild(optionElement); | ||
}); | ||
|
||
// label | ||
this.label = document.createElement("label"); | ||
this.label.setAttribute("for", this.path + "test"); | ||
this.label.classList.add("form-check-label"); | ||
this.label.textContent = this.schema.title; | ||
this.label.style.fontStyle = "italic"; | ||
|
||
// appends | ||
this.container.appendChild(this.control); | ||
this.control.appendChild(this.label); | ||
this.control.appendChild(this.input); | ||
} | ||
|
||
postBuild() { | ||
super.postBuild(); | ||
|
||
this.input.addEventListener("change", (e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
this.value = e.currentTarget.checked; | ||
this.onChange(true); | ||
}); | ||
} | ||
} | ||
|
||
const customEditorInterfaces = { | ||
args: { | ||
schema: { | ||
properties: { | ||
test: { | ||
type: "boolean", | ||
title: "Normal Boolean Editor Field", | ||
}, | ||
test2: { | ||
type: "boolean", | ||
format: "custom", | ||
title: "Custom Boolean Editor Field", | ||
}, | ||
}, | ||
}, | ||
value: { | ||
test: true, | ||
test2: true, | ||
}, | ||
customEditorInterfaces: [ | ||
{ | ||
type: "boolean", | ||
format: "custom", | ||
func: CustomEditorExample, | ||
}, | ||
], | ||
}, | ||
render: (args) => html` | ||
<eox-jsonform | ||
.schema=${args.schema} | ||
.value=${args.value} | ||
.noShadow=${false} | ||
.unstyled=${args.unstyled} | ||
.customEditorInterfaces=${args.customEditorInterfaces} | ||
@change=${args.onChange} | ||
></eox-jsonform> | ||
`, | ||
}; | ||
|
||
export default customEditorInterfaces; |
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
112 changes: 112 additions & 0 deletions
112
elements/jsonform/test/cases/load-custom-editor-interface.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,112 @@ | ||
import { html } from "lit"; | ||
import { JSONEditor } from "@json-editor/json-editor/src/core.js"; | ||
import { TEST_SELECTORS } from "../../src/enums"; | ||
|
||
class CustomEditorExample extends JSONEditor.AbstractEditor { | ||
build() { | ||
super.build(); | ||
|
||
// control | ||
this.control = document.createElement("div"); | ||
|
||
// Create a select element | ||
this.input = document.createElement("select"); | ||
this.input.setAttribute("id", this.path + "test"); | ||
this.input.classList.add("form-select"); | ||
|
||
const options = [ | ||
{ | ||
value: "true", | ||
label: "True", | ||
data: true, | ||
color: "green", | ||
}, | ||
{ | ||
value: "false", | ||
label: "False", | ||
data: false, | ||
color: "red", | ||
}, | ||
]; | ||
|
||
// build options | ||
options.forEach((option) => { | ||
const optionElement = document.createElement("option"); | ||
optionElement.setAttribute("value", option.value); | ||
optionElement.textContent = option.label; | ||
optionElement.style.background = option.color; | ||
optionElement.style.color = "white"; | ||
if (this.defaults.startVals[this.key] === option.data) { | ||
optionElement.selected = true; | ||
} | ||
this.input.appendChild(optionElement); | ||
}); | ||
|
||
// label | ||
this.label = document.createElement("label"); | ||
this.label.setAttribute("for", this.path + "test"); | ||
this.label.classList.add("form-check-label"); | ||
this.label.textContent = this.schema.title; | ||
this.label.style.fontStyle = "italic"; | ||
|
||
// appends | ||
this.container.appendChild(this.control); | ||
this.control.appendChild(this.label); | ||
this.control.appendChild(this.input); | ||
} | ||
|
||
postBuild() { | ||
super.postBuild(); | ||
|
||
this.input.addEventListener("change", (e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
this.value = e.currentTarget.checked; | ||
this.onChange(true); | ||
}); | ||
} | ||
} | ||
|
||
// Destructure TEST_SELECTORS object | ||
const { jsonForm } = TEST_SELECTORS; | ||
const schema = { | ||
properties: { | ||
test: { | ||
type: "boolean", | ||
format: "custom", | ||
title: "Custom Editor Field", | ||
}, | ||
}, | ||
}; | ||
|
||
const loadCustomEditorInterfaceTest = () => { | ||
cy.mount( | ||
html`<eox-jsonform | ||
.customEditorInterfaces=${[ | ||
{ | ||
type: "boolean", | ||
format: "custom", | ||
func: CustomEditorExample, | ||
}, | ||
]} | ||
.schema=${schema} | ||
.value=${{ | ||
test: false, | ||
}} | ||
></eox-jsonform>`, | ||
).as(jsonForm); | ||
cy.get(jsonForm) | ||
.shadow() | ||
.within(() => { | ||
cy.get("select").should("exist"); | ||
cy.get("label.form-check-label").should( | ||
"have.text", | ||
schema.properties.test.title, | ||
); | ||
cy.get("select").should("have.value", "false"); | ||
cy.get("select").select("true"); | ||
cy.get("select").should("have.value", "true"); | ||
}); | ||
}; | ||
|
||
export default loadCustomEditorInterfaceTest; |
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
Oops, something went wrong.