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

Submit actions #358

Merged
merged 9 commits into from
Nov 1, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ const AppFormRoute = () => {
uswdsRoot: ctx.uswdsRoot,
}}
session={formSessionResponse.formSession}
onSubmit={data => actions.onSubmitForm({ formId: id, data })}
onSubmit={data => {
actions.onSubmitForm({ formId: id, data });
}}
/>
)}
</>
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/locales/en/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export const en = {
displayName: 'Short answer',
maxLength: 'Maximum length',
},
packageDownload: {
...defaults,
displayName: 'Package download',
fieldLabel: 'Package download label',
},
page: {
fieldLabel: 'Page title',
},
Expand Down
2 changes: 2 additions & 0 deletions packages/database/src/clients/kysely/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ interface FormSessionsTable {
id: string;
form_id: string;
data: string;
created_at: Generated<Date>;
updated_at: Generated<Date>;
}
export type FormSessionsTableSelectable = Selectable<FormSessionsTable>;
export type FormSessionsTableInsertable = Insertable<FormSessionsTable>;
Expand Down
16 changes: 16 additions & 0 deletions packages/design/src/Form/components/PackageDownload/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

import { type PackageDownloadProps } from '@atj/forms';

import { type PatternComponent } from '../../../Form/index.js';
import ActionBar from '../../../Form/ActionBar/index.js';

