Skip to content

Commit

Permalink
Merge pull request #10 from eBay/fix-utils-and-examples
Browse files Browse the repository at this point in the history
Fix utils and examples
  • Loading branch information
supnate authored May 22, 2024
2 parents b947c82 + 08a4354 commit 89f0022
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 116 deletions.
108 changes: 55 additions & 53 deletions packages/examples-antd/src/examples/Wizard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useCallback, useState } from 'react';
import { Form, Button, Steps } from 'antd';
import NiceForm from '@ebay/nice-form-react';
import cloneDeep from 'lodash/cloneDeep';
import { NiceFormFieldType } from '@ebay/nice-form-react/lib/esm/NiceFormMeta';
import type { Dayjs } from 'dayjs';

Expand All @@ -22,60 +21,63 @@ interface WizardMeta {
steps: Step[];
}

const wizardMeta: WizardMeta = {
steps: [
{
title: 'Personal Information',
formMeta: {
columns: 2,
fields: [
{ key: 'name.first', label: 'First Name', initialValue: 'Nate', required: true },
{ key: 'name.last', label: 'Last Name', initialValue: 'Wang', required: true },
{ key: 'dob', label: 'Date of Birth', widget: 'date-picker', viewWidget: 'date-view' },
{
key: 'noAccountInfo',
label: 'No Account Info',
widget: 'switch',
// dynamic: true,
tooltip: 'Switch on to remove account step',
},
],
const getInitialMeta = () => {
const wizardMeta: WizardMeta = {
steps: [
{
title: 'Personal Information',
formMeta: {
columns: 2,
fields: [
{ key: 'name.first', label: 'First Name', initialValue: 'Nate', required: true },
{ key: 'name.last', label: 'Last Name', initialValue: 'Wang', required: true },
{ key: 'dob', label: 'Date of Birth', widget: 'date-picker', viewWidget: 'date-view' },
{
key: 'noAccountInfo',
label: 'No Account Info',
widget: 'switch',
// dynamic: true,
tooltip: 'Switch on to remove account step',
},
],
},
},
},
{
title: 'Account Information',
formMeta: {
columns: 2,
fields: [
{
key: 'email',
label: 'Email',
clear: 'right',
rules: [{ type: 'email', message: 'Invalid email' }],
},
{
key: 'security',
label: 'Security Question',
widget: 'select',
placeholder: 'Select a question...',
options: ["What's your pet's name?", 'Your nick name?'],
},
{ key: 'answer', label: 'Security Answer' },
],
{
title: 'Account Information',
formMeta: {
columns: 2,
fields: [
{
key: 'email',
label: 'Email',
clear: 'right',
rules: [{ type: 'email', message: 'Invalid email' }],
},
{
key: 'security',
label: 'Security Question',
widget: 'select',
placeholder: 'Select a question...',
options: ["What's your pet's name?", 'Your nick name?'],
},
{ key: 'answer', label: 'Security Answer' },
],
},
},
},
{
title: 'Contact Information',
formMeta: {
columns: 2,
fields: [
{ key: 'address', label: 'Address', colSpan: 2 },
{ key: 'city', label: 'City' },
{ key: 'phone', label: 'phone' },
],
{
title: 'Contact Information',
formMeta: {
columns: 2,
fields: [
{ key: 'address', label: 'Address', colSpan: 2 },
{ key: 'city', label: 'City' },
{ key: 'phone', label: 'phone' },
],
},
},
},
],
],
};
return wizardMeta;
};

export default () => {
Expand All @@ -87,7 +89,7 @@ export default () => {
}, [form]);

// Clone the meta for dynamic change
const newWizardMeta = cloneDeep(wizardMeta);
const newWizardMeta = getInitialMeta();
if (form.getFieldValue('noAccountInfo')) {
newWizardMeta.steps.splice(1, 1);
}
Expand Down
123 changes: 62 additions & 61 deletions packages/examples-formik/src/examples/Wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import {
FormikMuiNiceFormMeta,
FormikMuiNiceFormField,
} from '@ebay/nice-form-react/adapters/formikMuiAdapter';
import cloneDeep from 'lodash/cloneDeep';
const DateView = ({ value }: { value: Dayjs }) => (value ? value.format('MMM Do YYYY') : 'N/A');

NiceForm.defineWidget('date-view', DateView, ({ field }) => field);
NiceForm.defineWidget('date-view', DateView);

interface StepItem {
title: string;
Expand All @@ -38,73 +37,75 @@ interface FormValues {
phone: string;
}

const wizardMeta = {
steps: [
{
title: 'Personal Information',
formMeta: {
columns: 2,
rowGap: 18,
columnGap: 18,
fields: [
{ key: 'name.first', label: 'First Name', initialValue: 'Nate', required: true },
{ key: 'name.last', label: 'Last Name', initialValue: 'Wang', required: true },
{ key: 'dob', label: 'Date of Birth', widget: 'date-picker', viewWidget: 'date-view' },
{
key: 'noAccountInfo',
label: 'No Account Info',
widget: 'switch',
tooltip: 'Switch on to remove account step',
},
],
const getInitialMeta = () => {
const wizardMeta = {
steps: [
{
title: 'Personal Information',
formMeta: {
columns: 2,
rowGap: 18,
columnGap: 18,
fields: [
{ key: 'name.first', label: 'First Name', initialValue: 'Nate', required: true },
{ key: 'name.last', label: 'Last Name', initialValue: 'Wang', required: true },
{ key: 'dob', label: 'Date of Birth', widget: 'date-picker', viewWidget: 'date-view' },
{
key: 'noAccountInfo',
label: 'No Account Info',
widget: 'switch',
tooltip: 'Switch on to remove account step',
},
],
},
},
},
{
title: 'Account Information',
formMeta: {
columns: 2,
rowGap: 18,
columnGap: 18,
fields: [
{
key: 'email',
label: 'Email',
clear: 'right',
rules: [{ type: 'email', message: 'Invalid email' }],
},
{
key: 'security',
label: 'Security Question',
widget: 'select',
fullWidth: true,
placeholder: 'Select a question...',
options: ["What's your pet's name?", 'Your nick name?'],
},
{ key: 'answer', label: 'Security Answer' },
],
{
title: 'Account Information',
formMeta: {
columns: 2,
rowGap: 18,
columnGap: 18,
fields: [
{
key: 'email',
label: 'Email',
clear: 'right',
rules: [{ type: 'email', message: 'Invalid email' }],
},
{
key: 'security',
label: 'Security Question',
widget: 'select',
fullWidth: true,
placeholder: 'Select a question...',
options: ["What's your pet's name?", 'Your nick name?'],
},
{ key: 'answer', label: 'Security Answer' },
],
},
},
},
{
title: 'Contact Information',
formMeta: {
columns: 2,
rowGap: 18,
columnGap: 18,
fields: [
{ key: 'address', label: 'Address', colSpan: 2 },
{ key: 'city', label: 'City' },
{ key: 'phone', label: 'phone' },
],
{
title: 'Contact Information',
formMeta: {
columns: 2,
rowGap: 18,
columnGap: 18,
fields: [
{ key: 'address', label: 'Address', colSpan: 2 },
{ key: 'city', label: 'City' },
{ key: 'phone', label: 'phone' },
],
},
},
},
],
] as StepItem[],
};
return wizardMeta;
};

const Wizard = () => {
const [currentStep, setCurrentStep] = useState(0);

// Clone the meta for dynamic change
const newWizardMeta = cloneDeep(wizardMeta);
const newWizardMeta = getInitialMeta();

// Generate a general review step
const reviewFields: object[] = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/nice-form-react/src/NiceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ NiceForm.addAdapter = (adapter: NiceFormAdapter) => {
NiceForm.defineWidget = (
name: string,
widget: ReactComponent,
metaConverter: FieldMetaConverter,
metaConverter?: FieldMetaConverter,
) => {
config.defineWidget(name, widget, metaConverter);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/nice-form-react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export interface NiceFormConfig {
defineWidget: (
name: string,
widget: ReactComponent,
metaConverter: ({ meta, field }: { meta: NiceFormMeta; field: NiceFormField }) => NiceFormField,
metaConverter?: ({ meta, field }: { meta: NiceFormMeta; field: NiceFormField }) => NiceFormField,
) => void;

/**
Expand Down

0 comments on commit 89f0022

Please sign in to comment.