Skip to content

Commit

Permalink
Page and fieldset fixes (#161)
Browse files Browse the repository at this point in the history
* Don't scroll on page-set click; filter out empty values from hidden patterns input on page edit form

* Store hidden field for fieldset elements on edit form. TODO: remove the need for this
  • Loading branch information
danielnaab authored Jun 5, 2024
1 parent dc1fa41 commit acce251
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const PreviewPattern: PatternComponent = function PreviewPattern(props) {
if (
focus &&
focus.pattern.id === props._patternId &&
focus.pattern.type !== 'page' &&
focusRef.current?.scrollIntoView
) {
focusRef.current?.scrollIntoView({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ const EditComponent = ({ patternId }: { patternId: PatternId }) => {
return (
<div className="grid-row">
<div className="grid-col-12 margin-bottom-3 flex-align-self-end">
<input
type="hidden"
{...register('patterns')}
defaultValue={pattern.data.patterns}
></input>
<label
className={classNames('usa-label width-full maxw-full', {
'usa-label--error': legend.error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const PageEditComopnent = ({ pattern }: { pattern: PagePattern }) => {
const { fieldId, getFieldState, register } =
usePatternEditFormContext<PagePattern>(pattern.id);
const title = getFieldState('title');

return (
<div className="grid-row grid-gap">
<input
Expand All @@ -78,10 +77,10 @@ const PageEditComopnent = ({ pattern }: { pattern: PagePattern }) => {
) : null}
<input
className="usa-input"
type="text"
id={fieldId('title')}
defaultValue={pattern.data.title}
{...register('title')}
type="text"
></input>
</div>
<div className="grid-col-12">
Expand Down
4 changes: 4 additions & 0 deletions packages/design/src/FormManager/FormEdit/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export const createFormEditSlice =
return false;
}
const elementToSet = getPattern(state.session.form, patternId);
if (elementToSet.type === 'page-set') {
console.warn('Cannot focus on a page or page-set.', elementToSet.type);
return false;
}
if (elementToSet) {
set({ focus: { errors: undefined, pattern: elementToSet } });
} else {
Expand Down
17 changes: 16 additions & 1 deletion packages/forms/src/patterns/fieldset/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,22 @@ import { ParsePatternConfigData } from '../../pattern';

const configSchema = z.object({
legend: z.string().min(1),
patterns: z.array(z.string()).default([]),
patterns: z.union([
// Support either an array of strings...
z.array(z.string()),
// ...or a comma-separated string.
// REVISIT: This is messy, and exists only so we can store the data easily
// as a hidden input in the form. We should probably just store it as JSON.
z
.string()
.transform(value =>
value
.split(',')
.map(String)
.filter(value => value)
)
.pipe(z.string().array()),
]),
});
export type FieldsetConfigSchema = z.infer<typeof configSchema>;

Expand Down
7 changes: 6 additions & 1 deletion packages/forms/src/patterns/page/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ const configSchema = z.object({
// as a hidden input in the form. We should probably just store it as JSON.
z
.string()
.transform(value => value.split(',').map(String))
.transform(value =>
value
.split(',')
.map(String)
.filter(value => value)
)
.pipe(z.string().array()),
]),
});
Expand Down
11 changes: 4 additions & 7 deletions packages/forms/src/patterns/page/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ export const createPrompt: CreatePrompt<PagePattern> = (
pattern,
options
) => {
// :TODO: There's an edge case with page saving where it's being saved with an empty string value. Figure out root cause.
const children = pattern.data.patterns
.filter((patternId: string) => patternId.length > 0)
.map((patternId: string) => {
const childPattern = getPattern(session.form, patternId);
return createPromptForPattern(config, session, childPattern, options);
});
const children = pattern.data.patterns.map((patternId: string) => {
const childPattern = getPattern(session.form, patternId);
return createPromptForPattern(config, session, childPattern, options);
});
return {
props: {
_patternId: pattern.id,
Expand Down

0 comments on commit acce251

Please sign in to comment.