Skip to content

Commit 67214a1

Browse files
authored
feat(ObjectValue): added new input object value (#28)
1 parent 59afaae commit 67214a1

File tree

9 files changed

+117
-1
lines changed

9 files changed

+117
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react';
2+
3+
import _ from 'lodash';
4+
5+
import {Controller, FieldValue, ObjectIndependentInput, ValidateError} from '../../../../core';
6+
7+
const OBJECT_VALUE_PROPERTY_NAME = 'value';
8+
9+
export const ObjectValueInput: ObjectIndependentInput = ({spec, input, name}) => {
10+
const parentOnChange = React.useCallback(
11+
(childName: string, childValue: FieldValue, childErrors?: Record<string, ValidateError>) =>
12+
input.onChange(
13+
(currentValue) =>
14+
_.set(
15+
{...(currentValue || {})},
16+
childName.split(`${name}.`).join(''),
17+
childValue,
18+
),
19+
childErrors,
20+
),
21+
[input.onChange, input.name],
22+
);
23+
24+
const parentOnUnmount = React.useCallback(
25+
(childName: string) => input.onChange((currentValue) => currentValue, {[childName]: false}),
26+
[input.onChange],
27+
);
28+
29+
const specProperties = {...spec.properties};
30+
31+
if (!specProperties[OBJECT_VALUE_PROPERTY_NAME]) {
32+
return null;
33+
}
34+
35+
return (
36+
<Controller
37+
initialValue={input.value?.[OBJECT_VALUE_PROPERTY_NAME]}
38+
spec={specProperties[OBJECT_VALUE_PROPERTY_NAME]}
39+
name={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`}
40+
key={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`}
41+
parentOnChange={parentOnChange}
42+
parentOnUnmount={parentOnUnmount}
43+
/>
44+
);
45+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ObjectValueInput';

src/lib/kit/components/Inputs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './Checkbox';
44
export * from './FileInput';
55
export * from './MultiSelect';
66
export * from './ObjectBase';
7+
export * from './ObjectValueInput';
78
export * from './OneOf';
89
export * from './OneOfCard';
910
export * from './Secret';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
3+
import {ObjectIndependentView, ViewController} from '../../../../core';
4+
5+
const OBJECT_VALUE_PROPERTY_NAME = 'value';
6+
7+
export const ObjectValueInputView: ObjectIndependentView = ({spec, name}) => {
8+
const specProperties = {...spec.properties};
9+
10+
if (!specProperties[OBJECT_VALUE_PROPERTY_NAME]) {
11+
return null;
12+
}
13+
14+
return (
15+
<ViewController
16+
spec={specProperties[OBJECT_VALUE_PROPERTY_NAME]}
17+
name={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`}
18+
/>
19+
);
20+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ObjectValueInputView';

src/lib/kit/components/Views/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './MonacoInputView';
66
export * from './MultiSelectView';
77
export * from './NumberWithScaleView';
88
export * from './ObjectBaseView';
9+
export * from './ObjectValueInputView';
910
export * from './OneOfCardView';
1011
export * from './OneOfView';
1112
export * from './TableArrayView';

src/lib/kit/constants/config.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
NumberWithScaleView,
2525
ObjectBase,
2626
ObjectBaseView,
27+
ObjectValueInput,
28+
ObjectValueInputView,
2729
OneOf,
2830
OneOfCard,
2931
OneOfCardView,
@@ -130,6 +132,7 @@ export const dynamicConfig: DynamicFormConfig = {
130132
secret: {Component: Secret, independent: true},
131133
base: {Component: ObjectBase, independent: true},
132134
text_link: {Component: TextLink, independent: true},
135+
object_value: {Component: ObjectValueInput, independent: true},
133136
},
134137
layouts: {
135138
row: Row,
@@ -228,6 +231,7 @@ export const dynamicCardConfig: DynamicFormConfig = {
228231
secret: {Component: Secret, independent: true},
229232
base: {Component: ObjectBase, independent: true},
230233
text_link: {Component: TextLink, independent: true},
234+
object_value: {Component: ObjectValueInput, independent: true},
231235
},
232236
layouts: {
233237
row: Row2,
@@ -315,6 +319,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
315319
secret: undefined,
316320
base: {Component: ObjectBaseView, independent: true},
317321
text_link: {Component: TextLinkView, independent: true},
322+
object_value: {Component: ObjectValueInputView, independent: true},
318323
},
319324
layouts: {
320325
row: ViewRow,
@@ -395,6 +400,7 @@ export const dynamicViewCardConfig: DynamicViewConfig = {
395400
secret: undefined,
396401
base: {Component: ObjectBaseView, independent: true},
397402
text_link: {Component: TextLinkView, independent: true},
403+
object_value: {Component: ObjectValueInputView, independent: true},
398404
},
399405
layouts: {
400406
row: ViewRow2,

src/stories/ObjectTextLink.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const baseSpec: ObjectSpec = {
3131

3232
const value = {link: 'https://gravity-ui.com'};
3333

34-
const excludeOptions = ['description', 'validator', 'viewSpec'];
34+
const excludeOptions = ['description', 'validator', 'viewSpec', 'required'];
3535

3636
const template = (spec: ObjectSpec = baseSpec) => {
3737
const Template: ComponentStory<typeof TextLinkBase> = (__, {viewMode}) => (
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
3+
import {ComponentStory} from '@storybook/react';
4+
5+
import {ObjectSpec, ObjectValueInput as ObjectValueInputBase, SpecTypes} from '../lib';
6+
7+
import {InputPreview} from './components';
8+
9+
export default {
10+
title: 'Object/ObjectValue',
11+
component: ObjectValueInputBase,
12+
};
13+
14+
const baseSpec: ObjectSpec = {
15+
type: SpecTypes.Object,
16+
properties: {
17+
value: {
18+
type: SpecTypes.String,
19+
viewSpec: {
20+
type: 'base',
21+
layout: 'row',
22+
layoutTitle: 'Value',
23+
},
24+
},
25+
},
26+
viewSpec: {
27+
type: 'object_value',
28+
},
29+
};
30+
31+
const excludeOptions = ['description', 'validator', 'viewSpec', 'required'];
32+
33+
const template = (spec: ObjectSpec = baseSpec) => {
34+
const Template: ComponentStory<typeof ObjectValueInputBase> = (__, {viewMode}) => (
35+
<InputPreview spec={spec} excludeOptions={excludeOptions} viewMode={viewMode} />
36+
);
37+
38+
return Template;
39+
};
40+
41+
export const ObjectValue = template();

0 commit comments

Comments
 (0)