Skip to content

Commit

Permalink
fix: forms checkbox to reflect correct model state (#738)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Young <scoyou@amazon.com>
  • Loading branch information
scottyoung and Scott Young authored Nov 3, 2022
1 parent bbfa57d commit 221e8d5
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4977,7 +4977,7 @@ export default function InputGalleryCreateForm(props) {
rootbeer: undefined,
attend: undefined,
maybeSlide: false,
maybeCheck: undefined,
maybeCheck: false,
arrayTypeField: [],
timestamp: undefined,
ippy: undefined,
Expand Down Expand Up @@ -5249,7 +5249,7 @@ export default function InputGalleryCreateForm(props) {
name=\\"maybeCheck\\"
value=\\"maybeCheck\\"
isDisabled={false}
defaultChecked={false}
checked={maybeCheck}
onChange={(e) => {
let value = e.target.checked;
if (onChange) {
Expand Down Expand Up @@ -5695,7 +5695,7 @@ export default function InputGalleryUpdateForm(props) {
rootbeer: undefined,
attend: undefined,
maybeSlide: false,
maybeCheck: undefined,
maybeCheck: false,
arrayTypeField: [],
timestamp: undefined,
ippy: undefined,
Expand Down Expand Up @@ -6007,7 +6007,7 @@ export default function InputGalleryUpdateForm(props) {
name=\\"maybeCheck\\"
value=\\"maybeCheck\\"
isDisabled={false}
defaultChecked={false}
checked={maybeCheck}
defaultValue={maybeCheck}
onChange={(e) => {
let value = e.target.checked;
Expand Down
4 changes: 4 additions & 0 deletions packages/codegen-ui-react/lib/forms/component-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export const renderValueAttribute = ({
factory.createIdentifier('isChecked'),
factory.createJsxExpression(undefined, valueIdentifier),
),
CheckboxField: factory.createJsxAttribute(
factory.createIdentifier('checked'),
factory.createJsxExpression(undefined, valueIdentifier),
),
};

if (controlledComponentToAttributesMap[componentType]) {
Expand Down
1 change: 1 addition & 0 deletions packages/codegen-ui-react/lib/forms/form-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const getDefaultValueExpression = (
SwitchField: factory.createFalse(),
StepperField: factory.createNumericLiteral(0),
SliderField: factory.createNumericLiteral(0),
CheckboxField: factory.createFalse(),
};

// it's a nonModel or relationship object
Expand Down
1 change: 0 additions & 1 deletion packages/codegen-ui-react/lib/workflow/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const genericEventToReactEventImplementationOverrides: PrimitiveLevelPropConfigu

const PrimitiveDefaultValuePropMapping: PrimitiveLevelPropConfiguration<string> = new Proxy(
{
[Primitive.CheckboxField]: { checked: 'defaultChecked' },
[Primitive.SwitchField]: { isChecked: 'defaultChecked' },
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,12 @@ describe('getFormDefinitionInputElement', () => {
const config = {
inputType: {
type: 'CheckboxField',
defaultChecked: true,
},
};

expect(getFormDefinitionInputElement(config)).toStrictEqual({
componentType: 'CheckboxField',
props: { label: 'Label', name: 'fieldName', value: 'fieldName', defaultChecked: true },
props: { label: 'Label', name: 'fieldName', value: 'fieldName' },
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ export function getFormDefinitionInputElement(
value:
config.inputType?.value || baseConfig?.inputType?.value || FORM_DEFINITION_DEFAULTS.field.inputType.value,
isDisabled: getFirstDefinedValue([config.inputType?.readOnly, baseConfig?.inputType?.readOnly]),
defaultChecked:
getFirstDefinedValue([config.inputType?.defaultChecked, baseConfig?.inputType?.defaultChecked]) || false,
},
};
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export type FormDefinitionToggleButtonElement = {

export type FormDefinitionCheckboxFieldElement = {
componentType: 'CheckboxField';
props: { label: string; value: string; name: string; isDisabled?: boolean; defaultChecked?: boolean };
props: { label: string; value: string; name: string; isDisabled?: boolean };
};

export type FormDefinitionRadioGroupFieldElement = {
Expand Down

0 comments on commit 221e8d5

Please sign in to comment.