Skip to content

Commit 552b339

Browse files
author
Öner Zafer
committed
cell updates reverted, 'readonly' removed, tests are enriched
1 parent af4e74d commit 552b339

File tree

5 files changed

+127
-32
lines changed

5 files changed

+127
-32
lines changed

packages/core/src/util/cell.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
*/
2525
import isEmpty from 'lodash/isEmpty';
2626
import union from 'lodash/union';
27-
import { getAjv, getConfig, getData, getErrorAt, getSchema } from '../reducers';
27+
import { getConfig, getData, getErrorAt, getSchema, getAjv } from '../reducers';
2828
import {
2929
AnyAction,
3030
Dispatch,
3131
formatErrorMessage,
32-
isInherentlyEnabled,
32+
isEnabled,
3333
isVisible,
3434
OwnPropsOfControl,
3535
OwnPropsOfEnum,
@@ -82,7 +82,6 @@ export type DispatchPropsOfCell = DispatchPropsOfControl;
8282
* Props of a cell.
8383
*/
8484
export interface CellProps extends StatePropsOfCell, DispatchPropsOfCell {}
85-
8685
/**
8786
* Registers the given cell renderer when a JSON Forms store is created.
8887
* @param {RankedTester} tester
@@ -110,22 +109,18 @@ export const mapStateToCellProps = (
110109
ownProps.visible !== undefined
111110
? ownProps.visible
112111
: isVisible(uischema, rootData, undefined, getAjv(state));
112+
const readonly = state.jsonforms.readonly;
113+
const enabled =
114+
!readonly &&
115+
(ownProps.enabled !== undefined
116+
? ownProps.enabled
117+
: isEnabled(uischema, rootData, undefined, getAjv(state)));
113118
const errors = formatErrorMessage(
114119
union(getErrorAt(path, schema)(state).map(error => error.message))
115120
);
116121
const isValid = isEmpty(errors);
117122
const rootSchema = getSchema(state);
118123

119-
const config = getConfig(state);
120-
const enabled: boolean = isInherentlyEnabled(
121-
state,
122-
ownProps,
123-
uischema,
124-
schema || rootSchema,
125-
rootData,
126-
config
127-
);
128-
129124
return {
130125
data: Resolve.data(rootData, path),
131126
visible,
@@ -136,7 +131,7 @@ export const mapStateToCellProps = (
136131
isValid,
137132
schema,
138133
uischema,
139-
config,
134+
config: getConfig(state),
140135
rootSchema,
141136
renderers,
142137
cells

packages/core/src/util/runtime.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export const isInherentlyEnabled = (
185185
state: JsonFormsState,
186186
ownProps: any,
187187
uischema: UISchemaElement,
188-
schema: JsonSchema & { readonly?: boolean; readOnly?: boolean },
188+
schema: JsonSchema & { readOnly?: boolean },
189189
rootData: any,
190190
config: any
191191
) => {
@@ -210,9 +210,6 @@ export const isInherentlyEnabled = (
210210
if (typeof schema?.readOnly === 'boolean') {
211211
return !schema.readOnly;
212212
}
213-
if (typeof schema?.readonly === 'boolean') {
214-
return !schema.readonly;
215-
}
216213
if (typeof ownProps?.enabled === 'boolean') {
217214
return ownProps.enabled;
218215
}

packages/core/test/util/cell.test.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ test('mapStateToCellProps - visible via state ', t => {
152152
t.true(props.visible);
153153
});
154154

155-
test('mapStateToCellProps - enabled via rule ', t => {
155+
test('mapStateToCellProps - enabled via ownProps ', t => {
156156
const uischema = {
157157
...coreUISchema,
158-
rule: enableRule
158+
rule: disableRule
159159
};
160160
const ownProps = {
161161
enabled: true,
@@ -166,27 +166,39 @@ test('mapStateToCellProps - enabled via rule ', t => {
166166
});
167167

168168
test('mapStateToCellProps - disabled via ownProps ', t => {
169+
const uischema = {
170+
...coreUISchema,
171+
rule: disableRule
172+
};
169173
const ownProps = {
170174
enabled: false,
171-
uischema: coreUISchema
175+
uischema
172176
};
173-
const props = mapStateToCellProps(createState(coreUISchema), ownProps);
177+
const props = mapStateToCellProps(createState(uischema), ownProps);
174178
t.false(props.enabled);
175179
});
176180

177181
test('mapStateToCellProps - disabled via state ', t => {
182+
const uischema = {
183+
...coreUISchema,
184+
rule: disableRule
185+
};
178186
const ownProps = {
179-
uischema: coreUISchema
187+
uischema
180188
};
181-
const props = mapStateToCellProps(createState(coreUISchema), ownProps);
189+
const props = mapStateToCellProps(createState(uischema), ownProps);
182190
t.false(props.enabled);
183191
});
184192

185193
test('mapStateToCellProps - enabled via state ', t => {
194+
const uischema = {
195+
...coreUISchema,
196+
rule: disableRule
197+
};
186198
const ownProps = {
187-
uischema: coreUISchema
199+
uischema
188200
};
189-
const clonedState = _.cloneDeep(createState(coreUISchema));
201+
const clonedState = _.cloneDeep(createState(uischema));
190202
clonedState.jsonforms.core.data.firstName = 'Lisa';
191203
const props = mapStateToCellProps(clonedState, ownProps);
192204
t.true(props.enabled);

packages/core/test/util/runtime.test.ts

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
ControlElement,
2929
createAjv,
3030
isInherentlyEnabled,
31+
JsonFormsCore,
3132
LeafCondition,
3233
OrCondition,
3334
RuleEffect,
@@ -587,17 +588,55 @@ test('isInherentlyEnabled disabled by uischema', t => {
587588
);
588589
});
589590

591+
test('isInherentlyEnabled disabled by uischema over ownProps', t => {
592+
t.false(
593+
isInherentlyEnabled(
594+
null,
595+
{ enabled: false },
596+
({ options: { readonly: true } } as unknown) as ControlElement,
597+
null,
598+
null,
599+
null
600+
)
601+
);
602+
});
603+
604+
test('isInherentlyEnabled disabled by uischema over schema', t => {
605+
t.false(
606+
isInherentlyEnabled(
607+
null,
608+
null,
609+
({ options: { readonly: true } } as unknown) as ControlElement,
610+
{ readOnly: false },
611+
null,
612+
null
613+
)
614+
);
615+
});
616+
590617
test('isInherentlyEnabled disabled by schema', t => {
591618
t.false(
592-
isInherentlyEnabled(null, null, null, { readonly: true }, null, null)
619+
isInherentlyEnabled(null, null, null, { readOnly: true }, null, null)
620+
);
621+
});
622+
623+
test('isInherentlyEnabled disabled by schema over ownProps', t => {
624+
t.false(
625+
isInherentlyEnabled(
626+
null,
627+
{ enabled: true },
628+
null,
629+
{ readOnly: true },
630+
null,
631+
null
632+
)
593633
);
594634
});
595635

596636
test('isInherentlyEnabled disabled by rule', t => {
597-
const leafCondition: LeafCondition = {
598-
type: 'LEAF',
637+
const leafCondition: SchemaBasedCondition = {
599638
scope: '#/properties/ruleValue',
600-
expectedValue: 'bar'
639+
schema: { type: 'string', pattern: 'bar' }
601640
};
602641
const uischema: ControlElement = {
603642
type: 'Control',
@@ -612,7 +651,51 @@ test('isInherentlyEnabled disabled by rule', t => {
612651
ruleValue: 'bar'
613652
};
614653

615-
t.false(isInherentlyEnabled(null, null, uischema, null, data, null));
654+
t.false(
655+
isInherentlyEnabled(
656+
{ jsonforms: { core: { ajv: createAjv() } as JsonFormsCore } },
657+
null,
658+
uischema,
659+
null,
660+
data,
661+
null
662+
)
663+
);
664+
});
665+
666+
test('isInherentlyEnabled disabled by global over rule ', t => {
667+
const leafCondition: SchemaBasedCondition = {
668+
scope: '#/properties/ruleValue',
669+
schema: { type: 'string', pattern: 'bar' }
670+
};
671+
const uischema: ControlElement = {
672+
type: 'Control',
673+
scope: '#/properties/value',
674+
rule: {
675+
effect: RuleEffect.ENABLE,
676+
condition: leafCondition
677+
}
678+
};
679+
const data = {
680+
value: 'foo',
681+
ruleValue: 'bar'
682+
};
683+
684+
t.false(
685+
isInherentlyEnabled(
686+
{
687+
jsonforms: {
688+
readonly: true,
689+
core: { ajv: createAjv() } as JsonFormsCore
690+
}
691+
},
692+
null,
693+
uischema,
694+
null,
695+
data,
696+
null
697+
)
698+
);
616699
});
617700

618701
test('isInherentlyEnabled disabled by config', t => {
@@ -621,6 +704,14 @@ test('isInherentlyEnabled disabled by config', t => {
621704
);
622705
});
623706

707+
test('isInherentlyEnabled enabled by config over ownProps', t => {
708+
t.true(
709+
isInherentlyEnabled(null, { enabled: false }, null, null, null, {
710+
readonly: false
711+
})
712+
);
713+
});
714+
624715
test('isInherentlyEnabled enabled', t => {
625716
t.true(isInherentlyEnabled(null, null, null, null, null, null));
626717
});

packages/examples/src/readonly.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const schema = {
2929
properties: {
3030
readonly: {
3131
type: 'string',
32-
readonly: true
32+
readOnly: true
3333
},
3434
readonlyByUISchema: {
3535
type: 'string'

0 commit comments

Comments
 (0)