Skip to content

Commit

Permalink
Merge pull request #178 from open-formulieren/refactor/preview-to-pan…
Browse files Browse the repository at this point in the history
…el-preview

Refactor preview to panel preview
  • Loading branch information
sergei-maertens authored Aug 16, 2024
2 parents 4a175fa + 119bcff commit b79d8ab
Show file tree
Hide file tree
Showing 43 changed files with 194 additions and 191 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"/node_modules/(?!uuid)/"
],
"setupFilesAfterEnv": [
"<rootDir>/src/tests/setupTests.js"
"<rootDir>/src/tests/setupTests.ts"
],
"testEnvironment": "jsdom"
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ComponentEditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const ComponentEditForm: React.FC<ComponentEditFormProps> = ({

const registryEntry = getRegistryEntry(component);
const {edit: EditForm, editSchema: zodSchema} = registryEntry;
const hasPreview = registryEntry.preview !== null;
const hasPreview = registryEntry.preview.panel !== null;

let initialValues = cloneDeep(component);
if (isNew) {
Expand Down
9 changes: 6 additions & 3 deletions src/components/ComponentPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ const GenericComponentPreview: React.FC<GenericComponentPreviewProps> = ({
}) => {
const {key} = component;
const entry = getRegistryEntry(component);
const {preview: PreviewComponent, defaultValue = ''} = entry;
if (PreviewComponent === null) {
const {
preview: {panel: PanelPreviewComponent},
defaultValue = '',
} = entry;
if (PanelPreviewComponent === null) {
return null;
}
const isMultiple = hasOwnProperty(component, 'multiple') ? component.multiple : false;
Expand All @@ -106,7 +109,7 @@ const GenericComponentPreview: React.FC<GenericComponentPreviewProps> = ({
component={component}
initialValues={initialValues}
>
<PreviewComponent component={component} />
<PanelPreviewComponent component={component} />
</ComponentPreviewWrapper>
);
};
Expand Down
5 changes: 4 additions & 1 deletion src/components/ModeToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ function ModeToggle<T extends string>({
return (
<div className={clsx('btn-group', 'btn-group-toggle', className)}>
{modes.map(({value, label}) => (
<label className={clsx('btn', 'btn-sm', btnClassName, {active: value === currentMode})}>
<label
key={value}
className={clsx('btn', 'btn-sm', btnClassName, {active: value === currentMode})}
>
<input
type="radio"
name={name}
Expand Down
4 changes: 3 additions & 1 deletion src/registry/addressNL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {
panel: Preview,
},
defaultValue: {
postcode: '',
houseNumber: '',
Expand Down
2 changes: 1 addition & 1 deletion src/registry/bsn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: '',
} satisfies RegistryEntry<BsnComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: false, // formik field value
comparisonValue: ComparisonValueInput,
} satisfies RegistryEntry<CheckboxComponentSchema>;
7 changes: 5 additions & 2 deletions src/registry/columns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import {RegistryEntry} from '@/registry/types';

import EditForm from './edit';
import validationSchema from './edit-validation';
import Preview from './preview';
import PanelPreview from './panel-preview';
import './previews.scss';

export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {
panel: PanelPreview,
},
defaultValue: undefined, // a column component does not hold a value itself
} satisfies RegistryEntry<ColumnsComponentSchema>;
80 changes: 80 additions & 0 deletions src/registry/columns/panel-preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {ColumnsComponentSchema} from '@open-formulieren/types';
import clsx from 'clsx';
import {useState} from 'react';
import {FormattedMessage} from 'react-intl';

import ModeToggle from '@/components/ModeToggle';

import {ComponentPreviewProps} from '../types';

type ViewportMode = 'desktop' | 'mobile';

/**
* Show a formio columns component preview.
*/
const Preview: React.FC<ComponentPreviewProps<ColumnsComponentSchema>> = ({component}) => {
const [viewportMode, setViewportMode] = useState<ViewportMode>('desktop');

const {columns} = component;
const isMobile = viewportMode === 'mobile';
return (
<>
<div className="d-flex justify-content-end align-items-center">
<ModeToggle<ViewportMode>
name="previewViewport"
currentMode={viewportMode}
btnClassName="btn-light"
onToggle={setViewportMode}
modes={[
{
value: 'desktop',
label: (
<FormattedMessage
description="Columns 'desktop' preview viewport"
defaultMessage="Default"
/>
),
},
{
value: 'mobile',
label: (
<FormattedMessage
description="Columns 'mobile' preview viewport"
defaultMessage="Mobile"
/>
),
},
]}
/>
</div>

<div
className={clsx('offb-columns-panel-preview', {
'offb-columns-panel-preview--mobile': isMobile,
})}
>
{columns.map((column, index) => (
<div
key={`column-${index}`}
className="offb-columns-panel-preview__column"
style={
{
'--_col-preview-span': `${isMobile ? column.sizeMobile : column.size}`,
} as React.CSSProperties
}
>
<FormattedMessage
description="Column preview column description"
defaultMessage="Column {number}"
values={{
number: index + 1,
}}
/>
</div>
))}
</div>
</>
);
};

export default Preview;
83 changes: 0 additions & 83 deletions src/registry/columns/preview.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.offb-columns-preview {
.offb-columns-panel-preview {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 10px;
Expand Down
4 changes: 3 additions & 1 deletion src/registry/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import validationSchema from './edit-validation';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: null,
preview: {
panel: null,
},
defaultValue: undefined, // a content component does not hold a value itself
} satisfies RegistryEntry<ContentComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/cosignV1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
// component does not have a submission value but acts as a marker
defaultValue: undefined,
} satisfies RegistryEntry<CosignV1ComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/cosignV2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: '',
} satisfies RegistryEntry<CosignV2ComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/currency/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: undefined, // formik field value
comparisonValue: ComparisonValueInput,
} satisfies RegistryEntry<CurrencyComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: '',
} satisfies RegistryEntry<DateComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/datetime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: '',
} satisfies RegistryEntry<DateTimeComponentSchema>;
6 changes: 4 additions & 2 deletions src/registry/editgrid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {RegistryEntry} from '@/registry/types';

import EditForm from './edit';
import validationSchema from './edit-validation';
import Preview from './preview';
import PanelPreview from './panel-preview';

export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {
panel: PanelPreview,
},
defaultValue: [],
} satisfies RegistryEntry<EditGridComponentSchema>;
File renamed without changes.
2 changes: 1 addition & 1 deletion src/registry/email/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: '',
} satisfies RegistryEntry<EmailComponentSchema>;
6 changes: 4 additions & 2 deletions src/registry/fieldset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {RegistryEntry} from '@/registry/types';

import EditForm from './edit';
import validationSchema from './edit-validation';
import Preview from './preview';
import PanelPreview from './panel-preview';

export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {
panel: PanelPreview,
},
defaultValue: undefined, // a fieldset component does not hold a value itself
} satisfies RegistryEntry<FieldsetComponentSchema>;
File renamed without changes.
2 changes: 1 addition & 1 deletion src/registry/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: [],
} satisfies RegistryEntry<FileComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/iban/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: '',
} satisfies RegistryEntry<IbanComponentSchema>;
File renamed without changes.
2 changes: 1 addition & 1 deletion src/registry/licenseplate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: '',
} satisfies RegistryEntry<LicensePlateComponentSchema>;
2 changes: 1 addition & 1 deletion src/registry/map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import Preview from './preview';
export default {
edit: EditForm,
editSchema: validationSchema,
preview: Preview,
preview: {panel: Preview},
defaultValue: undefined,
} satisfies RegistryEntry<MapComponentSchema>;
Loading

0 comments on commit b79d8ab

Please sign in to comment.