Skip to content

Commit

Permalink
Address pattern hashed out more; to ease testing, add a button to cre…
Browse files Browse the repository at this point in the history
…ate a new empty blueprint.
  • Loading branch information
danielnaab committed Apr 3, 2024
1 parent e197d32 commit 91a62f7
Show file tree
Hide file tree
Showing 19 changed files with 357 additions and 63 deletions.
6 changes: 3 additions & 3 deletions packages/design/src/FormManager/DocumentImporter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { type FormService } from '@atj/form-service';

import Form, { FormUIContext } from '../../Form';
import { onFileInputChangeGetFile } from '../FormList/PDFFileSelect/file-input';
import { onFileInputChangeGetFile } from '../FormList/CreateNew/file-input';

const DocumentImporter = ({
baseUrl,
Expand Down Expand Up @@ -69,7 +69,7 @@ const DocumentImporter = ({
}
};

const PDFFileSelect = () => {
const CreateNew = () => {
return (
<div className="usa-form-group">
<label
Expand Down Expand Up @@ -175,7 +175,7 @@ const DocumentImporter = ({
<Step title="Preview form" step={3} current={state.page} />
</ol>
</div>
{state.page === 1 && <PDFFileSelect />}
{state.page === 1 && <CreateNew />}
{state.page === 2 && <BuildFormPage />}
{state.page === 3 && <PreviewFormPage />}
</div>
Expand Down
4 changes: 2 additions & 2 deletions packages/design/src/FormManager/FormEdit/AddPattern.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export const AddPattern = () => {
return (
<fieldset>
<label className="usa-label">
Add a pattern:
Add a pattern
<select
className="usa-select"
onChange={event => {
store.addPattern(event.target.value);
event.target.selectedIndex = 0;
}}
>
<option>Add a pattern</option>
<option></option>
{store.availablePatterns.map((pattern, index) => (
<option key={index} value={pattern.patternType}>
{pattern.displayName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import type { Meta, StoryObj } from '@storybook/react';

import PDFFileSelect from '.';
import CreateNew from '.';

export default {
title: 'FormManager/FormList/PDFFileSelect',
component: PDFFileSelect,
title: 'FormManager/FormList/CreateNew',
component: CreateNew,
decorators: [
(Story, args) => (
<MemoryRouter initialEntries={['/']}>
Expand All @@ -18,6 +18,6 @@ export default {
baseUrl: '/',
},
tags: ['autodocs'],
} satisfies Meta<typeof PDFFileSelect>;
} satisfies Meta<typeof CreateNew>;

export const PDFFileSelectTest = {} satisfies StoryObj<typeof PDFFileSelect>;
export const CreateNewTest = {} satisfies StoryObj<typeof CreateNew>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* @vitest-environment jsdom
*/
import { describeStories } from '../../../test-helper';
import meta, * as stories from './PDFFileSelect.stories';
import meta, * as stories from './CreateNew.stories';

describeStories(meta.title, stories);
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ export const useDocumentImporter = (
navigate(`/${result.data}/edit`);
}
},
createNewForm: async () => {
const builder = new FormBuilder();
builder.setFormSummary({
title: `My form - ${new Date().toISOString()}`,
description: '',
});
const result = await formService.addForm(builder.form);
if (result.success) {
navigate(`/${result.data}/edit`);
}
},
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormService } from '@atj/form-service';
import { onFileInputChangeGetFile } from './file-input';
import { useDocumentImporter } from './hooks';

export default function PDFFileSelect({
export default function CreateNew({
formService,
baseUrl,
}: {
Expand All @@ -16,6 +16,12 @@ export default function PDFFileSelect({
const { actions } = useDocumentImporter(formService, baseUrl);
return (
<div className="usa-form-group">
<button
className="usa-button usa-button--secondary"
onClick={() => actions.createNewForm()}
>
Create empty form
</button>
<label
className="usa-label"
id="file-input-specific-hint"
Expand Down
29 changes: 20 additions & 9 deletions packages/design/src/FormManager/FormList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Link } from 'react-router-dom';

import { FormService } from '@atj/form-service';
import PDFFileSelect from './PDFFileSelect';
import CreateNew from './CreateNew';

export default function FormList({
baseUrl,
Expand All @@ -22,27 +22,38 @@ export default function FormList({
<table className="usa-table usa-table--stacked">
<thead>
<tr>
<th className="column1" scope="col">Form title</th>
<th className="column2" scope="col">Description</th>
<th className="column3" scope="col">Actions</th>
<th className="column1" scope="col">
Form title
</th>
<th className="column2" scope="col">
Description
</th>
<th className="column3" scope="col">
Actions
</th>
</tr>
</thead>
<tbody>
{result.data.map((form, index) => (
<tr key={index}>
<th data-label="Form title" scope="row">{form.title}</th>
<th data-label="Form title" scope="row">
{form.title}
</th>
<td data-label="Description">{form.description}</td>
<td data-label="Actions" className="actionColumn">
{/* <span><Link to={`/${form.id}`}>Preview</Link></span> */}
<span><Link to={`/${form.id}/edit`}>Edit</Link></span>
<span><Link to={`/${form.id}/delete`}>Delete</Link></span>
<span>
<Link to={`/${form.id}/edit`}>Edit</Link>
</span>
<span>
<Link to={`/${form.id}/delete`}>Delete</Link>
</span>
</td>
</tr>
))}
</tbody>
</table>

<PDFFileSelect baseUrl={baseUrl} formService={formService} />
<CreateNew baseUrl={baseUrl} formService={formService} />
</>
);
}
Empty file.
Empty file.
147 changes: 147 additions & 0 deletions packages/design/src/config/view/Address/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import React from 'react';
import { useFormContext } from 'react-hook-form';

import { type PatternComponent } from '../../../Form';
import { AddressComponentProps } from '@atj/forms/src/patterns/address';
import classNames from 'classnames';

const Address: PatternComponent<AddressComponentProps> = props => {
const { register } = useFormContext();
return (
<fieldset className="usa-fieldset">
<legend className="usa-legend usa-legend--large">Mailing address</legend>
<label
className={classNames('usa-label', {
'usa-label--error': props.childProps.streetAddress.error,
})}
htmlFor={props.childProps.streetAddress.inputId}
>
{props.childProps.streetAddress.label}
{props.childProps.streetAddress.required && (
<abbr title="required" className="usa-hint usa-hint--required">
*
</abbr>
)}
</label>
<input
className="usa-input"
defaultValue={props.childProps.streetAddress.value}
{...register(props.childProps.streetAddress.inputId, {
//required: props.childProps.streetAddress.required,
})}
/>
<label
className={classNames('usa-label', {
'usa-label--error': props.childProps.streetAddress2.error,
})}
htmlFor={props.childProps.streetAddress2.inputId}
>
{props.childProps.streetAddress2.label}
{props.childProps.streetAddress2.required && (
<abbr title="required" className="usa-hint usa-hint--required">
*
</abbr>
)}
</label>
<input
className="usa-input"
defaultValue={props.childProps.streetAddress2.value}
{...register(props.childProps.streetAddress2.inputId, {
//required: props.childProps.streetAddress2.required,
})}
/>
<label
className={classNames('usa-label', {
'usa-label--error': props.childProps.city.error,
})}
htmlFor="city"
>
{props.childProps.city.label}
{props.childProps.city.required && (
<abbr title="required" className="usa-hint usa-hint--required">
*
</abbr>
)}
</label>
<input
className="usa-input"
defaultValue={props.childProps.city.value}
{...register(props.childProps.city.inputId, {
//required: props.childProps.city.required,
})}
/>
<label
className={classNames('usa-label', {
'usa-label--error':
props.childProps.stateTerritoryOrMilitaryPost.error,
})}
htmlFor={props.childProps.stateTerritoryOrMilitaryPost.inputId}
>
{props.childProps.stateTerritoryOrMilitaryPost.label}
{props.childProps.stateTerritoryOrMilitaryPost.required && (
<abbr title="required" className="usa-hint usa-hint--required">
*
</abbr>
)}
</label>
<select
className="usa-select"
defaultValue={props.childProps.stateTerritoryOrMilitaryPost.value}
{...register(props.childProps.stateTerritoryOrMilitaryPost.inputId, {
//required: props.childProps.stateTerritoryOrMilitaryPost.required,
})}
>
<option value="">- Select -</option>
{props.childProps.stateTerritoryOrMilitaryPost.options.map(
(jurisdiction, index) => (
<option key={index} value={jurisdiction.abbr}>
{jurisdiction.label}
</option>
)
)}
</select>
<label
className={classNames('usa-label', {
'usa-label--error': props.childProps.zipCode.error,
})}
htmlFor={props.childProps.zipCode.inputId}
>
{props.childProps.zipCode.label}
{props.childProps.zipCode.required && (
<abbr title="required" className="usa-hint usa-hint--required">
*
</abbr>
)}
</label>
<input
className="usa-input usa-input--medium"
pattern="[\d]{5}(-[\d]{4})?"
defaultValue={props.childProps.zipCode.value}
{...register(props.childProps.zipCode.inputId, {
//required: props.childProps.zipCode.required,
})}
/>
<label
className={classNames('usa-label', {
'usa-label--error': props.childProps.urbanizationCode.error,
})}
htmlFor={props.childProps.urbanizationCode.inputId}
>
{props.childProps.urbanizationCode.label}
{props.childProps.urbanizationCode.required && (
<abbr title="required" className="usa-hint usa-hint--required">
*
</abbr>
)}
</label>
<input
className="usa-input"
defaultValue={props.childProps.urbanizationCode.value}
{...register(props.childProps.urbanizationCode.inputId, {
//required: props.childProps.urbanizationCode.required,
})}
/>
</fieldset>
);
};
export default Address;
24 changes: 24 additions & 0 deletions packages/design/src/config/view/ZipCode/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { useFormContext } from 'react-hook-form';

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

export const ZipCode = (props: ZipcodeProps) => {
const { register } = useFormContext();
return (
<>
<label className="usa-label" htmlFor="zip">
ZIP code
</label>
<input
className="usa-input usa-input--medium"
id={`input-${props.inputId}`}
defaultValue={props.value}
{...register(props.inputId, {
//required: props.required,
})}
pattern="[\d]{5}(-[\d]{4})?"
/>
</>
);
};
2 changes: 2 additions & 0 deletions packages/design/src/config/view/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Address from './Address';
import Fieldset from './Fieldset';
import FormSummary from './FormSummary';
import Paragraph from './Paragraph';
Expand All @@ -7,6 +8,7 @@ import TextInput from './TextInput';
import { type ComponentForPattern } from '../../Form';

export const defaultPatternComponents: ComponentForPattern = {
address: Address,
fieldset: Fieldset,
'form-summary': FormSummary,
input: TextInput,
Expand Down
7 changes: 6 additions & 1 deletion packages/forms/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ export type FieldsetProps = PatternProps<{
legend?: string;
}>;

export type ZipcodeProps = PatternProps<{
type: 'zipcode';
inputId: string;
value: string;
}>;

export type PatternProps<T = {}> = {
_patternId: PatternId;
_children: PromptPart[];
type: string;
} & T;

Expand Down
Loading

0 comments on commit 91a62f7

Please sign in to comment.