Skip to content

Commit 93876ea

Browse files
committed
docs: added documentation to storybook
1 parent 1a43eec commit 93876ea

File tree

11 files changed

+1345
-6
lines changed

11 files changed

+1345
-6
lines changed

.github/workflows/playwright.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ jobs:
1010
container:
1111
image: mcr.microsoft.com/playwright:v1.40.0-jammy
1212
steps:
13-
- uses: actions/checkout@v3
14-
- uses: actions/setup-node@v3
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-node@v4
1515
with:
1616
node-version: 18
1717
- name: Install dependencies
@@ -22,7 +22,7 @@ jobs:
2222
CI: 'true'
2323
- name: Upload Playwright playwright report to GitHub Actions Artifacts
2424
if: always()
25-
uses: actions/upload-artifact@v3
25+
uses: actions/upload-artifact@v4
2626
with:
2727
name: playwright-report
2828
path: ./playwright-report
@@ -35,7 +35,7 @@ jobs:
3535
shell: bash
3636
- name: Create PR Artifact
3737
if: always()
38-
uses: actions/upload-artifact@v3
38+
uses: actions/upload-artifact@v4
3939
with:
4040
name: pr
4141
path: ./pr-id.txt

.storybook/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type {StorybookConfig} from '@storybook/react-webpack5';
33
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
44