const PackageDownload: PatternComponent<PackageDownloadProps> = props => {
return (
<>
<p className="maxw-tablet">{props.text}</p>
<ActionBar actions={props.actions} />
</>
);
};
export default PackageDownload;
2 changes: 2 additions & 0 deletions packages/design/src/Form/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Address from './Address/index.js';
import Checkbox from './Checkbox/index.js';
import Fieldset from './Fieldset/index.js';
import FormSummary from './FormSummary/index.js';
import PackageDownload from './PackageDownload/index.js';
import Page from './Page/index.js';
import PageSet from './PageSet/index.js';
import Paragraph from './Paragraph/index.js';
Expand All @@ -19,6 +20,7 @@ export const defaultPatternComponents: ComponentForPattern = {
fieldset: Fieldset as PatternComponent,
'form-summary': FormSummary as PatternComponent,
input: TextInput as PatternComponent,
'package-download': PackageDownload as PatternComponent,
page: Page as PatternComponent,
'page-set': PageSet as PatternComponent,
paragraph: Paragraph as PatternComponent,
Expand Down
16 changes: 14 additions & 2 deletions packages/design/src/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,22 @@ export default function Form({
className="usa-form margin-bottom-3 maxw-full"
onSubmit={
onSubmit
? formMethods.handleSubmit(async data => {
? formMethods.handleSubmit(async (data, event) => {
const submitEvent = event?.nativeEvent as
| SubmitEvent
| undefined;
if (submitEvent === undefined) {
console.error(
"Can't handle submission without event"
);
return;
}
const action = (
submitEvent.submitter as HTMLButtonElement
)?.value;
updatePrompt(data);
console.log('Submitting form...');
onSubmit(data);
onSubmit({ ...data, action });
})
: undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const sidebarPatterns: DropdownPattern[] = [
['paragraph', defaultFormConfig.patterns['paragraph']],
['rich-text', defaultFormConfig.patterns['rich-text']],
['radio-group', defaultFormConfig.patterns['radio-group']],
['package-download', defaultFormConfig.patterns['package-download']],
] as const;
export const fieldsetPatterns: DropdownPattern[] = [
['form-summary', defaultFormConfig.patterns['form-summary']],
Expand All @@ -102,6 +103,7 @@ export const fieldsetPatterns: DropdownPattern[] = [
['paragraph', defaultFormConfig.patterns['paragraph']],
['rich-text', defaultFormConfig.patterns['rich-text']],
['radio-group', defaultFormConfig.patterns['radio-group']],
['package-download', defaultFormConfig.patterns['package-download']],
] as const;

export const SidebarAddPatternMenuItem = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import classnames from 'classnames';
import React from 'react';

import { enLocale as message } from '@atj/common';
import {
type PackageDownloadPattern,
type PackageDownloadProps,
type PatternId,
} from '@atj/forms';

import PackageDownload from '../../../Form/components/PackageDownload/index.js';
import { PatternEditComponent } from '../types.js';

import { PatternEditActions } from './common/PatternEditActions.js';
import { PatternEditForm } from './common/PatternEditForm.js';
import { usePatternEditFormContext } from './common/hooks.js';
import { useFormManagerStore } from '../../store.js';

const PackageDownloadPatternEdit: PatternEditComponent<
PackageDownloadProps
> = ({ focus, previewProps }) => {
return (
<>
{focus ? (
<PatternEditForm
pattern={focus.pattern}
editComponent={<EditComponent patternId={focus.pattern.id} />}
></PatternEditForm>
) : (
<div className="padding-left-3 padding-bottom-3 padding-right-3">
<PackageDownload {...previewProps} />
</div>
)}
</>
);
};

const EditComponent = ({ patternId }: { patternId: PatternId }) => {
const pattern = useFormManagerStore<PackageDownloadPattern>(
state => state.session.form.patterns[patternId]
);
const { fieldId, getFieldState, register } =
usePatternEditFormContext<PackageDownloadPattern>(patternId);
const text = getFieldState('text');

return (
<div className="grid-row grid-gap-1">
<div className="tablet:grid-col-12">
<label
className={classnames('usa-label', {
'usa-label--error': text.error,
})}
htmlFor={fieldId('text')}
>
{message.patterns.packageDownload.fieldLabel}
</label>
{text.error ? (
<span className="usa-error-message" role="alert">
{text.error.message}
</span>
) : null}
<textarea
id={fieldId('text')}
className="usa-textarea bg-primary-lighter text-bold"
style={{ height: 'unset' }}
rows={4}
{...register('text')}
defaultValue={pattern.data.text}
autoFocus
></textarea>
</div>
<PatternEditActions />
</div>
);
};

export default PackageDownloadPatternEdit;
2 changes: 2 additions & 0 deletions packages/design/src/FormManager/FormEdit/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CheckboxPatternEdit from './CheckboxPatternEdit.js';
import FieldsetEdit from './FieldsetEdit.js';
import FormSummaryEdit from './FormSummaryEdit.js';
import InputPatternEdit from './InputPatternEdit.js';
import PackageDownloadPatternEdit from './PackageDownloadPatternEdit.js';
import { PageEdit } from './PageEdit.js';
import PageSetEdit from './PageSetEdit.js';
import ParagraphPatternEdit from './ParagraphPatternEdit.js';
Expand All @@ -21,6 +22,7 @@ export const defaultPatternEditComponents: EditComponentForPattern = {
input: InputPatternEdit as PatternEditComponent,
'form-summary': FormSummaryEdit as PatternEditComponent,
fieldset: FieldsetEdit as PatternEditComponent,
'package-download': PackageDownloadPatternEdit as PatternEditComponent,
page: PageEdit as PatternEditComponent,
'page-set': PageSetEdit as PatternEditComponent,
'radio-group': RadioGroupPatternEdit as PatternEditComponent,
Expand Down
9 changes: 8 additions & 1 deletion packages/forms/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import {
getPatternConfig,
} from './pattern.js';
import { type FormSession, nullSession, sessionIsComplete } from './session.js';
import { type ActionName } from './submission.js';

export type PackageDownloadProps = PatternProps<{
type: 'package-download';
text: string;
actions: PromptAction[];
}>;

export type TextInputProps = PatternProps<{
type: 'input';
Expand Down Expand Up @@ -92,7 +99,7 @@ export type PatternProps<T = {}> = {

export type SubmitAction = {
type: 'submit';
submitAction: 'next' | 'submit';
submitAction: 'next' | 'submit' | ActionName;
text: string;
};
export type LinkAction = {
Expand Down
Loading
Loading