Skip to content

Commit

Permalink
feat: add view for text-content (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
bocembocem authored Sep 16, 2024
1 parent 16174a1 commit a04374a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
58 changes: 46 additions & 12 deletions src/lib/kit/components/Inputs/TextContent/TextContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import {Label, Text} from '@gravity-ui/uikit';
import cloneDeep from 'lodash/cloneDeep';

import {StringIndependentInputProps} from '../../../../core';
import {StringIndependentInput, StringSpec} from '../../../../core';
import {block} from '../../../utils';
import {LazyLoader} from '../../LazyLoader';

Expand All @@ -13,11 +13,16 @@ import './TextContent.scss';

const b = block('text-content');

export const TextContent: React.FC<StringIndependentInputProps> = ({
export interface TextContentComponentProps {
spec: StringSpec;
value?: string;
Layout?: React.FC<{spec: StringSpec; children: React.ReactElement}>;
}

export const TextContentComponent: React.FC<TextContentComponentProps> = ({
spec,
value,
Layout,
input,
...restProps
}) => {
const {textContentParams, layoutDescription} = spec.viewSpec;

Expand All @@ -42,7 +47,7 @@ export const TextContent: React.FC<StringIndependentInputProps> = ({
size="m"
theme={textContentParams.themeLabel}
className={b()}
value={input.value}
value={value}
icon={iconLib}
>
{content}
Expand All @@ -57,10 +62,10 @@ export const TextContent: React.FC<StringIndependentInputProps> = ({
</Text>
) : null}
{content}
{input.value ? (
{value ? (
<React.Fragment>
<Text className={b('separator')}>:</Text>
<Text color="secondary">{input.value}</Text>
<Text color="secondary">{value}</Text>
</React.Fragment>
) : null}
</div>
Expand All @@ -74,12 +79,41 @@ export const TextContent: React.FC<StringIndependentInputProps> = ({
_spec.viewSpec.layoutDescription = undefined;
}

return (
<Layout spec={_spec} input={input} {...restProps}>
{content}
</Layout>
);
return <Layout spec={_spec}>{content}</Layout>;
}

return content;
};

export const TextContent: StringIndependentInput = ({
name,
spec,
Layout,
input,
arrayInput,
meta,
layoutProps,
}) => {
const WrappedLayout = React.useMemo(() => {
if (Layout) {
const Component: TextContentComponentProps['Layout'] = (props) => {
return (
<Layout
name={name}
input={input}
layoutProps={layoutProps}
arrayInput={arrayInput}
meta={meta}
{...props}
/>
);
};

return Component;
}

return undefined;
}, [Layout, layoutProps, input, arrayInput, meta, name]);

return <TextContentComponent spec={spec} value={input.value} Layout={WrappedLayout} />;
};
23 changes: 23 additions & 0 deletions src/lib/kit/components/Views/TextContentView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';

import {StringIndependentView} from '../../../core';
import {TextContentComponent, TextContentComponentProps} from '../Inputs';

export const TextContentView: StringIndependentView = ({name, spec, Layout, value}) => {
const WrappedLayout = React.useMemo(() => {
if (Layout) {
const Component: TextContentComponentProps['Layout'] = (props) => {
const VALUE_STUB =
'if u see this, please create issue about it here: https://github.com/gravity-ui/dynamic-forms/issues/new';

return <Layout name={name} value={VALUE_STUB} {...props} />;
};

return Component;
}

return undefined;
}, [Layout, name]);

return <TextContentComponent spec={spec} value={value} Layout={WrappedLayout} />;
};
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 @@ -12,6 +12,7 @@ export * from './ObjectValueInputView';
export * from './OneOfView';
export * from './TableArrayView';
export * from './TextAreaView';
export * from './TextContentView';
export * from './TextLinkView';
export * from './TimeRangeSelectorView';
export * from './DateView';
3 changes: 2 additions & 1 deletion src/lib/kit/constants/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
TextArea,
TextAreaView,
TextContent,
TextContentView,
TextLink,
TextLinkView,
TimeRangeSelector,
Expand Down Expand Up @@ -277,7 +278,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
file_input: {Component: FileInputView},
number_with_scale: {Component: NumberWithScaleView},
monaco_input: {Component: MonacoView},
text_content: undefined,
text_content: {Component: TextContentView, independent: true},
},
layouts: {
row: ViewRow,
Expand Down

0 comments on commit a04374a

Please sign in to comment.