55
const config: StorybookConfig = {
6-
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
7-
addons: ['@storybook/addon-essentials', '@storybook/preset-scss'],
6+
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
7+
addons: ['@storybook/addon-essentials', '@storybook/preset-scss', '@storybook/addon-docs'],
88
framework: {
99
name: '@storybook/react-webpack5',
1010
options: {fastRefresh: true},

src/stories/Config.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {Meta, Markdown} from '@storybook/addon-docs';
2+
3+
import Config from '../../docs/config.md?raw';
4+
5+
<Meta title="Docs/Config" />
6+
7+
<Markdown>{Config}</Markdown>

src/stories/CustomInput.mdx

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import {Meta} from '@storybook/addon-docs';
2+
3+
<Meta title="Docs/Custom input" />
4+
5+
# Custom Input in Dynamic Forms
6+
7+
The `@gravity-ui/dynamic-forms` library provides a standard set of inputs, but sometimes you may need to add your own custom input. In this guide, we'll walk through how to create and integrate a custom text input into a dynamic forms.
8+
9+
## Steps to Add a Custom Input
10+
11+
1. [Create Your Own Input](#1-create-your-own-input)
12+
2. [Create a View for the Input](#2-create-a-view-for-the-input)
13+
3. [Integrate the Input into the Config](#3-integrate-the-input-into-the-config)
14+
4. [Use the Custom Input](#4-use-the-custom-input)
15+
16+
---
17+
18+
### 1. Create Your Own Input
19+
20+
First, we need to create our own input component. Inputs in `@gravity-ui/dynamic-forms` come in the following types:
21+
22+
- `array`
23+
- `string`
24+
- `object`
25+
- `number`
26+
- `boolean`
27+
28+
We'll be creating a string type input, intended for text data entry.
29+
30+
```tsx
31+
import React from 'react';
32+
import { isNil } from 'lodash';
33+
34+
import type { FieldRenderProps, StringInput } from '@gravity-ui/dynamic-forms';
35+
import type { TextInputProps as TextInputBaseProps } from '@gravity-ui/uikit';
36+
import { TextInput } from '@gravity-ui/uikit';
37+
38+
export interface TextProps
39+
extends Omit<
40+
TextInputBaseProps,
41+
'value' | 'onBlur' | 'onFocus' | 'onUpdate' | 'disabled' | 'placeholder' | 'qa'
42+
> {}
43+
44+
export const CustomTextInput: StringInput<TextProps> = ({
45+
name,
46+
input: { value, onBlur, onChange, onFocus },
47+
spec,
48+
inputProps,
49+
}) => {
50+
const props = {
51+
hasClear: true,
52+
...inputProps,
53+
value: isNil(value) ? '' : ${value}, // Set default value if value is null or undefined
54+
onBlur,
55+
onFocus,
56+
onUpdate: onChange as FieldRenderProps<string>['input']['onChange'],
57+
disabled: spec.viewSpec.disabled,
58+
placeholder: spec.viewSpec.placeholder,
59+
qa: name,
60+
};
61+
62+
return <TextInput {...props} type="text" />;
63+
};
64+
```
65+
66+
### 2. Create a View for the Input
67+
68+
To display the input's value in view mode, we need to create a view component.
69+
70+
```tsx
71+
import React from 'react';
72+
73+
import type {StringView} from '@gravity-ui/dynamic-forms';
74+
import {Text} from '@gravity-ui/uikit';
75+
76+
export const CustomTextInputView: StringView = ({value}) => {
77+
return <Text>{value}</Text>;
78+
};
79+
```
80+
81+
### 3. Integrate the Input into the Config
82+
83+
Next, we need to integrate our custom input and its view into the dynamic form configurations.
84+
85+
```tsx
86+
import _ from 'lodash';
87+
88+
import type { DynamicFormConfig, DynamicViewConfig } from '@gravity-ui/dynamic-forms';
89+
import {
90+
dynamicConfig as libConfig,
91+
dynamicViewConfig as libViewConfig,
92+
} from '@gravity-ui/dynamic-forms';
93+
94+
import { CustomTextInput } from './CustomTextInput';
95+
import { CustomTextInputView } from './CustomTextInputView';
96+
97+
const getDynamicConfig = (): DynamicFormConfig => {
98+
const dynamicConfig = _.cloneDeep(libConfig);
99+
100+
// Register our custom input with a specific name
101+
dynamicConfig.string.inputs['custom_text_input'] = { Component: CustomTextInput };
102+
103+
return dynamicConfig;
104+
};
105+
106+
export const dynamicConfig = getDynamicConfig();
107+
108+
const getDynamicViewConfig = (): DynamicViewConfig => {
109+
const dynamicViewConfig = _.cloneDeep(libViewConfig);
110+
111+
// Register the view for our custom input
112+
dynamicViewConfig.string.views['custom_text_input'] = { Component: CustomTextInputView };
113+
114+
return dynamicViewConfig;
115+
};
116+
117+
export const dynamicViewConfig = getDynamicViewConfig();
118+
```
119+
120+
**Explanations:**
121+
122+
- We clone the base library configuration using `\_.cloneDeep` to avoid modifying the original settings and prevent potential conflicts.
123+
- In the square brackets, we specify the name of our custom input `'custom_text_input'`.
124+
- If you use a name that matches an existing one in the library, your input will override the standard one.
125+
126+
### 4. Use the Custom Input
127+
128+
Now, you can use your custom input in the form specification by setting its type to `'custom_text_input'`.
129+
130+
#### Example Field Spec:
131+
132+
``` json
133+
{
134+
"type": "string",
135+
"viewSpec": {
136+
"type": "custom_text_input", // Specify the input name in 'type'
137+
"layout": "row",
138+
"layoutTitle": "Name",
139+
"placeholder": "Enter your name"
140+
}
141+
}
142+
```
143+
144+
**Explanations:**
145+
146+
- The field will use our custom input `'custom_text_input'`, which we registered in the configuration.
147+
- layout, layoutTitle, and placeholder are used to configure the field's appearance and behavior.
148+
149+
## Conclusion
150+
151+
In this guide, we've explored how to create and integrate a custom text input into a dynamic form using the `@gravity-ui/dynamic-forms` library. Now, you can create custom inputs tailored to your specific requirements.
152+
153+
**Benefits of Creating Custom Inputs:**
154+
155+
- Flexibility in displaying and processing data.
156+
- Ability to implement unique logic and styles.
157+
- Enhancing user experience through customization.

0 commit comments

Comments
 (0)