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

Mock api / Throwaway Edit UI Changes #50

Merged
merged 8 commits into from
Feb 27, 2024
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
8 changes: 2 additions & 6 deletions packages/design/src/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ComponentForPattern<T extends Pattern = Pattern<unknown>> = Record<

export type FormElementComponent<T extends Pattern = Pattern<unknown>> =
React.ComponentType<{
prompt: T;
pattern: T;
}>;

const usePrompt = (
Expand Down Expand Up @@ -103,12 +103,8 @@ export default function Form({
<fieldset className="usa-fieldset">
{prompt.parts
.map((pattern, index) => {
if (pattern.type === 'text') {
console.log('skipping', pattern.type);
return null;
}
const Component = context.components[pattern.type];
return <Component key={index} prompt={pattern} />;
return <Component key={index} pattern={pattern} />;
})
.filter(a => a)}
{/* Add submit button or other controls as needed */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const FormElementEdit = ({
formElement.id,
formData
);
//onChange(updatedForm);
onChange(updatedForm);
})}
>
<input className="usa-button" type="submit" value="Save" />
Expand Down
16 changes: 10 additions & 6 deletions packages/design/src/FormManager/FormEdit/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const PreviewForm = ({ uiContext, form }: PreviewFormProps) => {
// don't have to regenerate it every time we render the form.
components: createPreviewComponents(uiContext.components),
};
console.log('creating session');
const disposable = createFormSession(form); // nullSession instead?
return <Form context={previewUiContext} session={disposable}></Form>;
};
Expand Down Expand Up @@ -56,24 +55,29 @@ export const PreviewContext = createContext<PreviewContextValue>(
);

const createPatternPreviewComponent = (Component: FormElementComponent) => {
return function PatternPreviewComponent({ prompt }: { prompt: Pattern }) {
const PatternPreviewComponent: FormElementComponent = ({
pattern,
}: {
pattern: Pattern;
}) => {
const { selectedId, setSelectedId } = useContext(PreviewContext);
return (
<div
onClick={() => {
setSelectedId(prompt._elementId);
setSelectedId(pattern._elementId);
}}
className={classNames({
'bg-primary-lighter': selectedId === prompt._elementId,
'bg-primary-lighter': selectedId === pattern._elementId,
})}
//onKeyDown={handleKeyDown}
role="button"
aria-pressed={selectedId === prompt._elementId}
aria-pressed={selectedId === pattern._elementId}
aria-label="Select this pattern"
tabIndex={0}
>
<Component prompt={prompt} />
<Component pattern={pattern} />
</div>
);
};
return PatternPreviewComponent;
};
8 changes: 3 additions & 5 deletions packages/design/src/FormManager/FormEdit/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import React, { useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';

import { type FormService } from '@atj/form-service';
import {
type FormDefinition,
type FormElementMap,
getRootFormElement,
updateElements,
FormElementId,
getFormElement,
} from '@atj/forms';
Expand Down Expand Up @@ -45,13 +42,13 @@
const EditForm = ({
context,
initialForm,
onSave,

Check failure on line 45 in packages/design/src/FormManager/FormEdit/index.tsx

View workflow job for this annotation

GitHub Actions / run-tests (ubuntu-latest, x86_64)

'onSave' is defined but never used
}: {
context: FormEditUIContext;
initialForm: FormDefinition;
onSave: (form: FormDefinition) => void;
}) => {
const [currentForm, setCurrentForm] = useState(initialForm);

Check failure on line 51 in packages/design/src/FormManager/FormEdit/index.tsx

View workflow job for this annotation

GitHub Actions / run-tests (ubuntu-latest, x86_64)

'setCurrentForm' is assigned a value but never used
const [selectedId, setSelectedId] = useState<FormElementId>();
const rootField = getRootFormElement(currentForm);
const formElement = getFormElement(currentForm, selectedId || rootField.id);
Expand All @@ -78,8 +75,9 @@
context={context}
initialForm={currentForm}
formElement={formElement}
onChange={function (form: FormDefinition): void {
setCurrentForm(form);
onChange={(form: FormDefinition) => {
//setCurrentForm(form);
console.log('setting form onChange', form);
}}
/>
)}
Expand Down
84 changes: 45 additions & 39 deletions packages/design/src/config/edit/InputElementEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,58 @@ const InputElementEdit: FormElementEditComponent<InputElement> = ({
}) => {
const { register } = useFormContext();
return (
<div className="grid-row grid-gap">
<div className="grid-col grid-col-4">
<label className="usa-label">
Field label
<input
className="usa-input"
{...register(`${element.id}.data.text`)}
type="text"
></input>
</label>
</div>
<div className="grid-col grid-col-2">
<label className="usa-label">
Default field value
<input
className="usa-input"
type="text"
{...register(`${element.id}.data.initial`)}
></input>
</label>
</div>
<div className="grid-col grid-col-2">
<label className="usa-label">
Maximum length
<input
className="usa-input"
type="text"
{...register(`${element.id}.data.maxLength`)}
></input>
</label>
</div>
<div className="grid-col grid-col-2">
<label className="usa-label">
Field type
<select className="usa-select" {...register(`${element.id}.type`)}>
<option value={'input'}>Input</option>
</select>
</label>
<div>
<div className="grid-row grid-gap padding-left-5">
<div className="grid-col grid-col-4 flex-fill flex-align-self-end">
<label className="usa-label">
Input
<p className="usa-hint font-ui-3xs">Instructions:</p>
<input
className="usa-input"
{...register(`${element.id}.data.instructions`)}
type="text"
placeholder={`Short answer: ${element.data.maxLength} chars`}
></input>
</label>
</div>
{/* <div className="grid-col grid-col-2 flex-align-self-end">
<label className="usa-label">
<p className="usa-hint font-ui-3xs">Max length</p>
<input
className="usa-input"
type="text"
{...register(`${element.id}.data.maxLength`)}
></input>
</label>
</div> */}
<div className="grid-col grid-col-4 flex-align-self-end">
<label className="usa-label">
<p className="usa-hint font-ui-3xs">Input type</p>
<select className="usa-select" {...register(`${element.id}.type`)}>
<option
value={'input'}
>{`${element.default} (${element.data.maxLength} chars)`}</option>
<option value={'input'}>Long Answer (500 chars)</option>{' '}
{/* this is a stub */}
</select>
</label>
</div>
</div>
<div className="grid-col grid-col-2">
<div className="grid-row grid-gap padding-left-5 flex-justify-end">
<div>
<label className="usa-label">
<p className="usa-hint font-ui-3xs">
PDF Field ID: {`${element.data.text}`}
</p>
</label>
</div>
<div className="usa-checkbox">
<input
className="usa-checkbox__input"
type="checkbox"
id={`${element.id}.required`}
{...register(`${element.id}.data.required`)}
checked={element.required}
/>
<label
className="usa-checkbox__label"
Expand Down
38 changes: 38 additions & 0 deletions packages/design/src/config/edit/ParagraphElementEdit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { useFormContext } from 'react-hook-form';

import { type FormElementComponent } from '../../Form';
import { type ParagraphElement } from '@atj/forms/src/config/elements/paragraph';

const ParagraphElementEdit: FormElementComponent<ParagraphElement> = ({
element,
}) => {
const { register } = useFormContext();
return (
<div className="grid-row grid-gap">
<div className="grid-col grid-col-10 flex-align-self-end">
<label className="usa-label">
Text Element
<input
className="usa-input"
{...register(`${element.id}.data.text`)}
type="text"
></input>
</label>
</div>
<div className="grid-col grid-col-2 flex-align-self-end">
<label className="usa-label">
<p className="usa-hint font-ui-3xs">Style</p>
<select className="usa-select" {...register(`${element.id}.type`)}>
<option value={'paragraph'}>Question</option> {/* this is a stub */}
<option value={'paragraph'}>Title</option> {/* this is a stub */}
<option value={'paragraph'}>Instructions</option>{' '}
{/* this is a stub */}
</select>
</label>
</div>
</div>
);
};

export default ParagraphElementEdit;
2 changes: 1 addition & 1 deletion packages/design/src/config/edit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import InputElementEdit from './InputElementEdit';
import SequenceElementEdit from './SequenceElementEdit';
import { type ComponentForPattern } from 'config/view';
import { ComponentForPattern } from '../../Form';

export type FormEditUIContext = {
config: FormConfig;
Expand Down
17 changes: 17 additions & 0 deletions packages/design/src/config/view/Fieldset/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

import { type Pattern } from '@atj/forms';
import { type ParagraphElement } from '@atj/forms/src/config/elements/paragraph';

import { type FormElementComponent } from '../../../Form';

const FormSummary: FormElementComponent<Pattern<ParagraphElement>> = ({
pattern,
}) => {
return (
<>
<p>{pattern.data.text}</p>
</>
);
};
export default FormSummary;
6 changes: 3 additions & 3 deletions packages/design/src/config/view/FormSummary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Pattern, type FormSummaryPattern } from '@atj/forms';
import { type FormElementComponent } from '../../../Form';

const FormSummary: FormElementComponent<Pattern<FormSummaryPattern>> = ({
prompt,
pattern,
}) => {
return (
<>
<legend className="usa-legend usa-legend--large">{prompt.title}</legend>
<p>{prompt.description}</p>
<legend className="usa-legend usa-legend--large">{pattern.title}</legend>
<p>{pattern.description}</p>
</>
);
};
Expand Down
17 changes: 17 additions & 0 deletions packages/design/src/config/view/Paragraph/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

import { type Pattern } from '@atj/forms';
import { type ParagraphElement } from '@atj/forms/src/config/elements/paragraph';

import { type FormElementComponent } from '../../../Form';

const FormSummary: FormElementComponent<Pattern<ParagraphElement>> = ({
pattern,
}) => {
return (
<>
<p>{pattern.data.text}</p>
</>
);
};
export default FormSummary;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FormElementComponent } from '../../../Form';

const SubmissionConfirmation: FormElementComponent<
Pattern<SubmissionConfirmationPattern>
> = ({ prompt }) => {
> = ({ pattern }) => {
return (
<>
<legend className="usa-legend usa-legend--large">
Expand All @@ -19,7 +19,7 @@ const SubmissionConfirmation: FormElementComponent<
</tr>
</thead>
<tbody>
{prompt.table.map((row, index) => {
{pattern.table.map((row, index) => {
return (
<tr key={index}>
<td>{row.label}</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const Required = {
args: {
prompt: {
_elementId: '',
type: 'text',
type: 'input',
inputId: 'test-prompt',
value: '',
label: 'Please enter your first name.',
Expand All @@ -41,7 +41,7 @@ export const NotRequired = {
args: {
prompt: {
_elementId: '',
type: 'text',
type: 'input',
inputId: 'test-prompt',
value: '',
label: 'Please enter your first name.',
Expand Down
30 changes: 14 additions & 16 deletions packages/design/src/config/view/TextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,45 @@ import { useFormContext } from 'react-hook-form';
import { Pattern, type TextInputPattern } from '@atj/forms';
import { type FormElementComponent } from '../../../Form';

export type TextInputProps = { prompt: Pattern<TextInputPattern> };

const TextInput: FormElementComponent<Pattern<TextInputPattern>> = ({
prompt,
pattern,
}) => {
const { register } = useFormContext();
return (
<div className="usa-form-group" key={prompt.inputId}>
<div className="usa-form-group" key={pattern.inputId}>
<div
className={classNames('usa-form-group', {
'usa-form-group--error': prompt.error,
'usa-form-group--error': pattern.error,
})}
>
<label
className={classNames('usa-label', {
'usa-label--error': prompt.error,
'usa-label--error': pattern.error,
})}
htmlFor="input-error"
>
{prompt.label}
{pattern.label}
</label>
{prompt.error && (
{pattern.error && (
<span
className="usa-error-message"
id={`input-error-message-${prompt.inputId}`}
id={`input-error-message-${pattern.inputId}`}
role="alert"
>
{prompt.error}
{pattern.error}
</span>
)}
<input
className={classNames('usa-input', {
'usa-input--error': prompt.error,
'usa-input--error': pattern.error,
})}
id={`input-${prompt.inputId}`}
defaultValue={prompt.value}
{...register(prompt.inputId, {
//required: prompt.required,
id={`input-${pattern.inputId}`}
defaultValue={pattern.value}
{...register(pattern.inputId, {
//required: pattern.required,
})}
type="text"
aria-describedby={`input-message-${prompt.inputId}`}
aria-describedby={`input-message-${pattern.inputId}`}
/>
</div>
</div>
Expand Down
Loading
Loading