Skip to content

Commit 7d90afe

Browse files
committed
Add enum renderer (select)
1 parent 535fc4f commit 7d90afe

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/renderers/StringEnum.tsx

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as React from "react";
2+
3+
import WithDescription from "./utils/WithDescription";
4+
import WithErrors from "./utils/WithErrors";
5+
import WithMargins from "./utils/WithMargins";
6+
7+
import { SimpleNode } from "@blinkforms/core/simpleNodes";
8+
9+
import {
10+
FormContext,
11+
NodeStringSchema,
12+
} from "@blinkforms/core/schemaTypes";
13+
14+
export default class StringEnum extends SimpleNode<string, NodeStringSchema> {
15+
getInitialValue() {
16+
return "";
17+
}
18+
19+
renderSimple(value: string, context: FormContext) {
20+
const enumLabels = this.getSchema().enumLabels || null;
21+
const enumOpts = (this.getSchema().enum || []).map(value => {
22+
let oLabel = value;
23+
let oVal = value;
24+
25+
if (enumLabels) {
26+
const lbSpec = enumLabels.find(opt => opt.name === value);
27+
if (lbSpec) {
28+
oLabel = lbSpec.label;
29+
oVal = lbSpec.name || value;
30+
}
31+
}
32+
33+
return ({
34+
name: oVal,
35+
label: oLabel,
36+
});
37+
});
38+
39+
const selectedValue = enumOpts.find(opt => opt.name === value) || null;
40+
return (
41+
<WithMargins parent={this}>
42+
<WithErrors parent={this} context={context}>
43+
<WithDescription parent={this}>
44+
<select
45+
onChange={(event) => this.setState({ value: event.target.value })}
46+
>
47+
{enumOpts.map((item, index) => {
48+
return (
49+
<option
50+
key={`opt-${item.name+''}-${index}`}
51+
value={item.name}
52+
selected={selectedValue === item.name && item.name !== null && typeof item.name !== 'undefined'}
53+
>
54+
{item.label}
55+
</option>
56+
);
57+
})}
58+
</select>
59+
</WithDescription>
60+
</WithErrors>
61+
</WithMargins>
62+
);
63+
}
64+
}

src/renderers/defaultReactHandlerProvider.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ObjectDefault from "./ObjectDefault";
22

33
import StringDefault from "./StringDefault";
44
import StringNotice from "./StringNotice";
5+
import StringEnum from "./StringEnum";
56

67
import ArrayDefault from "./ArrayDefault";
78
import ArrayTuple from "./ArrayTuple";
@@ -13,6 +14,7 @@ export default {
1314
STRING: {
1415
default: StringDefault,
1516
notice: StringNotice,
17+
enum: StringEnum,
1618
},
1719
OBJECT: {
1820
default: ObjectDefault,

stories/basicForm.stories.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ storiesOf('Basic form', module)
2323
},
2424
"lastName": {
2525
type: "string",
26+
ui: "enum",
2627
title: "Your last name",
27-
minLength: 3,
28+
enum: [
29+
"Name1",
30+
"Name2",
31+
"Name3",
32+
],
2833
},
2934
"notice": {
3035
type: "string",

0 commit comments

Comments
 (0)