Skip to content

Commit

Permalink
fix: add state for state reference in set state param (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch authored and alharris-at committed Feb 24, 2022
1 parent 3ab343b commit 8143ca8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
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 @@ -107,6 +107,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 @@ -358,7 +361,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) },
]),
};
};
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"
}
}
}
]
}

0 comments on commit 8143ca8

Please sign in to comment.