Skip to content

Commit b9b339e

Browse files
committed
fix(renderer): take condition and hideField also from action mapper
1 parent 1fa4870 commit b9b339e

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

packages/react-form-renderer/src/form-renderer/render-form.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,21 @@ FormConditionWrapper.propTypes = {
8686
field: PropTypes.object,
8787
};
8888

89-
const SingleField = ({ component, condition, hideField, ...rest }) => {
89+
const SingleField = ({ component, ...rest }) => {
9090
const { actionMapper, componentMapper } = useContext(RendererContext);
9191

9292
const { componentProps, Component, overrideProps, mergedResolveProps } = prepareComponentProps({ component, rest, componentMapper, actionMapper });
9393

94+
const { condition, hideField, ...restProps } = {
95+
...componentProps,
96+
...overrideProps,
97+
...(mergedResolveProps && { resolveProps: mergedResolveProps }),
98+
};
99+
94100
return (
95-
<FormConditionWrapper condition={condition} field={componentProps}>
101+
<FormConditionWrapper condition={condition} field={restProps}>
96102
<FormFieldHideWrapper hideField={hideField}>
97-
<Component {...componentProps} {...overrideProps} {...(mergedResolveProps && { resolveProps: mergedResolveProps })} />
103+
<Component {...restProps} />
98104
</FormFieldHideWrapper>
99105
</FormConditionWrapper>
100106
);

packages/react-form-renderer/src/tests/form-renderer/condition.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,46 @@ describe('condition test', () => {
100100
expect(() => screen.getByLabelText('field-2')).toThrow();
101101
});
102102

103+
it('should render when condition is fulfill - when is a function from action mapper', async () => {
104+
const whenSpy = jest.fn().mockImplementation(() => 'field-1');
105+
const actionMapper = {
106+
condition: () => [
107+
{
108+
when: whenSpy,
109+
is: 'show',
110+
},
111+
],
112+
};
113+
114+
schema = {
115+
fields: [
116+
{
117+
component: componentTypes.TEXT_FIELD,
118+
name: 'field-1',
119+
},
120+
{
121+
component: componentTypes.TEXT_FIELD,
122+
name: 'field-2',
123+
actions: { condition: ['condition'] },
124+
},
125+
],
126+
};
127+
128+
render(<FormRenderer {...initialProps} schema={schema} actionMapper={actionMapper} />);
129+
130+
expect(whenSpy.mock.calls[0][0]).toEqual({ component: 'text-field', name: 'field-2' });
131+
132+
expect(() => screen.getByLabelText('field-2')).toThrow();
133+
134+
await userEvent.type(screen.getByLabelText('field-1'), 'show');
135+
136+
expect(screen.getByLabelText('field-2')).toBeInTheDocument();
137+
138+
await userEvent.type(screen.getByLabelText('field-1'), 'dont');
139+
140+
expect(() => screen.getByLabelText('field-2')).toThrow();
141+
});
142+
103143
it('sets value when condition is fulfill', async () => {
104144
schema = {
105145
fields: [

0 commit comments

Comments
 (0)