Skip to content

Commit 94edde6

Browse files
authored
fix: fields general (#93)
* fix: fields general * test(core): update snapshot
1 parent b7f895c commit 94edde6

11 files changed

Lines changed: 88 additions & 20 deletions

File tree

packages/prime-core/__tests__/modules/__snapshots__/external.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Array [
1313
Object {
1414
"description": "Boolean field",
1515
"options": Object {
16+
"default": false,
1617
"label": "",
1718
},
1819
"title": "Boolean",

packages/prime-core/src/utils/DocumentTransformer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ export class DocumentTransformer {
6161

6262
let value = get(data, io === Types.INPUT ? field.name : field.id);
6363

64-
if (typeof value === 'undefined') {
65-
continue;
66-
}
67-
6864
if (primeField) {
6965
if (io === Types.INPUT) {
7066
value = await primeField.processInput(value);
@@ -73,6 +69,10 @@ export class DocumentTransformer {
7369
}
7470
}
7571

72+
if (typeof value === 'undefined') {
73+
continue;
74+
}
75+
7676
if (field.parentFieldId && type !== Types.GROUP) {
7777
continue;
7878
}

packages/prime-field-boolean/src/PrimeFieldBoolean.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GraphQLBoolean } from 'graphql';
33

44
interface Options {
55
label: string;
6+
default: boolean;
67
}
78

89
export class PrimeFieldBoolean extends PrimeField {
@@ -11,6 +12,7 @@ export class PrimeFieldBoolean extends PrimeField {
1112
public static description: string = 'Boolean field';
1213
public static options: Options = {
1314
label: '',
15+
default: false,
1416
};
1517

1618
public outputType() {
@@ -30,10 +32,18 @@ export class PrimeFieldBoolean extends PrimeField {
3032
}
3133

3234
public async processInput(value) {
35+
if (typeof value === 'undefined') {
36+
return this.options.default;
37+
}
38+
3339
return Boolean(value);
3440
}
3541

3642
public async processOutput(value) {
43+
if (typeof value === 'undefined') {
44+
return this.options.default;
45+
}
46+
3747
return Boolean(value);
3848
}
3949
}

packages/prime-field-boolean/src/ui/InputComponent.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ import React from 'react';
44

55
export class InputComponent extends React.PureComponent<PrimeFieldProps> {
66
public render() {
7-
const { form, field, path, initialValue = false } = this.props;
7+
const { form, field, path, initialValue = this.props.field.options.default } = this.props;
88
const { getFieldDecorator } = form;
99

10+
const error = form.getFieldError(path);
11+
const help =
12+
field.description && field.description !== ''
13+
? `${field.description}${error ? ` - ${error}` : ''}`
14+
: undefined;
15+
1016
return (
11-
<Form.Item label={field.title}>
17+
<Form.Item label={field.title} help={help}>
1218
{getFieldDecorator(path, {
1319
initialValue,
1420
valuePropName: 'checked',
1521
})(<Switch />)}
16-
{field.description && field.description !== '' && (
22+
{field.options.label && field.options.label !== '' && (
1723
<label htmlFor={path} style={{ marginLeft: 8 }}>
18-
{field.description}
24+
{field.options.label}
1925
</label>
2026
)}
2127
</Form.Item>
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import { PrimeFieldProps } from '@primecms/field';
2+
import { Form, Input, Switch } from 'antd';
23
import React from 'react';
34

4-
export class SchemaSettingsComponent extends React.PureComponent<PrimeFieldProps> {
5+
type Props = PrimeFieldProps & {
6+
options?: { label: string; default: boolean };
7+
};
8+
9+
export class SchemaSettingsComponent extends React.PureComponent<Props> {
510
public render() {
6-
return null;
11+
const { form } = this.props;
12+
return (
13+
<>
14+
<Form.Item label="On by default">
15+
{form.getFieldDecorator('options.default', {
16+
valuePropName: 'checked',
17+
})(<Switch />)}
18+
</Form.Item>
19+
<Form.Item label="Label">{form.getFieldDecorator('options.label')(<Input />)}</Form.Item>
20+
</>
21+
);
722
}
823
}

packages/prime-field-datetime/src/ui/SchemaSettingsComponent.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ export class SchemaSettingsComponent extends React.PureComponent<Props> {
1515
return (
1616
<>
1717
<Form.Item label="Options" style={{ marginBottom: -8 }} />
18-
<Form.Item>
18+
<Form.Item help="Date vs. DateTime">
1919
{form.getFieldDecorator('options.time', {
2020
valuePropName: 'checked',
21-
initialValue: options.time,
21+
initialValue: options.time || true,
2222
})(<Switch />)}
2323
<label htmlFor="options.time" style={{ marginLeft: 8 }}>
24-
Show time picker
24+
Include timestamp
2525
</label>
2626
</Form.Item>
2727
</>

packages/prime-field-group/src/ui/InputComponent.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,25 @@ import { Button, Card, Form, Icon } from 'antd';
33
import { get } from 'lodash';
44
import React from 'react';
55

6-
const { uuid } = window as any;
6+
const randomByte = () => {
7+
const seq = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16);
8+
return seq.substr(seq.length - 1, 1);
9+
};
10+
const randomBytes = length =>
11+
Array.from({ length })
12+
.map(randomByte)
13+
.join('');
14+
const randomUuid = () => [8, 4, 4, 4, 12].map(randomBytes).join('-');
15+
16+
const { uuid = { v4: randomUuid } } = window as any;
17+
18+
const initialToken = uuid.v4();
19+
20+
const getItems = ({ initialValue, field }: PrimeFieldProps) => {
21+
if (!field.options.repeated) {
22+
return [[initialToken, 0]];
23+
}
724

8-
const getItems = ({ initialValue }: PrimeFieldProps) => {
925
if (Array.isArray(initialValue)) {
1026
return initialValue.map((_, index) => [uuid.v4(), index]);
1127
}
@@ -105,6 +121,10 @@ export class InputComponent extends React.PureComponent<PrimeFieldProps, any> {
105121
const { field } = this.props;
106122
const repeated = get(field, 'options.repeated', false);
107123

124+
if (field.fields.length === 0) {
125+
return null;
126+
}
127+
108128
return (
109129
<Form.Item label={field.title} className="prime-group">
110130
{items.map(this.renderGroupItem)}

packages/prime-field-number/src/PrimeFieldNumber.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export class PrimeFieldNumber extends PrimeField {
7373
public processInput(value) {
7474
const { rules } = this.options;
7575
const { name } = this.schemaField;
76+
77+
if (!this.options.float) {
78+
value = parseInt(value, 10);
79+
}
80+
7681
const num = Number(value);
7782

7883
if (rules.required) {

packages/prime-ui/src/routes/documents/DocumentsDetail.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,23 @@ export class DocumentsDetail extends React.Component<IProps> {
226226

227227
public onKeyDown = (e: any) => {
228228
if (e.which === 83 && (e.ctrlKey || e.metaKey)) {
229-
return this.onSave(e);
229+
if (e.shiftKey) {
230+
return this.onPublish(e);
231+
} else {
232+
return this.onSave(e);
233+
}
230234
}
231235
return true;
232236
};
233237

234-
public onSave = async (e: React.MouseEvent<HTMLElement>) => {
238+
public onSave = async (e: React.MouseEvent<HTMLElement>, feedback = true) => {
235239
e.preventDefault();
236240
try {
237241
const releaseId = this.options.release;
238242
await this.save({ documentId: this.props.match.params.entryId, releaseId });
239-
message.success('Document was saved', 1);
243+
if (feedback) {
244+
message.success('Document was saved', 1);
245+
}
240246
} catch (err) {
241247
message.error('Could not save document');
242248
console.error(err); // tslint:disable-line no-console
@@ -247,6 +253,9 @@ export class DocumentsDetail extends React.Component<IProps> {
247253
public onPublish = async (e: any) => {
248254
if (this.contentEntry) {
249255
this.loading.publish = true;
256+
if (this.promptEnabled && this.documentForm!.props.form.isFieldsTouched()) {
257+
await this.onSave(e, false);
258+
}
250259
await this.contentEntry.publish();
251260
message.success('Document was published', 1);
252261
this.loading.publish = false;

packages/prime-ui/src/routes/schemas/SchemaDetail.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ export class SchemaDetail extends React.Component<IProps> {
117117
public onKeyDown = (e: any) => {
118118
if (e.which === 83 && (e.ctrlKey || e.metaKey)) {
119119
e.preventDefault();
120-
this.saveSchema();
120+
if (!this.isDrawerOpen) {
121+
this.saveSchema();
122+
}
121123
return false;
122124
}
123125
return true;

0 commit comments

Comments
 (0)