Skip to content

Commit

Permalink
chore: update snapshots and fix model import
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsbp committed Nov 18, 2022
1 parent 28e7c78 commit e6fdc1a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3797,9 +3797,9 @@ export default function MyMemberForm(props) {
items={Team ? [Team] : []}
hasError={errors.Team?.hasError}
getBadgeText={getDisplayValue.Team}
setFieldValue={currentTeamDisplayValue}
setFieldValue={setCurrentTeamDisplayValue}
inputFieldRef={TeamRef}
defaultFieldValue={undefined}
defaultFieldValue={\\"\\"}
>
<Autocomplete
label=\\"Team Label\\"
Expand Down Expand Up @@ -9575,7 +9575,7 @@ exports[`amplify form renderer tests datastore form tests should use proper fiel
"/* eslint-disable */
import * as React from \\"react\\";
import { fetchByPath, validateField } from \\"./utils\\";
import { Member, Team } from \\"../models\\";
import { Member, Team as Team0 } from \\"../models\\";
import {
getOverrideProps,
useDataStoreBinding,
Expand Down Expand Up @@ -9752,32 +9752,32 @@ export default function MyMemberForm(props) {
} = props;
const initialValues = {
name: undefined,
team: undefined,
Team: undefined,
};
const [name, setName] = React.useState(initialValues.name);
const [team, setTeam] = React.useState(initialValues.team);
const [Team, setTeam] = React.useState(initialValues.Team);
const [errors, setErrors] = React.useState({});
const resetStateValues = () => {
setName(initialValues.name);
setTeam(initialValues.team);
setTeam(initialValues.Team);
setCurrentTeamValue(undefined);
setCurrentTeamDisplayValue(\\"\\");
setErrors({});
};
const [currentTeamDisplayValue, setCurrentTeamDisplayValue] =
React.useState(\\"\\");
const [currentTeamValue, setCurrentTeamValue] = React.useState(undefined);
const teamRef = React.createRef();
const TeamRef = React.createRef();
const teamRecords = useDataStoreBinding({
type: \\"collection\\",
model: Team,
model: Team0,
}).items;
const getDisplayValue = {
team: (record) => record?.name,
Team: (record) => record?.name,
};
const validations = {
name: [],
team: [],
Team: [],
};
const runValidationTasks = async (
fieldName,
Expand Down Expand Up @@ -9805,7 +9805,7 @@ export default function MyMemberForm(props) {
event.preventDefault();
let modelFields = {
name,
team,
Team,
};
const validationResponses = await Promise.all(
Object.keys(validations).reduce((promises, fieldName) => {
Expand Down Expand Up @@ -9891,7 +9891,7 @@ export default function MyMemberForm(props) {
if (onChange) {
const modelFields = {
name: value,
team,
Team,
};
const result = onChange(modelFields);
value = result?.name ?? value;
Expand All @@ -9913,22 +9913,22 @@ export default function MyMemberForm(props) {
if (onChange) {
const modelFields = {
name,
team: value,
Team: value,
};
const result = onChange(modelFields);
value = result?.team ?? value;
value = result?.Team ?? value;
}
setTeam(value);
setCurrentTeamValue(undefined);
setCurrentTeamDisplayValue(\\"\\");
}}
currentFieldValue={currentTeamValue}
label={\\"Team Label\\"}
items={team ? [team] : []}
hasError={errors.team?.hasError}
getBadgeText={getDisplayValue.team}
items={Team ? [Team] : []}
hasError={errors.Team?.hasError}
getBadgeText={getDisplayValue.Team}
setFieldValue={setCurrentTeamDisplayValue}
inputFieldRef={teamRef}
inputFieldRef={TeamRef}
defaultFieldValue={\\"\\"}
>
<Autocomplete
Expand All @@ -9938,7 +9938,7 @@ export default function MyMemberForm(props) {
value={currentTeamDisplayValue}
options={teamRecords.map((r) => ({
id: r.id,
label: getDisplayValue.team?.(r) ?? r.id,
label: getDisplayValue.Team?.(r) ?? r.id,
}))}
onSelect={({ id, label }) => {
setCurrentTeamValue(teamRecords.find((r) => r.id === id));
Expand All @@ -9949,17 +9949,17 @@ export default function MyMemberForm(props) {
}}
onChange={(e) => {
let { value } = e.target;
if (errors.team?.hasError) {
runValidationTasks(\\"team\\", value);
if (errors.Team?.hasError) {
runValidationTasks(\\"Team\\", value);
}
setCurrentTeamDisplayValue(value);
setCurrentTeamValue(undefined);
}}
onBlur={() => runValidationTasks(\\"team\\", team)}
errorMessage={errors.team?.errorMessage}
hasError={errors.team?.hasError}
ref={teamRef}
{...getOverrideProps(overrides, \\"team\\")}
onBlur={() => runValidationTasks(\\"Team\\", Team)}
errorMessage={errors.Team?.errorMessage}
hasError={errors.Team?.hasError}
ref={TeamRef}
{...getOverrideProps(overrides, \\"Team\\")}
></Autocomplete>
</ArrayField>
</Grid>
Expand All @@ -9970,7 +9970,7 @@ export default function MyMemberForm(props) {

exports[`amplify form renderer tests datastore form tests should use proper field overrides for belongsTo relationship 2`] = `
"import * as React from \\"react\\";
import { Team } from \\"../models\\";
import { Team as Team0 } from \\"../models\\";
import { EscapeHatchProps } from \\"@aws-amplify/ui-react/internal\\";
import { AutocompleteProps, GridProps, TextFieldProps } from \\"@aws-amplify/ui-react\\";
export declare type ValidationResponse = {
Expand All @@ -9980,17 +9980,17 @@ export declare type ValidationResponse = {
export declare type ValidationFunction<T> = (value: T, validationResponse: ValidationResponse) => ValidationResponse | Promise<ValidationResponse>;
export declare type MyMemberFormInputValues = {
name?: string;
team?: Team;
Team?: Team0;
};
export declare type MyMemberFormValidationValues = {
name?: ValidationFunction<string>;
team?: ValidationFunction<Team>;
Team?: ValidationFunction<Team0>;
};
export declare type FormProps<T> = Partial<T> & React.DOMAttributes<HTMLDivElement>;
export declare type MyMemberFormOverridesProps = {
MyMemberFormGrid?: FormProps<GridProps>;
name?: FormProps<TextFieldProps>;
team?: FormProps<AutocompleteProps>;
Team?: FormProps<AutocompleteProps>;
} & EscapeHatchProps;
export declare type MyMemberFormProps = React.PropsWithChildren<{
overrides?: MyMemberFormOverridesProps | undefined | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('amplify form renderer tests', () => {
// Check that custom field label is working as expected
expect(componentText).toContain('Team Label');
// Check that Autocomplete custom display value is set
expect(componentText).toContain('team: (record) => record?.name');
expect(componentText).toContain('Team: (record) => record?.name');

expect(componentText).toMatchSnapshot();
expect(declaration).toMatchSnapshot();
Expand Down
13 changes: 6 additions & 7 deletions packages/codegen-ui-react/lib/forms/react-form-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
private renderBindingPropsType(): TypeAliasDeclaration[] {
const {
name: formName,
formActionType,
dataType: { dataSourceType, dataTypeName },
} = this.component;
const fieldConfigs = this.componentMetadata.formMetadata?.fieldConfigs ?? {};
Expand All @@ -297,13 +296,13 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
this.importCollection.addMappedImport(ImportValue.ESCAPE_HATCH_PROPS);

let modelName = dataTypeName;
if (dataSourceType === 'DataStore' && formActionType === 'update') {
// add model import for datastore type
if (dataSourceType === 'DataStore') {
this.requiredDataModels.push(dataTypeName);
modelName = this.importCollection.addImport(ImportSource.LOCAL_MODELS, dataTypeName);
}

// add model import for datastore type
if (dataSourceType === 'DataStore') {
this.requiredDataModels.push(dataTypeName);
modelName = this.importCollection.addImport(ImportSource.LOCAL_MODELS, dataTypeName);
}

return [
validationResponseType,
validationFunctionType,
Expand Down

0 comments on commit e6fdc1a

Please sign in to comment.