Skip to content

Commit

Permalink
feat(ObjectValue): added new input object value (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
NasgulNexus authored Apr 18, 2023
1 parent 59afaae commit 67214a1
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react';

import _ from 'lodash';

import {Controller, FieldValue, ObjectIndependentInput, ValidateError} from '../../../../core';

const OBJECT_VALUE_PROPERTY_NAME = 'value';

export const ObjectValueInput: ObjectIndependentInput = ({spec, input, name}) => {
const parentOnChange = React.useCallback(
(childName: string, childValue: FieldValue, childErrors?: Record<string, ValidateError>) =>
input.onChange(
(currentValue) =>
_.set(
{...(currentValue || {})},
childName.split(`${name}.`).join(''),
childValue,
),
childErrors,
),
[input.onChange, input.name],
);

const parentOnUnmount = React.useCallback(
(childName: string) => input.onChange((currentValue) => currentValue, {[childName]: false}),
[input.onChange],
);

const specProperties = {...spec.properties};

if (!specProperties[OBJECT_VALUE_PROPERTY_NAME]) {
return null;
}

return (
<Controller
initialValue={input.value?.[OBJECT_VALUE_PROPERTY_NAME]}
spec={specProperties[OBJECT_VALUE_PROPERTY_NAME]}
name={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`}
key={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`}
parentOnChange={parentOnChange}
parentOnUnmount={parentOnUnmount}
/>
);
};
1 change: 1 addition & 0 deletions src/lib/kit/components/Inputs/ObjectValueInput/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ObjectValueInput';
1 change: 1 addition & 0 deletions src/lib/kit/components/Inputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './Checkbox';
export * from './FileInput';
export * from './MultiSelect';
export * from './ObjectBase';
export * from './ObjectValueInput';
export * from './OneOf';
export * from './OneOfCard';
export * from './Secret';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

import {ObjectIndependentView, ViewController} from '../../../../core';

const OBJECT_VALUE_PROPERTY_NAME = 'value';

export const ObjectValueInputView: ObjectIndependentView = ({spec, name}) => {
const specProperties = {...spec.properties};

if (!specProperties[OBJECT_VALUE_PROPERTY_NAME]) {
return null;
}

return (
<ViewController
spec={specProperties[OBJECT_VALUE_PROPERTY_NAME]}
name={`${name}.${OBJECT_VALUE_PROPERTY_NAME}`}
/>
);
};
1 change: 1 addition & 0 deletions src/lib/kit/components/Views/ObjectValueInputView/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ObjectValueInputView';
1 change: 1 addition & 0 deletions src/lib/kit/components/Views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './MonacoInputView';
export * from './MultiSelectView';
export * from './NumberWithScaleView';
export * from './ObjectBaseView';
export * from './ObjectValueInputView';
export * from './OneOfCardView';
export * from './OneOfView';
export * from './TableArrayView';
Expand Down
6 changes: 6 additions & 0 deletions src/lib/kit/constants/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
NumberWithScaleView,
ObjectBase,
ObjectBaseView,
ObjectValueInput,
ObjectValueInputView,
OneOf,
OneOfCard,
OneOfCardView,
Expand Down Expand Up @@ -130,6 +132,7 @@ export const dynamicConfig: DynamicFormConfig = {
secret: {Component: Secret, independent: true},
base: {Component: ObjectBase, independent: true},
text_link: {Component: TextLink, independent: true},
object_value: {Component: ObjectValueInput, independent: true},
},
layouts: {
row: Row,
Expand Down Expand Up @@ -228,6 +231,7 @@ export const dynamicCardConfig: DynamicFormConfig = {
secret: {Component: Secret, independent: true},
base: {Component: ObjectBase, independent: true},
text_link: {Component: TextLink, independent: true},
object_value: {Component: ObjectValueInput, independent: true},
},
layouts: {
row: Row2,
Expand Down Expand Up @@ -315,6 +319,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
secret: undefined,
base: {Component: ObjectBaseView, independent: true},
text_link: {Component: TextLinkView, independent: true},
object_value: {Component: ObjectValueInputView, independent: true},
},
layouts: {
row: ViewRow,
Expand Down Expand Up @@ -395,6 +400,7 @@ export const dynamicViewCardConfig: DynamicViewConfig = {
secret: undefined,
base: {Component: ObjectBaseView, independent: true},
text_link: {Component: TextLinkView, independent: true},
object_value: {Component: ObjectValueInputView, independent: true},
},
layouts: {
row: ViewRow2,
Expand Down
2 changes: 1 addition & 1 deletion src/stories/ObjectTextLink.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const baseSpec: ObjectSpec = {

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

const excludeOptions = ['description', 'validator', 'viewSpec'];
const excludeOptions = ['description', 'validator', 'viewSpec', 'required'];

const template = (spec: ObjectSpec = baseSpec) => {
const Template: ComponentStory<typeof TextLinkBase> = (__, {viewMode}) => (
Expand Down
41 changes: 41 additions & 0 deletions src/stories/ObjectValue.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';

import {ComponentStory} from '@storybook/react';

import {ObjectSpec, ObjectValueInput as ObjectValueInputBase, SpecTypes} from '../lib';

import {InputPreview} from './components';

export default {
title: 'Object/ObjectValue',
component: ObjectValueInputBase,
};

const baseSpec: ObjectSpec = {
type: SpecTypes.Object,
properties: {
value: {
type: SpecTypes.String,
viewSpec: {
type: 'base',
layout: 'row',
layoutTitle: 'Value',
},
},
},
viewSpec: {
type: 'object_value',
},
};

const excludeOptions = ['description', 'validator', 'viewSpec', 'required'];

const template = (spec: ObjectSpec = baseSpec) => {
const Template: ComponentStory<typeof ObjectValueInputBase> = (__, {viewMode}) => (
<InputPreview spec={spec} excludeOptions={excludeOptions} viewMode={viewMode} />
);

return Template;
};

export const ObjectValue = template();

0 comments on commit 67214a1

Please sign in to comment.