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(MultiOneOf): added new input multi one of #77

Merged
merged 5 commits into from
Aug 11, 2023
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 @@ -86,6 +86,7 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
| viewSpec.order | `string[]` | | Array of `properties` keys in the right order |
| viewSpec.link | `any` | | A field containing information for forming a [link](#link) for a value |
| viewSpec.oneOfParams | `object` | | [Parameters](#oneofparams) that must be passed to oneof input |
| viewSpec.placeholder | `string` | | A short hint displayed in the field before the user enters the value |

### StringSpec

Expand Down
1 change: 1 addition & 0 deletions src/lib/core/types/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface ObjectSpec<LinkType = any> {
oneOfParams?: {
toggler?: 'select' | 'radio';
};
placeholder?: string;
};
}

Expand Down
25 changes: 25 additions & 0 deletions src/lib/kit/components/Inputs/MultiOneOf/MultiOneOf.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@import '../../../styles/variables';

.#{$ns}multi-oneof {
&__select {
max-width: 305px;
}

&__content {
&_flat {
& > .#{$ns}group-indent {
margin: 0;
border-left: none;
padding: 0;

& > .#{$ns}use-search {
margin-top: 15px;

&:empty {
display: none;
}
}
}
}
}
}
149 changes: 149 additions & 0 deletions src/lib/kit/components/Inputs/MultiOneOf/MultiOneOf.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React from 'react';

import {Select} from '@gravity-ui/uikit';
import _ from 'lodash';

import {
Controller,
FieldValue,
ObjectIndependentInput,
ObjectIndependentInputProps,
ValidateError,
} from '../../../../core';
import {block} from '../../../utils';
import {GroupIndent} from '../../GroupIndent';

import './MultiOneOf.scss';

const b = block('multi-oneof');

export interface MultiOneOfProps extends ObjectIndependentInputProps {
withoutIndent?: boolean;
}

export const MultiOneOf: React.FC<MultiOneOfProps> = (props) => {
const {name, input, spec, Layout, withoutIndent} = props;
const {value, onBlur, onChange, onFocus, parentOnUnmount} = input;

const [valueSelect, setValueSelect] = React.useState(() => (value ? Object.keys(value) : []));

const handleOpenChange = React.useCallback(
(open: boolean) => {
if (open) {
onFocus();
} else {
onBlur();
}
},
[onFocus, onBlur],
);

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

const specProperties = React.useMemo(
() => (_.isObjectLike(spec.properties) ? spec.properties! : {}),
[spec.properties],
);

const options = React.useMemo(
NasgulNexus marked this conversation as resolved.
Show resolved Hide resolved
() =>
(spec.viewSpec.order || Object.keys(specProperties)).map((value) => {
const title =
spec.description?.[value] ||
specProperties[value]?.viewSpec.layoutTitle ||
value ||
'';

return {
value,
title,
content: title,
};
}),
[spec.description, spec.viewSpec.order, specProperties],
);

const filterable = React.useMemo(() => (options.length || 0) > 9, [options.length]);

const selectInput = React.useMemo(() => {
const select = (
<Select
width="max"
value={valueSelect}
options={options}
onUpdate={setValueSelect}
onOpenChange={handleOpenChange}
disabled={spec.viewSpec.disabled}
placeholder={spec.viewSpec.placeholder}
filterable={filterable}
multiple
className={b('select')}
qa={name}
/>
);

if (Layout) {
return <Layout {...props}>{select}</Layout>;
}

return <React.Fragment>{select}</React.Fragment>;
}, [
Layout,
filterable,
handleOpenChange,
name,
options,
props,
spec.viewSpec.disabled,
spec.viewSpec.placeholder,
valueSelect,
]);

if (!options) {
return null;
}

return (
<React.Fragment>
{selectInput}
<div
className={b('content', {
flat: withoutIndent,
})}
>
<GroupIndent>
{valueSelect.map((property) => (
<React.Fragment key={property}>
{specProperties && specProperties[property] ? (
<Controller
name={`${name}.${property}`}
spec={specProperties[property]}
parentOnUnmount={parentOnUnmount}
parentOnChange={parentOnChange}
value={input.value?.[property]}
/>
) : null}
</React.Fragment>
))}
</GroupIndent>
</div>
</React.Fragment>
);
};

