|
| 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