Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value in select from schema property #1561

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class AbstractEditor {

this.parent = options.parent
this.key = this.parent !== undefined ? this.path.split('.').slice(this.parent.path.split('.').length).join('.') : this.path
this.defaultValue = options.jsoneditor.schema.property ? options.jsoneditor.schema.property[this.key] : undefined
this.defaultValueIsEmpty = this.defaultValue === undefined || this.defaultValue === null

this.link_watchers = []
this.watchLoop = false
Expand Down
27 changes: 22 additions & 5 deletions src/editors/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ import { extend } from '../utilities.js'
export class SelectEditor extends AbstractEditor {
setValue (value, initial) {
/* Sanitize value before setting it */
let sanitized = this.typecast(value)
let sanitized = this.typecast(this.defaultValueIsEmpty ? value : this.defaultValue)
const inEnum = (this.enum_options.length > 0 && this.enum_values.includes(sanitized))

const haveToUseDefaultValue = !!this.jsoneditor.options.use_default_values || typeof this.schema.default !== 'undefined'

if (!this.hasPlaceholderOption && (!inEnum || (initial && !this.isRequired() && !haveToUseDefaultValue))) {
if (this.defaultValueIsEmpty && !this.hasPlaceholderOption && (!inEnum || (initial && !this.isRequired() && !haveToUseDefaultValue))) {
sanitized = this.enum_values[0]
}

if (!this.defaultValueIsEmpty) {
this.value = sanitized
if (this.schema.type === 'boolean') {
this.input.value = this.value === false ? '' : '1'
} else {
this.input.value = this.value
}
}

if (this.value === sanitized) return

const selectedIndex = this.enum_values.indexOf(sanitized)

if (inEnum && selectedIndex !== -1) {
if (inEnum && selectedIndex !== -1 && this.defaultValueIsEmpty) {
this.input.value = this.enum_options[selectedIndex]
} else if (this.hasPlaceholderOption) {
} else if (this.hasPlaceholderOption && this.defaultValueIsEmpty) {
this.input.value = '_placeholder_'
} else {
this.input.value = sanitized
Expand Down Expand Up @@ -199,7 +208,15 @@ export class SelectEditor extends AbstractEditor {
this.control = this.theme.getFormControl(this.label, this.input, this.description, this.infoButton, this.formname)
this.container.appendChild(this.control)

this.value = this.enum_values[0]
if (this.defaultValueIsEmpty) {
this.value = this.enum_values[0]
} else {
if (this.schema.type === 'boolean') {
this.value = this.defaultValue === false ? '' : '1'
} else {
this.value = this.defaultValue
}
}

/* Any special formatting that needs to happen after the input is added to the dom */
window.requestAnimationFrame(() => {
Expand Down
Loading