export const MultiOneOfFlat: ObjectIndependentInput = (props) => (
<MultiOneOf {...props} withoutIndent />
);
1 change: 1 addition & 0 deletions src/lib/kit/components/Inputs/MultiOneOf/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './MultiOneOf';
5 changes: 3 additions & 2 deletions src/lib/kit/components/Inputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ export * from './ArrayBase';
export * from './CardOneOf';
export * from './Checkbox';
export * from './FileInput';
export * from './MonacoInput';
export * from './MultiOneOf';
export * from './MultiSelect';
export * from './NumberWithScale';
export * from './ObjectBase';
export * from './ObjectValueInput';
export * from './OneOf';
Expand All @@ -15,5 +18,3 @@ export * from './Text';
export * from './TextArea';
export * from './TextContent';
export * from './TextLink';
export * from './NumberWithScale';
export * from './MonacoInput';
35 changes: 35 additions & 0 deletions src/lib/kit/components/Views/MultiOneOfView/MultiOneOfView.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@import '../../../styles/variables.scss';

.#{$ns}multi-oneof-view {
&__tooltip {
overflow-wrap: break-word;
}

&__tooltip-container {
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
display: block;
margin-bottom: 6px;

&:last-child {
margin-bottom: 0;
}
}

&__content {
&_multiple-values {
& > .#{$ns}group-indent {
padding-top: 0px;
}
}

&_flat {
& > .#{$ns}group-indent {
margin: 0 0 20px;
border-left: none;
padding: 0;
}
}
}
}
102 changes: 102 additions & 0 deletions src/lib/kit/components/Views/MultiOneOfView/MultiOneOfView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import React from 'react';

import {Popover} from '@gravity-ui/uikit';
import _ from 'lodash';

import {ObjectIndependentView, ObjectIndependentViewProps, ViewController} from '../../../../core';
import {block} from '../../../utils';
import {GroupIndent} from '../../GroupIndent';

import './MultiOneOfView.scss';

const b = block('multi-oneof-view');

export interface MultiOneOfViewProps extends ObjectIndependentViewProps {
withoutIndent?: boolean;
}

export const MultiOneOfView: React.FC<MultiOneOfViewProps> = (props) => {
const {name, value, Layout, spec, withoutIndent} = props;

const specProperties = React.useMemo(
() => (_.isObjectLike(spec.properties) ? spec.properties! : {}),
[spec.properties],
);

const values = React.useMemo(() => Object.keys(value || []), [value]);

const items = React.useMemo(
() =>
values.map((value) => {
const title =
spec.description?.[value] ||
specProperties[value]?.viewSpec.layoutTitle ||
value ||
'';

return title;
}),
[spec.description, specProperties, values],
);

const selectView = React.useMemo(() => {
const selectView = (
<React.Fragment>
{items.map((item) => {
return (
<Popover
placement={['bottom', 'top']}
key={item}
content={item}
className={b('tooltip-container')}
contentClassName={b('tooltip')}
disabled={item.length < 51}
>
{item}
</Popover>
);
})}
</React.Fragment>
);

if (Layout) {
return (
<Layout {...props} value={values as any}>
{selectView}
</Layout>
);
}

return <React.Fragment>{selectView}</React.Fragment>;
}, [Layout, items, props, values]);

if (values.length === 0) {
return null;
}

return (
<React.Fragment>
{selectView}
<div
className={b('content', {flat: withoutIndent, 'multiple-values': items.length > 1})}
>
<GroupIndent>
{values.map((value) => (
<React.Fragment key={value}>
{specProperties && specProperties[value] ? (
<ViewController
name={`${name}.${value}`}
spec={specProperties[value]}
/>
) : null}
</React.Fragment>
))}
</GroupIndent>
</div>
</React.Fragment>
);
};

export const MultiOneOfFlatView: ObjectIndependentView = (props) => (
<MultiOneOfView {...props} withoutIndent />
);
1 change: 1 addition & 0 deletions src/lib/kit/components/Views/MultiOneOfView/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './MultiOneOfView';
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 @@ -3,6 +3,7 @@ export * from './BaseView';
export * from './CardOneOfView';
export * from './FileInputView';
export * from './MonacoInputView';
export * from './MultiOneOfView';
export * from './MultiSelectView';
export * from './NumberWithScaleView';
export * from './ObjectBaseView';
Expand Down
Loading
Loading