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

Allow list values to be raw strings. Fixes #289 #469

Open
wants to merge 2 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
13 changes: 9 additions & 4 deletions lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,14 @@ export class List extends Component {
}

for (let i = 0, len = this.state.value.length; i < len; i++) {
result = this.refs[i].validate();
errors = errors.concat(result.errors);
value.push(result.value);
const ref = this.refs[i];
if(ref) {
result = this.refs[i].validate();
errors = errors.concat(result.errors);
value.push(result.value);
} else {
value.push(this.state.value[i]);
}
}

// handle subtype
Expand All @@ -632,7 +637,7 @@ export class List extends Component {
}

onChange(value, keys, path, kind) {
const allkeys = toSameLength(value, keys, this.props.ctx.uidGenerator);
const allkeys = toSameLength(value, keys || [], this.props.ctx.uidGenerator);
this.setState({ value, keys: allkeys, isPristine: false }, () => {
this.props.onChange(value, path, kind);
});
Expand Down
58 changes: 58 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,3 +1213,61 @@ test("List:should support unions", assert => {

assert.strictEqual(component.getItems()[0].input.props.type, UnknownAccount);
});

test("List:should support validation using string values", assert => {
let component = new List({
type: t.list(t.String),
ctx: ctx,
options: {},
value: [
"item 1",
"item 2"
]
});

// Stop warnings from being printed
component.setState = function() {};

try {
assert.plan(2);

const validationResult = component.validate();

assert.deepEqual(validationResult.value, [
"item 1",
"item 2"
]);
assert.deepEqual(validationResult.errors, []);
} catch(e) {
assert.fail("Should not throw an exception");
}
});

test("List:should support calling `onChange()` with no keys", assert => {
let component = new List({
type: t.list(t.String),
ctx: ctx,
options: {},
value: [
"item 1",
"item 2"
]
});

// Stop warnings from being printed
component.setState = function() {};

try {
assert.plan(1);

const newValue = [
"item 1",
"item 2",
"item 3"
]
component.onChange(newValue);
assert.pass("Value changed successfully");
} catch(e) {
assert.fail("Should not throw an exception");
}
});