From c254c5a85ec3e1769d264594f5394456911193a5 Mon Sep 17 00:00:00 2001 From: Al Harris <91494052+alharris-at@users.noreply.github.com> Date: Wed, 9 Feb 2022 14:37:41 -0800 Subject: [PATCH] chore: add control overrides for checkbox and switch (#390) --- .../studio-ui-codegen-react.test.ts.snap | 105 ++++++++++++++++-- .../__tests__/studio-ui-codegen-react.test.ts | 8 ++ .../lib/react-component-renderer.ts | 1 + .../react-component-with-children-renderer.ts | 1 + .../codegen-ui-react/lib/workflow/mutation.ts | 99 ++++++++++------- .../workflow/checkboxControlledElement.json | 24 ++++ .../workflow/switchControlledElement.json | 24 ++++ .../workflow/twoWayBindings.json | 4 +- .../components/workflow/twoWayBindings.json | 4 +- 9 files changed, 220 insertions(+), 50 deletions(-) create mode 100644 packages/codegen-ui/example-schemas/workflow/checkboxControlledElement.json create mode 100644 packages/codegen-ui/example-schemas/workflow/switchControlledElement.json diff --git a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap index ac3272ce3..68ae1eb87 100644 --- a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap @@ -5406,6 +5406,53 @@ export default function TwoWayBindings( } `; +exports[`amplify render tests mutations supports a controlled checkbox primitive 1`] = ` +Object { + "componentText": "/* eslint-disable */ +import React from \\"react\\"; +import { + EscapeHatchProps, + getOverrideProps, + useStateMutationAction, +} from \\"@aws-amplify/ui-react/internal\\"; +import { CheckboxField, Flex, FlexProps, Text } from \\"@aws-amplify/ui-react\\"; + +export type CheckboxControlledElementProps = React.PropsWithChildren< + Partial & { + overrides?: EscapeHatchProps | undefined | null; + } +>; +export default function CheckboxControlledElement( + props: CheckboxControlledElementProps +): React.ReactElement { + const { overrides, ...rest } = props; + const [inputChecked, setInputChecked] = useStateMutationAction(undefined); + return ( + /* @ts-ignore: TS2322 */ + + { + setInputChecked(event.target.checked); + }} + {...getOverrideProps(overrides, \\"Input\\")} + > + + + ); +} +", + "declaration": undefined, + "renderComponentToFilesystem": [Function], +} +`; + exports[`amplify render tests mutations supports a controlled stepper primitive 1`] = ` Object { "componentText": "/* eslint-disable */ @@ -5457,6 +5504,48 @@ export default function StepperControlledElement( } `; +exports[`amplify render tests mutations supports a controlled switch primitive 1`] = ` +Object { + "componentText": "/* eslint-disable */ +import React from \\"react\\"; +import { + EscapeHatchProps, + getOverrideProps, + useStateMutationAction, +} from \\"@aws-amplify/ui-react/internal\\"; +import { Flex, FlexProps, SwitchField, Text } from \\"@aws-amplify/ui-react\\"; + +export type SwitchControlledElementProps = React.PropsWithChildren< + Partial & { + overrides?: EscapeHatchProps | undefined | null; + } +>; +export default function SwitchControlledElement( + props: SwitchControlledElementProps +): React.ReactElement { + const { overrides, ...rest } = props; + const [inputIsChecked, setInputIsChecked] = useStateMutationAction(undefined); + return ( + /* @ts-ignore: TS2322 */ + + setInputIsChecked(!inputIsChecked)} + {...getOverrideProps(overrides, \\"Input\\")} + > + + + ); +} +", + "declaration": undefined, + "renderComponentToFilesystem": [Function], +} +`; + exports[`amplify render tests mutations supports multiple actions pointing to the same value 1`] = ` Object { "componentText": "/* eslint-disable */ @@ -5741,7 +5830,7 @@ export default function TwoWayBindings( useStateMutationAction(undefined); const [stepperFieldInputValue, setStepperFieldInputValue] = useStateMutationAction(undefined); - const [switchFieldInputValue, setSwitchFieldInputValue] = + const [switchFieldInputIsChecked, setSwitchFieldInputIsChecked] = useStateMutationAction(undefined); const [textFieldInputValue, setTextFieldInputValue] = useStateMutationAction(undefined); @@ -5770,7 +5859,7 @@ export default function TwoWayBindings( setStepperFieldInputValue(9); }; const setSwitchFieldValueClick = () => { - setSwitchFieldInputValue(true); + setSwitchFieldInputIsChecked(true); }; const setTextFieldValueClick = () => { setTextFieldInputValue(\\"Hardcoded Value\\"); @@ -5802,7 +5891,7 @@ export default function TwoWayBindings( value=\\"yes\\" checked={checkboxFieldInputChecked} onChange={(event: SyntheticEvent) => { - setCheckboxFieldInputChecked(event.target.value); + setCheckboxFieldInputChecked(event.target.checked); }} {...getOverrideProps(overrides, \\"CheckboxFieldInput\\")} > @@ -6079,14 +6168,14 @@ export default function TwoWayBindings( labelPosition=\\"start\\" isDisabled={false} isLabelHidden={false} - value={switchFieldInputValue} - onChange={(event: SyntheticEvent) => { - setSwitchFieldInputValue(event.target.value); - }} + isChecked={switchFieldInputIsChecked} + onChange={() => + setSwitchFieldInputIsChecked(!switchFieldInputIsChecked) + } {...getOverrideProps(overrides, \\"SwitchFieldInput\\")} >