Skip to content

Commit a04374a

Browse files
authored
feat: add view for text-content (#235)
1 parent 16174a1 commit a04374a

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

src/lib/kit/components/Inputs/TextContent/TextContent.tsx

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
import {Label, Text} from '@gravity-ui/uikit';
44
import cloneDeep from 'lodash/cloneDeep';
55

6-
import {StringIndependentInputProps} from '../../../../core';
6+
import {StringIndependentInput, StringSpec} from '../../../../core';
77
import {block} from '../../../utils';
88
import {LazyLoader} from '../../LazyLoader';
99

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

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

16-
export const TextContent: React.FC<StringIndependentInputProps> = ({
16+
export interface TextContentComponentProps {
17+
spec: StringSpec;
18+
value?: string;
19+
Layout?: React.FC<{spec: StringSpec; children: React.ReactElement}>;
20+
}
21+
22+
export const TextContentComponent: React.FC<TextContentComponentProps> = ({
1723
spec,
24+
value,
1825
Layout,
19-
input,
20-
...restProps
2126
}) => {
2227
const {textContentParams, layoutDescription} = spec.viewSpec;
2328

@@ -42,7 +47,7 @@ export const TextContent: React.FC<StringIndependentInputProps> = ({
4247
size="m"
4348
theme={textContentParams.themeLabel}
4449
className={b()}
45-
value={input.value}
50+
value={value}
4651
icon={iconLib}
4752
>
4853
{content}
@@ -57,10 +62,10 @@ export const TextContent: React.FC<StringIndependentInputProps> = ({
5762
</Text>
5863
) : null}
5964
{content}
60-
{input.value ? (
65+
{value ? (
6166
<React.Fragment>
6267
<Text className={b('separator')}>:</Text>
63-
<Text color="secondary">{input.value}</Text>
68+
<Text color="secondary">{value}</Text>
6469
</React.Fragment>
6570
) : null}
6671
</div>
@@ -74,12 +79,41 @@ export const TextContent: React.FC<StringIndependentInputProps> = ({
7479
_spec.viewSpec.layoutDescription = undefined;
7580
}
7681

77-
return (
78-
<Layout spec={_spec} input={input} {...restProps}>
79-
{content}
80-
</Layout>
81-
);
82+
return <Layout spec={_spec}>{content}</Layout>;
8283
}
8384

8485
return content;
8586
};
87+
88+
export const TextContent: StringIndependentInput = ({
89+
name,
90+
spec,
91+
Layout,
92+
input,
93+
arrayInput,
94+
meta,
95+
layoutProps,
96+
}) => {
97+
const WrappedLayout = React.useMemo(() => {
98+
if (Layout) {
99+
const Component: TextContentComponentProps['Layout'] = (props) => {
100+
return (
101+
<Layout
102+
name={name}
103+
input={input}
104+
layoutProps={layoutProps}
105+
arrayInput={arrayInput}
106+
meta={meta}
107+
{...props}
108+
/>
109+
);
110+
};
111+
112+
return Component;
113+
}
114+
115+
return undefined;
116+
}, [Layout, layoutProps, input, arrayInput, meta, name]);
117+
118+
return <TextContentComponent spec={spec} value={input.value} Layout={WrappedLayout} />;
119+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react';
2+
3+
import {StringIndependentView} from '../../../core';
4+
import {TextContentComponent, TextContentComponentProps} from '../Inputs';
5+
6+
export const TextContentView: StringIndependentView = ({name, spec, Layout, value}) => {
7+
const WrappedLayout = React.useMemo(() => {
8+
if (Layout) {
9+
const Component: TextContentComponentProps['Layout'] = (props) => {
10+
const VALUE_STUB =
11+
'if u see this, please create issue about it here: https://github.com/gravity-ui/dynamic-forms/issues/new';
12+
13+
return <Layout name={name} value={VALUE_STUB} {...props} />;
14+
};
15+
16+
return Component;
17+
}
18+
19+
return undefined;
20+
}, [Layout, name]);
21+
22+
return <TextContentComponent spec={spec} value={value} Layout={WrappedLayout} />;
23+
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './ObjectValueInputView';
1212
export * from './OneOfView';
1313
export * from './TableArrayView';
1414
export * from './TextAreaView';
15+
export * from './TextContentView';
1516
export * from './TextLinkView';
1617
export * from './TimeRangeSelectorView';
1718
export * from './DateView';

src/lib/kit/constants/config.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
TextArea,
5353
TextAreaView,
5454
TextContent,
55+
TextContentView,
5556
TextLink,
5657
TextLinkView,
5758
TimeRangeSelector,
@@ -277,7 +278,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
277278
file_input: {Component: FileInputView},
278279
number_with_scale: {Component: NumberWithScaleView},
279280
monaco_input: {Component: MonacoView},
280-
text_content: undefined,
281+
text_content: {Component: TextContentView, independent: true},
281282
},
282283
layouts: {
283284
row: ViewRow,

0 commit comments

Comments
 (0)