-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Use index for value in SelectWidget #1562
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,47 @@ | ||
module.exports = { | ||
schema: { | ||
definitions: { | ||
locations: { | ||
enumNames: ["New York", "Amsterdam", "Hong Kong"], | ||
enum: [ | ||
{ | ||
name: "New York", | ||
lat: 40, | ||
lon: 74, | ||
}, | ||
{ | ||
name: "Amsterdam", | ||
lat: 52, | ||
lon: 5, | ||
}, | ||
{ | ||
name: "Hong Kong", | ||
lat: 22, | ||
lon: 114, | ||
}, | ||
], | ||
}, | ||
}, | ||
type: "object", | ||
properties: { | ||
location: { | ||
title: "Location", | ||
$ref: "#/definitions/locations", | ||
}, | ||
multiSelect: { | ||
type: "array", | ||
uniqueItems: true, | ||
items: { | ||
$ref: "#/definitions/locations", | ||
}, | ||
}, | ||
}, | ||
}, | ||
formData: { | ||
location: { | ||
name: "Amsterdam", | ||
lat: 52, | ||
lon: 5, | ||
}, | ||
}, | ||
}; |
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,49 +1,47 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import _isEqual from "lodash/isEqual"; | ||
|
||
import { asNumber, guessType } from "../../utils"; | ||
|
||
const nums = new Set(["number", "integer"]); | ||
|
||
/** | ||
* This is a silly limitation in the DOM where option change event values are | ||
* always retrieved as strings. | ||
*/ | ||
function processValue(schema, value) { | ||
// "enum" is a reserved word, so only "type" and "items" can be destructured | ||
const { type, items } = schema; | ||
if (value === "") { | ||
function getValue(event, enumOptions, multiple) { | ||
if (event.target.value === "") { | ||
return undefined; | ||
} else if (type === "array" && items && nums.has(items.type)) { | ||
return value.map(asNumber); | ||
} else if (type === "boolean") { | ||
return value === "true"; | ||
} else if (type === "number") { | ||
return asNumber(value); | ||
} | ||
|
||
// If type is undefined, but an enum is present, try and infer the type from | ||
// the enum values | ||
if (schema.enum) { | ||
if (schema.enum.every(x => guessType(x) === "number")) { | ||
return asNumber(value); | ||
} else if (schema.enum.every(x => guessType(x) === "boolean")) { | ||
return value === "true"; | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
|
||
function getValue(event, multiple) { | ||
if (multiple) { | ||
return [].slice | ||
.call(event.target.options) | ||
.filter(o => o.selected) | ||
.map(o => o.value); | ||
.map(o => enumOptions[o.value].value); | ||
} else { | ||
return event.target.value; | ||
return enumOptions[event.target.value].value; | ||
} | ||
} | ||
|
||
function getOneSelected(value, enumOptions) { | ||
if (typeof value === "undefined") { | ||
return ""; | ||
} | ||
|
||
for (const i in enumOptions) { | ||
if (_isEqual(enumOptions[i].value, value)) { | ||
return i; | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
function getMultipleSelected(values, enumOptions) { | ||
if (!Array.isArray(values)) { | ||
return []; | ||
} | ||
let selected = []; | ||
for (const i in enumOptions) { | ||
for (const value of values) { | ||
if (_isEqual(enumOptions[i].value, value)) { | ||
selected.push(i); | ||
} | ||
} | ||
} | ||
return selected; | ||
} | ||
|
||
function SelectWidget(props) { | ||
|
@@ -63,41 +61,40 @@ function SelectWidget(props) { | |
placeholder, | ||
} = props; | ||
const { enumOptions, enumDisabled } = options; | ||
const emptyValue = multiple ? [] : ""; | ||
const selected = multiple | ||
? getMultipleSelected(value, enumOptions) | ||
: getOneSelected(value, enumOptions); | ||
return ( | ||
<select | ||
id={id} | ||
multiple={multiple} | ||
className="form-control" | ||
value={typeof value === "undefined" ? emptyValue : value} | ||
value={selected} | ||
required={required} | ||
disabled={disabled || readonly} | ||
autoFocus={autofocus} | ||
onBlur={ | ||
onBlur && | ||
(event => { | ||
const newValue = getValue(event, multiple); | ||
onBlur(id, processValue(schema, newValue)); | ||
onBlur(id, getValue(event, enumOptions, multiple)); | ||
}) | ||
} | ||
onFocus={ | ||
onFocus && | ||
(event => { | ||
const newValue = getValue(event, multiple); | ||
onFocus(id, processValue(schema, newValue)); | ||
onFocus(id, getValue(event, enumOptions, multiple)); | ||
}) | ||
} | ||
onChange={event => { | ||
const newValue = getValue(event, multiple); | ||
onChange(processValue(schema, newValue)); | ||
onChange(getValue(event, enumOptions, multiple)); | ||
}}> | ||
{!multiple && schema.default === undefined && ( | ||
<option value="">{placeholder}</option> | ||
)} | ||
{enumOptions.map(({ value, label }, i) => { | ||
const disabled = enumDisabled && enumDisabled.indexOf(value) != -1; | ||
return ( | ||
<option key={i} value={value} disabled={disabled}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is likely a breaking change because it changes what's rendered in the DOM (the |
||
<option key={i} value={i} disabled={disabled}> | ||
{label} | ||
</option> | ||
); | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why return an empty string by default?
Could we just return
undefined
instead -- so that it would also be possible to select""
? (an additional improvement from the old code)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds like a good idea, will look into it