Skip to content

Commit e963713

Browse files
committed
Some code improvements
1 parent e489e53 commit e963713

File tree

8 files changed

+434
-438
lines changed

8 files changed

+434
-438
lines changed

src/components/buttons.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function Button({className, ...props}) {
2+
if (!className)
3+
className = '';
4+
5+
className = 'rjf-' + className + '-button';
6+
return (
7+
<button
8+
className={className}
9+
type="button"
10+
{...props}
11+
>
12+
{props.children}
13+
</button>
14+
);
15+
}

src/components/containers.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Button from './buttons';
2+
3+
4+
export function GroupTitle(props) {
5+
if (!props.children)
6+
return null;
7+
8+
return (
9+
<div className="rjf-form-group-title">{props.children}</div>
10+
);
11+
}
12+
13+
14+
export function FormRow(props) {
15+
return (
16+
<div className="rjf-form-row">
17+
{props.onRemove &&
18+
<Button
19+
className="remove"
20+
onClick={(e) => props.onRemove(name)}
21+
title="Remove"
22+
>
23+
&times;
24+
</Button>
25+
}
26+
<div className="rjf-form-row-inner">
27+
{props.children}
28+
</div>
29+
</div>
30+
);
31+
}
32+
33+
34+
export function FormGroup(props) {
35+
let innerClassName = props.level === 0 && !React.Children.count(props.children)
36+
? ""
37+
: "rjf-form-group-inner";
38+
39+
return (
40+
<div className="rjf-form-group">
41+
{props.level === 0 && <GroupTitle>{props.schema.title}</GroupTitle>}
42+
<div className={innerClassName}>
43+
{props.level > 0 && <GroupTitle>{props.schema.title}</GroupTitle>}
44+
{props.children}
45+
{props.addable &&
46+
<Button
47+
className="add"
48+
onClick={(e) => props.onAdd()}
49+
title="Add new"
50+
>
51+
Add more
52+
</Button>
53+
}
54+
</div>
55+
</div>
56+
);
57+
}

src/components/form.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export function FormInput({label, help_text, error, ...props}) {
2+
3+
if (props.type === 'string')
4+
props.type = 'text'
5+
6+
return (
7+
<div>
8+
{label && <label>{label}</label>}
9+
<input {...props} />
10+
</div>
11+
);
12+
}
13+
14+
15+
export function CheckInput({label, help_text, error, value, ...props}) {
16+
17+
if (!label)
18+
label = props.name.toUpperCase();
19+
20+
if (props.type === 'bool')
21+
props.type = 'checkbox';
22+
23+
if (props.checked === undefined)
24+
props.checked = value;
25+
26+
if (props.checked === '')
27+
props.checked = false
28+
29+
return (
30+
<div>
31+
<label><input {...props} /> {label}</label>
32+
</div>
33+
);
34+
}
35+
36+
37+
export function FileInput(props) {
38+
return <FormInput {...props} />
39+
}

src/components/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Button from './buttons';
2+
import {FormInput, CheckInput, FileInput} from './form';
3+
import {FormRow, FormGroup} from './containers';
4+
5+
export {
6+
Button,
7+
FormInput, CheckInput, FileInput,
8+
FormRow, FormGroup
9+
};

src/data.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export function getBlankObject(schema) {
2+
let keys = {};
3+
4+
for (let key in schema.keys) {
5+
let value = schema.keys[key];
6+
let type = value.type;
7+
8+
if (type === 'string')
9+
keys[key] = '';
10+
else if (type === 'array')
11+
keys[key] = getBlankArray(value);
12+
else if (type === 'object')
13+
keys[key] = getBlankObject(value);
14+
}
15+
16+
return keys;
17+
}
18+
19+
20+
export function getBlankArray(schema) {
21+
let items = [];
22+
let type = schema.items.type;
23+
24+
if (type === 'string')
25+
items.push('');
26+
else if (type === 'array')
27+
items.push(getBlankArray(schema.items))
28+
else if (type === 'object')
29+
items.push(getBlankObject(schema.items));
30+
31+
return items;
32+
}
33+
34+
35+
export function getBlankData(schema) {
36+
if (schema.type === 'array') {
37+
return getBlankArray(schema);
38+
}
39+
else if (schema.type === 'object') {
40+
return getBlankObject(schema);
41+
} else if (schema.type === 'string') {
42+
return '';
43+
}
44+
}
45+
46+
47+
export function getBlankItem(schema) {
48+
let dataObject = {};
49+
50+
for (let key in schema.fields) {
51+
if (!schema.fields.hasOwnProperty(key))
52+
continue;
53+
54+
let item = schema.fields[key];
55+
56+
dataObject[key] = '';
57+
}
58+
59+
return dataObject;
60+
}
61+
62+
63+
export function getSyncedData(data, schema) {
64+
// adds those keys to data which are in schema but not in data
65+
66+
let blankItem = getBlankItem(schema);
67+
68+
if (schema.type === 'object') {
69+
return {...blankItem, ...data};
70+
} else if (schema.type === 'array') {
71+
for (let i = 0; i < data.length; i++) {
72+
data[i] = {...blankItem, ...data[i]};
73+
}
74+
}
75+
76+
return data;
77+
}

0 commit comments

Comments
 (0)