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

Use index for value in SelectWidget #1562

Closed
wants to merge 3 commits into from
Closed
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
47 changes: 47 additions & 0 deletions packages/core/playground/samples/enumObjects.js
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,
},
},
};
85 changes: 41 additions & 44 deletions packages/core/src/components/widgets/SelectWidget.js
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 "";
Copy link
Member

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)

Copy link
Author

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

}

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) {
Expand All @@ -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}>
Copy link
Member

Choose a reason for hiding this comment

The 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 value)... but it's fine to go into version 2, and worth it to allow for better selection of any kinds of enums.

<option key={i} value={i} disabled={disabled}>
{label}
</option>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/src/samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import arrays from "./arrays";
import anyOf from "./anyOf";
import oneOf from "./oneOf";
import allOf from "./allOf";
import enumObjects from "./enumObjects";
import nested from "./nested";
import numbers from "./numbers";
import simple from "./simple";
Expand Down Expand Up @@ -52,6 +53,7 @@ export const samples = {
"Any Of": anyOf,
"One Of": oneOf,
"All Of": allOf,
"Enumerated objects": enumObjects,
"Null fields": nullField,
Nullable: nullable,
ErrorSchema: errorSchema,
Expand Down