Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add state for state reference in set state param #397

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5776,6 +5776,60 @@ export default function SetStateWithoutInitialValue(
}
`;

exports[`amplify render tests mutations supports nested mutation 1`] = `
Object {
"componentText": "/* eslint-disable */
import React from \\"react\\";
import {
EscapeHatchProps,
getOverrideProps,
useStateMutationAction,
} from \\"@aws-amplify/ui-react/internal\\";
import { Button, Flex, FlexProps, Text } from \\"@aws-amplify/ui-react\\";

export type NestedMutationProps = React.PropsWithChildren<
Partial<FlexProps> & {
overrides?: EscapeHatchProps | undefined | null;
}
>;
export default function NestedMutation(
props: NestedMutationProps
): React.ReactElement {
const { overrides, ...rest } = props;
const [mutatedValueChildren, setMutatedValueChildren] =
useStateMutationAction(\\"Default Value\\");
const [stateSourceChildren, setStateSourceChildren] =
useStateMutationAction(\\"State Value\\");
const statePropertyMutationClick = () => {
setMutatedValueChildren(stateSourceLabel);
};
return (
/* @ts-ignore: TS2322 */
<Flex {...rest} {...getOverrideProps(overrides, \\"NestedMutation\\")}>
<Text
children={mutatedValueChildren}
{...getOverrideProps(overrides, \\"MutatedValue\\")}
></Text>
<Button
children=\\"Apply State Property Mutation\\"
onClick={() => {
statePropertyMutationClick();
}}
{...getOverrideProps(overrides, \\"StatePropertyMutation\\")}
></Button>
<Text
children={stateSourceChildren}
{...getOverrideProps(overrides, \\"StateSource\\")}
></Text>
</Flex>
);
}
",
"declaration": undefined,
"renderComponentToFilesystem": [Function],
}
`;

exports[`amplify render tests mutations supports two-way data binding on form elements 1`] = `
Object {
"componentText": "/* eslint-disable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ describe('amplify render tests', () => {
it('controls an input that is modified by a button', () => {
expect(generateWithAmplifyRenderer('workflow/inputMutationOnClick')).toMatchSnapshot();
});

it('supports nested mutation', () => {
expect(generateWithAmplifyRenderer('workflow/nestedMutation')).toMatchSnapshot();
});
});

describe('default value', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/codegen-ui-react/lib/workflow/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export function getActionStateParameters(action: ActionStudioComponentEvent): St
if (key === 'fields' || key === 'attributes') {
return Object.values(parameter);
}
if (isSetStateParameter(parameter)) {
return [parameter, parameter.set];
}
return parameter;
})
.filter((parameter) => isStateProperty(parameter) || isSetStateParameter(parameter));
Expand Down Expand Up @@ -326,7 +329,7 @@ function mutationReferenceReducerWithComponentType(componentType: string) {
return {
...mutationReferences,
[stateReference.property]: propertyReferences.concat([
{ addControlEvent: PrimitivesWithChangeEvent.has(componentType as Primitive) || !('set' in stateReference) },
{ addControlEvent: PrimitivesWithChangeEvent.has(componentType as Primitive) },
]),
alharris-at marked this conversation as resolved.
Show resolved Hide resolved
};
};
Expand Down
50 changes: 50 additions & 0 deletions packages/codegen-ui/example-schemas/workflow/nestedMutation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": "1234-5678-9010",
"componentType": "Flex",
"name": "NestedMutation",
"properties": {},
"children": [
{
"componentType": "Text",
"name": "MutatedValue",
"properties": {
"label": {
"value": "Default Value"
}
}
},
{
"componentType": "Button",
"name": "StatePropertyMutation",
"events": {
"click": {
"action": "Amplify.Mutation",
"parameters": {
"state": {
"componentName": "MutatedValue",
"property": "label",
"set": {
"componentName": "StateSource",
"property": "label"
}
}
}
}
},
"properties": {
"children": {
"value": "Apply State Property Mutation"
}
}
},
{
"componentType": "Text",
"name": "StateSource",
"properties": {
"label": {
"value": "State Value"
}
}
}
]
}