Skip to content

Commit

Permalink
Merge pull request #1543 from germanbisurgi/feature/rating-fix
Browse files Browse the repository at this point in the history
WIP: Fix rating "undefined" value when type of integer
  • Loading branch information
germanbisurgi committed Apr 11, 2024
2 parents 0498ede + 5246dc1 commit 5a8c22d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/editors/starrating.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,24 @@ export class StarratingEditor extends StringEditor {
return undefined
}
if (this.schema.type === 'integer') {
return this.value === '' ? undefined : this.value * 1
return this.value === '' ? 0 : parseInt(this.value)
}
return this.value
}

setValue (value) {
this.value = value

for (let i = 0; i < this.radioGroup.length; i++) {
if (this.radioGroup[i].value === `${value}`) {
this.radioGroup[i].checked = true
this.value = value

if (this.options.displayValue) this.displayRating.innerHTML = this.value

this.onChange(true)
break
}
}

super.setValue(this.value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/codeceptjs/editors/rating_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global Feature Scenario */

Feature('rating')
Feature('readonly')

Scenario('should be disabled if "readonly" is specified @readOnly', async ({ I }) => {
I.amOnPage('read-only.html')
Expand Down
15 changes: 15 additions & 0 deletions tests/codeceptjs/editors/starrating_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* global Feature Scenario */

Feature('starrating')

Scenario('should set and get values properly @starrating', async ({ I }) => {
I.amOnPage('starrating.html')
I.waitForElement('.je-ready')
I.waitForValue('#value', '{"integer_rating":0,"string_rating":"","starrating":"Beginner","starrating2":"Beginner","starrating3":"5 Stars"}')
I.click('[for="root[integer_rating]3"]')
I.click('[for="root[string_rating]3"]')
I.click('[for="root[starrating]3"]')
I.click('[for="root[starrating2]3"]')
I.click('[for="root[starrating3]10"]')
I.waitForValue('#value', '{"integer_rating":3,"string_rating":"3","starrating":"Experienced","starrating2":"Experienced","starrating3":"10 Stars"}')
})
86 changes: 86 additions & 0 deletions tests/pages/starrating.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>rating</title>
<script src="../../dist/jsoneditor.js"></script>
<link rel="stylesheet" id="theme-link" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" id="iconlib-link" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css">
</head>
<body>

<div class="container">
<h1>Test</h1>
<div class="form-group">
<label for="value">Value</label>
<textarea id="value" class="form-control" rows="10"></textarea>
</div>

<div id='editor-container'></div>
</div>

<script>
const editorContainer = document.querySelector('#editor-container')
const value = document.querySelector('#value')
const schema = {
type: "object",
title: "Car",
properties: {
integer_rating: {
type: "integer",
format: "rating",
},
string_rating: {
type: "string",
format: "rating",
},
starrating: {
type: "string",
format: "starrating",
title: "Starrating",
description: 'Starrating',
enum: ["Beginner", "Skilled", "Experienced", "Advanced", "Expert"],
options: {
grid_columns: 2
}
},
starrating2: {
type: "string",
format: "starrating",
title: "Starrating",
description: 'Starrating with "displayValue=true"',
enum: ["Beginner", "Skilled", "Experienced", "Advanced", "Expert"],
options: {
grid_columns: 2,
displayValue: true
}
},
starrating3: {
type: "string",
format: "starrating",
title: "Starrating",
description: 'Starrating with "displayValue=true"',
default: "5 Stars",
enum: ["1 Star", "2 Stars", "3 Stars", "4 Stars", "5 Stars", "6 Stars", "7 Stars", "8 Stars", "9 Stars", "10 Stars"],
options: {
grid_columns: 4,
displayValue: true
}
}
}
}

const editor = new JSONEditor(editorContainer, {
schema: schema,
theme: 'bootstrap4',
iconlib: 'fontawesome',
show_errors: 'always'
})

editor.on('change', function () {
value.value = JSON.stringify(editor.getValue())
})
</script>

</body>
</html>
2 changes: 1 addition & 1 deletion tests/unit/editor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Editor', () => {
startval: 5
})
editor.promise.then(() => {
expect(editor.root.value).toBe(5)
expect(editor.root.getValue()).toBe(5)
})
})

Expand Down

0 comments on commit 5a8c22d

Please sign in to comment.