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

feat: add default initial value for form component state #396

Merged
merged 2 commits 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 @@ -5223,7 +5223,7 @@ export default function InputMutationOnClick(
props: InputMutationOnClickProps
): React.ReactElement {
const { overrides, ...rest } = props;
const [myInputValue, setMyInputValue] = useStateMutationAction(undefined);
const [myInputValue, setMyInputValue] = useStateMutationAction(\\"\\");
const setInputButtonClick = () => {
setMyInputValue(\\"Razor Crest\\");
};
Expand Down Expand Up @@ -5376,7 +5376,7 @@ export default function TwoWayBindings(
): React.ReactElement {
const { overrides, ...rest } = props;
const [textFieldInputValue, setTextFieldInputValue] =
useStateMutationAction(undefined);
useStateMutationAction(\\"\\");
return (
/* @ts-ignore: TS2322 */
<Flex
Expand Down Expand Up @@ -5426,7 +5426,7 @@ export default function CheckboxControlledElement(
props: CheckboxControlledElementProps
): React.ReactElement {
const { overrides, ...rest } = props;
const [inputChecked, setInputChecked] = useStateMutationAction(undefined);
const [inputChecked, setInputChecked] = useStateMutationAction(false);
return (
/* @ts-ignore: TS2322 */
<Flex
Expand Down Expand Up @@ -5524,7 +5524,7 @@ export default function SwitchControlledElement(
props: SwitchControlledElementProps
): React.ReactElement {
const { overrides, ...rest } = props;
const [inputIsChecked, setInputIsChecked] = useStateMutationAction(undefined);
const [inputIsChecked, setInputIsChecked] = useStateMutationAction(false);
return (
/* @ts-ignore: TS2322 */
<Flex {...rest} {...getOverrideProps(overrides, \\"SwitchControlledElement\\")}>
Expand Down Expand Up @@ -5815,25 +5815,25 @@ export default function TwoWayBindings(
): React.ReactElement {
const { overrides, ...rest } = props;
const [checkboxFieldInputChecked, setCheckboxFieldInputChecked] =
useStateMutationAction(undefined);
useStateMutationAction(false);
const [passwordFieldInputValue, setPasswordFieldInputValue] =
useStateMutationAction(undefined);
useStateMutationAction(\\"\\");
const [phoneNumberFieldInputValue, setPhoneNumberFieldInputValue] =
useStateMutationAction(undefined);
useStateMutationAction(\\"\\");
const [radioGroupFieldInputValue, setRadioGroupFieldInputValue] =
useStateMutationAction(undefined);
useStateMutationAction(\\"html\\");
const [searchFieldInputValue, setSearchFieldInputValue] =
useStateMutationAction(undefined);
useStateMutationAction(\\"\\");
const [selectFieldInputValue, setSelectFieldInputValue] =
useStateMutationAction(undefined);
const [sliderFieldInputValue, setSliderFieldInputValue] =
useStateMutationAction(undefined);
const [stepperFieldInputValue, setStepperFieldInputValue] =
useStateMutationAction(undefined);
const [switchFieldInputIsChecked, setSwitchFieldInputIsChecked] =
useStateMutationAction(undefined);
useStateMutationAction(false);
const [textFieldInputValue, setTextFieldInputValue] =
useStateMutationAction(undefined);
useStateMutationAction(\\"\\");
const setCheckboxFieldValueClick = () => {
setCheckboxFieldInputChecked(false);
};
Expand Down
25 changes: 25 additions & 0 deletions packages/codegen-ui-react/lib/primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import { FixedStudioComponentProperty } from '@aws-amplify/codegen-ui';
import { factory, SyntaxKind, TypeParameterDeclaration, TypeNode } from 'typescript';
import iconset from './iconset';

Expand Down Expand Up @@ -128,3 +129,27 @@ export const PrimitivesWithChangeEvent: Set<Primitive> = new Set([
Primitive.SwitchField,
Primitive.TextField,
]);

export const PrimitiveDefaultPropertyValue: PrimitiveLevelPropConfiguration<FixedStudioComponentProperty> = {
[Primitive.CheckboxField]: {
checked: { value: false, type: 'boolean' },
},
[Primitive.PasswordField]: {
value: { value: '', type: 'string' },
},
[Primitive.PhoneNumberField]: {
value: { value: '', type: 'string' },
},
[Primitive.RadioGroupField]: {
value: { value: '', type: 'string' },
},
[Primitive.SearchField]: {
value: { value: '', type: 'string' },
},
[Primitive.SwitchField]: {
isChecked: { value: false, type: 'boolean' },
},
[Primitive.TextField]: {
value: { value: '', type: 'string' },
},
};
34 changes: 33 additions & 1 deletion packages/codegen-ui-react/lib/workflow/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
StateReference,
StudioGenericEvent,
buildComponentNameToTypeMap,
StudioComponentProperty,
} from '@aws-amplify/codegen-ui';
import {
isActionEvent,
Expand All @@ -32,9 +33,13 @@ import {
getSetStateName,
} from '../react-component-render-helper';
import { ImportCollection, ImportValue } from '../imports';
import Primitive, { PrimitivesWithChangeEvent, PrimitiveLevelPropConfiguration } from '../primitive';
import { mapGenericEventToReact } from './events';
import { getChildPropMappingForComponentName } from './utils';
import Primitive, {
PrimitivesWithChangeEvent,
PrimitiveLevelPropConfiguration,
PrimitiveDefaultPropertyValue,
} from '../primitive';

type EventHandlerBuilder = (setStateName: string, stateName: string) => JsxExpression;

Expand Down Expand Up @@ -273,6 +278,14 @@ export function getStateDefaultValue(component: StudioComponent, stateReference:
const { componentName, property } = stateReference;
const referencedComponent = getComponentFromComponentTree(component, componentName);
const componentProperty = referencedComponent.properties[property];

// does not work for custom components wrapping form components
if (
componentProperty === undefined &&
PrimitiveDefaultPropertyValue[referencedComponent.componentType as Primitive]
) {
return propertyToExpression(getDefaultForComponentAndProperty(referencedComponent, property));
}
return propertyToExpression(componentProperty);
}

Expand Down Expand Up @@ -303,6 +316,25 @@ export function getComponentFromComponentTree(
return res;
}

export function getDefaultForComponentAndProperty(
component: StudioComponent | StudioComponentChild,
property: string,
): StudioComponentProperty {
const { componentType } = component;
const componentDefault = PrimitiveDefaultPropertyValue[componentType as Primitive];
if (componentDefault && property in componentDefault) {
// if component has defaultValue use defaultValue as initial state value
if (property === 'value' && component.properties.defaultValue) {
// TODO: remove defaultValue becuase coponents canot have value and defaultValue
return component.properties.defaultValue;
}
return componentDefault[property];
}

// use empty string a fallback default
return { value: '', type: 'string' };
}

export type MutationReferences = {
[property: string]: { addControlEvent: boolean }[];
};
Expand Down