Skip to content

docs(Form): add edit & display-only mode example #4957

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

Merged
merged 3 commits into from
Aug 10, 2023
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
118 changes: 118 additions & 0 deletions packages/main/src/components/Form/Form.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import * as ComponentStories from './Form.stories';

<DocsHeader since="0.7.0" />

**Note:** For more detailed examples of how the form can be used or integrated with other external libraries such as "React Hook Form", you can visit our [knowledge base](https://sap.github.io/ui5-webcomponents-react/?path=/docs/knowledge-base-form-handling--docs).

<br />

## Example
Expand Down Expand Up @@ -135,6 +137,122 @@ const FormComponent = (props) => {

</details>

## Display-Only and Edit mode

<Canvas of={ComponentStories.DisplayEditMode} sourceState="none" />

### Code

<details>

<summary>Show Code</summary>

```jsx
const StandardField = ({ editMode, value, inputType = InputType.None, onInput, ...rest }) => {
if (editMode) {
return <Input value={value} style={{ width: '100%' }} type={inputType} onInput={onInput} {...rest} />;
}
if (inputType === InputType.URL || inputType === InputType.Email) {
return (
<Link href={inputType === InputType.Email ? `mailto:${value}` : value} target="_blank" {...rest}>
{value}
</Link>
);
}
return <Text {...rest}>{value}</Text>;
};

const reducer = (state, { field, value }) => {
return { ...state, [field]: value };
};

const FormComponent = (props) => {
const [editMode, toggleEditMode] = useReducer((prev) => !prev, false, undefined);
const [formState, dispatch] = useReducer(
reducer,
{
name: 'Red Point Stores',
street: 'Main St 1618',
zip: 31415,
city: 'Maintown',
country: 'Germany',
web: 'https://www.sap.com',
mail: '[email protected]',
twitter: '@sap',
phone: '+49 1234 56789'
},
undefined
);
const { zip, city, name, street, country, web, mail, twitter, phone } = formState;

const handleInput = (e) => {
dispatch({ field: Object.keys(e.target.dataset)[0], value: e.target.value });
};

return (
<>
<Button onClick={toggleEditMode}>Toggle {editMode ? 'Display-Only Mode' : 'Edit Mode'}</Button>
<Form
{...props}
onSubmit={(e) => {
e.preventDefault();
}}
>
<FormGroup titleText="Address">
<FormItem label="Name">
<StandardField editMode={editMode} value={name} onInput={handleInput} data-name />
</FormItem>
<FormItem label="Street">
<StandardField editMode={editMode} value={street} onInput={handleInput} data-street />
</FormItem>
<FormItem label="ZIP Code / City">
{editMode ? (
<>
<Input value={zip} type={InputType.Number} style={{ width: '30%' }} onInput={handleInput} data-zip />
<Input value={city} style={{ width: '70%' }} onInput={handleInput} data-city />
</>
) : (
<Text>{`${zip} ${city}`}</Text>
)}
</FormItem>
<FormItem label="Country">
<StandardField editMode={editMode} value={country} onInput={handleInput} data-country />
</FormItem>
<FormItem label="Web">
<StandardField editMode={editMode} value={web} inputType={InputType.URL} onInput={handleInput} data-web />
</FormItem>
</FormGroup>
<FormGroup titleText="Contact">
<FormItem label="Email">
<StandardField
editMode={editMode}
value={mail}
inputType={InputType.Email}
onInput={handleInput}
data-email
/>
</FormItem>
<FormItem label="Twitter">
<StandardField editMode={editMode} value={twitter} onInput={handleInput} data-twitter />
</FormItem>
<FormItem label="Phone">
<StandardField
editMode={editMode}
value={phone}
inputType={InputType.Tel}
onInput={handleInput}
data-phone
/>
</FormItem>
</FormGroup>
</Form>
</>
);
};
```

</details>

<Markdown>{SubcomponentsSection}</Markdown>

## Form Group
Expand Down
108 changes: 108 additions & 0 deletions packages/main/src/components/Form/Form.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Meta, StoryObj } from '@storybook/react';
import { useReducer } from 'react';
import {
Button,
CheckBox,
FormBackgroundDesign,
FormGroup,
Expand Down Expand Up @@ -222,3 +224,109 @@ export const FormWithCustomComponents: Story = {
);
}
};

const StandardField = ({ editMode, value, inputType = InputType.None, onInput, ...rest }) => {
if (editMode) {
return <Input value={value} style={{ width: '100%' }} type={inputType} onInput={onInput} {...rest} />;
}
if (inputType === InputType.URL || inputType === InputType.Email) {
return (
<Link href={inputType === InputType.Email ? `mailto:${value}` : value} target="_blank" {...rest}>
{value}
</Link>
);
}
return <Text {...rest}>{value}</Text>;
};

const reducer = (state, { field, value }) => {
return { ...state, [field]: value };
};

export const DisplayEditMode: Story = {
name: 'Display & Edit mode',
args: { titleText: 'Supplier' },
render: (args) => {
const [editMode, toggleEditMode] = useReducer((prev) => !prev, false, undefined);
const [formState, dispatch] = useReducer(
reducer,
{
name: 'Red Point Stores',
street: 'Main St 1618',
zip: 31415,
city: 'Maintown',
country: 'Germany',
web: 'https://www.sap.com',
mail: '[email protected]',
twitter: '@sap',
phone: '+49 1234 56789'
},
undefined
);
const { zip, city, name, street, country, web, mail, twitter, phone } = formState;

const handleInput = (e) => {
dispatch({ field: Object.keys(e.target.dataset)[0], value: e.target.value });
};

return (
<>
<Button onClick={toggleEditMode}>Toggle {editMode ? 'Display-Only Mode' : 'Edit Mode'}</Button>
<Form
{...args}
onSubmit={(e) => {
e.preventDefault();
}}
>
<FormGroup titleText="Address">
<FormItem label="Name">
<StandardField editMode={editMode} value={name} onInput={handleInput} data-name />
</FormItem>
<FormItem label="Street">
<StandardField editMode={editMode} value={street} onInput={handleInput} data-street />
</FormItem>
<FormItem label="ZIP Code / City">
{editMode ? (
<>
<Input value={zip} type={InputType.Number} style={{ width: '30%' }} onInput={handleInput} data-zip />
<Input value={city} style={{ width: '70%' }} onInput={handleInput} data-city />
</>
) : (
<Text>{`${zip} ${city}`}</Text>
)}
</FormItem>
<FormItem label="Country">
<StandardField editMode={editMode} value={country} onInput={handleInput} data-country />
</FormItem>
<FormItem label="Web">
<StandardField editMode={editMode} value={web} inputType={InputType.URL} onInput={handleInput} data-web />
</FormItem>
</FormGroup>
<FormGroup titleText="Contact">
<FormItem label="Email">
<StandardField
editMode={editMode}
value={mail}
inputType={InputType.Email}
onInput={handleInput}
data-email
/>
</FormItem>
<FormItem label="Twitter">
<StandardField editMode={editMode} value={twitter} onInput={handleInput} data-twitter />
</FormItem>
<FormItem label="Phone">
<StandardField
editMode={editMode}
value={phone}
inputType={InputType.Tel}
onInput={handleInput}
data-phone
/>
</FormItem>
</FormGroup>
</Form>
</>
);
}
};