Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add layout props variant header for section and accordeon and a… #271

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ You can provide all props of [original component](https://preview.gravity-ui.com
| iconColor | `'primary'` `'complementary'` `'secondary'` `'hint'` `'info'` `'info-heavy'` `'positive'` `'positive-heavy'` `'warning'` `'warning-heavy'` `'danger'` `'danger-heavy'` `'utility'` `'utility-heavy'` `'misc'` `'misc-heavy'` `'brand'` `'dark-primary'` `'dark-complementary'` `'dark-secondary'` | | The color of the icon, if it does not have the themeLabel parameter |
| themeIcon | `'normal'` `'info'` `'success'` `'warning'` `'danger'` `'utility'` | | Alert color |
| titleAlert | `string` | | Alert title |
| viewAlert | `'filled'` `'outlined'` | | Alert view |

#### SelectParams

Expand Down
5 changes: 4 additions & 1 deletion src/lib/core/components/View/types/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {FormValue, Spec} from '../../../';

import {ViewProps} from './';

export type ViewLayoutProps<Value extends FormValue, SpecType extends Spec> = {
export type ViewLayoutProps<
Value extends FormValue,
SpecType extends Spec<undefined, undefined, Record<string, any> | undefined> = Spec,
> = {
children: React.ReactElement;
} & ViewProps<Value, SpecType>;

Expand Down
5 changes: 4 additions & 1 deletion src/lib/core/components/View/types/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {FormValue, Spec} from '../../../';

import {ViewLayoutType} from './';

export type ViewProps<Value extends FormValue, SpecType extends Spec> = {
export type ViewProps<
Value extends FormValue,
SpecType extends Spec<undefined, undefined, Record<string, any> | undefined> = Spec,
> = {
spec: SpecType;
name: string;
value?: Value;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/core/types/specs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AlertProps, LabelProps} from '@gravity-ui/uikit';
import type {AlertProps, LabelProps} from '@gravity-ui/uikit';
import {ColorTextBaseProps} from '@gravity-ui/uikit/build/esm/components/Text/colorText/colorText';

import {ReadAsMethod, SpecTypes} from '../constants';
Expand Down Expand Up @@ -175,6 +175,7 @@ export interface StringSpec<
iconColor?: ColorTextBaseProps['color'];
titleAlert?: string;
themeAlert?: AlertProps['theme'];
viewAlert?: AlertProps['view'];
};
fileInput?: {
accept?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const TextContentComponent: React.FC<TextContentComponentProps> = ({
// If the title is an empty line, then you need to explicitly write undefined, otherwise there will be an additional indent
title={titleAlert}
theme={textContentParams?.themeAlert}
view={textContentParams?.viewAlert}
/>
);
} else if (textContentParams?.themeLabel) {
Expand Down
15 changes: 14 additions & 1 deletion src/lib/kit/components/Layouts/Accordeon/Accordeon.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import React from 'react';

import {TextProps} from '@gravity-ui/uikit';

import {ArrayLayoutProps, ObjectLayoutProps, isArrayItem} from '../../../../core';
import {ErrorWrapper} from '../../../components';
import {useErrorChecker} from '../../../hooks';
import {RemoveButton} from '../../RemoveButton';
import {SimpleVerticalAccordeon} from '../../SimpleVerticalAccordeon';

export const Accordeon = <T extends ArrayLayoutProps | ObjectLayoutProps>({
interface AccordeonLayoutProps {
variantTitle?: TextProps['variant'];
}

export const Accordeon = <
T extends
| ArrayLayoutProps<Record<string, any> | undefined, AccordeonLayoutProps | undefined>
| ObjectLayoutProps<Record<string, any> | undefined, AccordeonLayoutProps | undefined>,
>({
name,
spec,
input,
meta,
children,
}: T): JSX.Element => {
const {variantTitle} = spec.viewSpec.layoutProps || {};

const [open, setOpen] = React.useState(Boolean(spec.viewSpec?.layoutOpen));

const onDrop = React.useCallback(() => {
Expand Down Expand Up @@ -40,6 +52,7 @@ export const Accordeon = <T extends ArrayLayoutProps | ObjectLayoutProps>({
headerActionsTemplate={removeButton}
hideInsteadOfDestroy
withBranchView
variantTitle={variantTitle}
>
<ErrorWrapper name={name} meta={meta} withoutChildErrorStyles>
{children}
Expand Down
41 changes: 36 additions & 5 deletions src/lib/kit/components/Layouts/Section/Section.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

import {HelpPopover} from '@gravity-ui/components';
import {Popover, Text} from '@gravity-ui/uikit';
import {Popover, Text, TextProps} from '@gravity-ui/uikit';

import {GroupIndent} from '../../';
import {RemoveButton} from '../../RemoveButton';
Expand All @@ -23,14 +23,22 @@ import './Section.scss';

const b = block('section');

interface SectionLayoutProps {
variantTitle?: TextProps['variant'];
}

interface SectionProps {
titleSize: 's' | 'm';
withIndent?: boolean;
ignoreDescription?: boolean;
descriptionAsSubtitle?: boolean;
}

const SectionBase = <D extends FieldValue, T extends FormValue, S extends Spec>({
const SectionBase = <
D extends FieldValue,
T extends FormValue,
S extends Spec<any, any, SectionLayoutProps>,
>({
name,
spec,
titleSize,
Expand All @@ -39,7 +47,7 @@ const SectionBase = <D extends FieldValue, T extends FormValue, S extends Spec>(
descriptionAsSubtitle,
children,
...restProps
}: (LayoutProps<D, undefined, undefined, S> | ViewLayoutProps<T, S>) & SectionProps) => {
}: (LayoutProps<D, undefined, SectionLayoutProps, S> | ViewLayoutProps<T, S>) & SectionProps) => {
const input = (restProps as FieldRenderProps<D>).input as
| FieldRenderProps<D>['input']
| undefined;
Expand All @@ -48,6 +56,29 @@ const SectionBase = <D extends FieldValue, T extends FormValue, S extends Spec>(
const titleRef = React.useRef<HTMLHeadingElement>(null);
let content = children;

const {variantTitle: variantTitleProp} = spec.viewSpec.layoutProps || {};

const {sizeTitle, variantTitle} = React.useMemo(() => {
if (variantTitleProp) {
return {
sizeTitle: undefined,
variantTitle: variantTitleProp,
};
}

if (titleSize === 'm') {
return {
sizeTitle: titleSize,
variantTitle: 'body-2',
};
}

return {
sizeTitle: titleSize,
variantTitle: 'body-1',
};
}, [variantTitleProp, titleSize]);

const removeButton = React.useMemo(() => {
if (input?.value && input?.onDrop && isArrayItem(name)) {
return (
Expand Down Expand Up @@ -107,7 +138,7 @@ const SectionBase = <D extends FieldValue, T extends FormValue, S extends Spec>(
<div
className={b('header', {
'with-popover': !descriptionAsSubtitle,
size: titleSize,
size: sizeTitle,
})}
>
<Popover
Expand All @@ -118,7 +149,7 @@ const SectionBase = <D extends FieldValue, T extends FormValue, S extends Spec>(
>
<Text
className={b('title')}
variant={titleSize === 'm' ? 'body-2' : 'body-1'}
variant={variantTitle as TextProps['variant']}
ref={titleRef}
ellipsis
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import {HelpPopover} from '@gravity-ui/components';
import {ChevronDown} from '@gravity-ui/icons';
import {Button, Icon, Popover, Text} from '@gravity-ui/uikit';
import {Button, Icon, Popover, Text, TextProps} from '@gravity-ui/uikit';

import {COMMON_POPOVER_PLACEMENT} from '../../constants/common';
import {block} from '../../utils';
Expand All @@ -27,6 +27,7 @@ interface SimpleVerticalAccordeonProps {
hideInsteadOfDestroy?: boolean;
withBranchView?: boolean;
viewLayout?: boolean;
variantTitle?: TextProps['variant'];
}

interface SimpleVerticalAccordeonState {
Expand Down Expand Up @@ -81,6 +82,7 @@ export class SimpleVerticalAccordeon extends React.Component<
withBranchView,
viewLayout,
name,
variantTitle,
} = this.props;
const {open, hidden, isFirstRender} = this.state;

Expand All @@ -104,7 +106,7 @@ export class SimpleVerticalAccordeon extends React.Component<
const titlePopoverDisabled =
(this.titleRef.current?.offsetWidth || 0) <= TITLE_TEXT_MAX_WIDTH;

const currentTitleVariant = this.getCurrentTitleVariant();
const currentTitleVariant = variantTitle || this.getCurrentTitleVariant();

return (
Boolean(React.Children.count(children)) && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import React from 'react';

import isBoolean from 'lodash/isBoolean';
import {TextProps} from '@gravity-ui/uikit';

import {ArrayViewLayoutProps, ObjectViewLayoutProps, useDynamicFormsCtx} from '../../../../core';
import {ArrayValue, ObjectValue, Spec, ViewLayoutProps, useDynamicFormsCtx} from '../../../../core';
import {isNotEmptyValue} from '../../../utils';
import {SimpleVerticalAccordeon} from '../../SimpleVerticalAccordeon';

export const ViewAccordeon = <T extends ArrayViewLayoutProps | ObjectViewLayoutProps>({
interface ViewAccordeonLayoutProps {
variantTitle?: TextProps['variant'];
}

export const ViewAccordeon = <
T extends ViewLayoutProps<ArrayValue | ObjectValue, Spec<any, any, ViewAccordeonLayoutProps>>,
>({
name,
value,
spec,
Expand All @@ -17,7 +24,9 @@ export const ViewAccordeon = <T extends ArrayViewLayoutProps | ObjectViewLayoutP
isBoolean(spec.viewSpec.layoutOpen) ? spec.viewSpec.layoutOpen : true,
);

if (!isNotEmptyValue(value, spec)) {
const {variantTitle} = spec.viewSpec.layoutProps || {};

if (!isNotEmptyValue(value, spec as Spec)) {
return null;
}

Expand All @@ -35,6 +44,7 @@ export const ViewAccordeon = <T extends ArrayViewLayoutProps | ObjectViewLayoutP
hideInsteadOfDestroy
withBranchView
viewLayout
variantTitle={variantTitle}
>
{children}
</SimpleVerticalAccordeon>
Expand Down
5 changes: 5 additions & 0 deletions src/lib/kit/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export const prepareSpec = <Type extends Spec>(
result.viewSpec.textContentParams.themeAlert.toLowerCase();
}

if (isString(result.viewSpec?.textContentParams?.viewAlert)) {
result.viewSpec.textContentParams.viewAlert =
result.viewSpec.textContentParams.viewAlert.toLowerCase();
}

if (isString(result.validator)) {
result.validator = result.validator.toLowerCase();
}
Expand Down
1 change: 1 addition & 0 deletions src/stories/ArrayCheckboxGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const excludeOptions = [
'viewSpec.inputProps',
'viewSpec.placeholder',
'viewSpec.layoutOpen',
'viewSpec.layoutProps',
];

const template = () => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ArraySelect.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const excludeOptions = [
'viewSpec.itemPrefix',
'viewSpec.addButtonPosition',
'viewSpec.checkboxGroupParams',
'viewSpec.layoutProps',
];

const template = () => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ArrayTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const excludeOptions = [
'viewSpec.selectParams',
'viewSpec.checkboxGroupParams',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const value = [
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectCardOneOf.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectInline.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const excludeOptions = [
'viewSpec.oneOfParams',
'viewSpec.placeholder',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectMultiOneOf.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const excludeOptions = [
'viewSpec.oneOfParams',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const value = {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectMultiOneOfFlat.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const excludeOptions = [
'viewSpec.oneOfParams',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const value = {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectOneOf.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectOneOfFlat.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectSecret.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectTextLink.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectTimeRangeSelector.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/ObjectValue.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.delimiter',
'viewSpec.inputProps',
'viewSpec.layoutProps',
];

const template = (spec: ObjectSpec = baseSpec) => {
Expand Down
Loading