Skip to content

Commit 25e9a35

Browse files
authored
feat: add flat oneof input, add oneof params to object spec (#44)
1 parent ce52d65 commit 25e9a35

File tree

16 files changed

+212
-31
lines changed

16 files changed

+212
-31
lines changed

docs/spec.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
8484
| viewSpec.layoutOpen | `boolean` | | Expand [Layout](./config.md#layouts) at the first rendering |
8585
| viewSpec.order | `string[]` | | Array of `properties` keys in the right order |
8686
| viewSpec.link | `any` | | A field containing information for forming a [link](#link) for a value |
87+
| viewSpec.oneOfParams | `object` | | [Parameters](#oneofparams) that must be passed to oneof input |
8788

8889
### StringSpec
8990

@@ -128,6 +129,12 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
128129
| language | `string` | yes | Syntax highlighting language |
129130
| fontSize | `string` | | Font size |
130131

132+
#### OneOfParams
133+
134+
| Property | Type | Required | Description |
135+
| :------- | :------------------- | :------: | :---------- |
136+
| toggler | `'select'` `'radio'` | | Switch type |
137+
131138
#### FileInput
132139

133140
| Property | Type | Required | Description |

src/lib/core/types/specs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export interface ObjectSpec<LinkType = any> {
8383
layoutOpen?: boolean;
8484
order?: string[];
8585
link?: LinkType;
86+
oneOfParams?: {
87+
toggler?: 'select' | 'radio';
88+
};
8689
};
8790
}
8891

src/lib/kit/components/Card/Card.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
&__header-left {
5252
display: flex;
5353
align-items: center;
54+
flex-grow: 1;
5455
}
5556

5657
&__header-right {
5758
display: flex;
5859
align-items: center;
5960

6061
& > * {
61-
margin-left: 5px;
62+
margin-left: 10px;
6263
}
6364
}
6465

src/lib/kit/components/Inputs/OneOf/OneOf.scss

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,45 @@
1010
}
1111
}
1212

13-
.#{$ns}group-indent {
14-
all: unset;
15-
16-
& > .#{$ns}use-search {
17-
padding-top: 11px;
18-
padding-left: $normalOffset;
19-
margin-top: 4px;
20-
margin-bottom: 20px;
21-
margin-left: 5px;
22-
border-left: 1px solid var(--yc-color-line-generic-accent);
23-
24-
&:empty {
25-
display: none;
26-
}
13+
&_base {
14+
.#{$ns}group-indent {
15+
all: unset;
16+
17+
& > .#{$ns}use-search {
18+
padding-top: 11px;
19+
padding-left: $normalOffset;
20+
margin-top: 4px;
21+
margin-bottom: 20px;
22+
margin-left: 5px;
23+
border-left: 1px solid var(--yc-color-line-generic-accent);
24+
25+
&:empty {
26+
display: none;
27+
}
2728

28-
&:last-child {
29-
margin-bottom: 0;
29+
&:last-child {
30+
margin-bottom: 0;
31+
}
32+
33+
& > .#{$ns}simple-vertical-accordeon_view {
34+
margin-top: -10px;
35+
}
3036
}
37+
}
38+
}
39+
40+
&_flat {
41+
& > .#{$ns}group-indent {
42+
margin: 0;
43+
border-left: none;
44+
padding: 0;
45+
46+
& > .#{$ns}use-search {
47+
margin-top: 15px;
3148

32-
& > .#{$ns}simple-vertical-accordeon_view {
33-
margin-top: -10px;
49+
&:empty {
50+
display: none;
51+
}
3452
}
3553
}
3654
}

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ import React from 'react';
33
import _ from 'lodash';
44

55
import {GroupIndent} from '../../';
6-
import {Controller, FieldValue, ObjectIndependentInput, ValidateError} from '../../../../core';
6+
import {
7+
Controller,
8+
FieldValue,
9+
ObjectIndependentInput,
10+
ObjectIndependentInputProps,
11+
ValidateError,
12+
} from '../../../../core';
713
import {useOneOf} from '../../../hooks';
814
import {block} from '../../../utils';
915

1016
import './OneOf.scss';
1117

1218
const b = block('oneof');
1319

14-
export const OneOf: ObjectIndependentInput = (props) => {
20+
export interface OneOfProps extends ObjectIndependentInputProps {
21+
withoutIndent?: boolean;
22+
}
23+
24+
const OneOfComponent: React.FC<OneOfProps> = (props) => {
1525
const {oneOfValue, specProperties, toggler} = useOneOf({props});
1626

1727
const parentOnChange = React.useCallback(
@@ -34,7 +44,12 @@ export const OneOf: ObjectIndependentInput = (props) => {
3444
);
3545

3646
return (
37-
<div className={b()}>
47+
<div
48+
className={b({
49+
base: !props.withoutIndent,
50+
flat: props.withoutIndent,
51+
})}
52+
>
3853
<div>{toggler}</div>
3954
{specProperties[oneOfValue] ? (
4055
<GroupIndent>
@@ -51,3 +66,9 @@ export const OneOf: ObjectIndependentInput = (props) => {
5166
</div>
5267
);
5368
};
69+
70+
export const OneOf = OneOfComponent;
71+
72+
export const OneOfFlat: ObjectIndependentInput = (props) => (
73+
<OneOfComponent {...props} withoutIndent />
74+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@import '../../../styles/variables';
2+
3+
.#{$ns}oneof-view {
4+
.#{$ns}group-indent {
5+
margin-bottom: 20px;
6+
}
7+
8+
&_flat {
9+
& > .#{$ns}group-indent {
10+
margin: 0 0 20px;
11+
border-left: none;
12+
padding: 0;
13+
}
14+
}
15+
}

src/lib/kit/components/Views/OneOfView.tsx renamed to src/lib/kit/components/Views/OneOfView/OneOfView.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ import React from 'react';
22

33
import _ from 'lodash';
44

5-
import {GroupIndent} from '../';
6-
import {ObjectIndependentView, ViewController} from '../../../core';
5+
import {GroupIndent} from '../../';
6+
import {ObjectIndependentView, ObjectIndependentViewProps, ViewController} from '../../../../core';
7+
import {block} from '../../../utils';
78

8-
export const OneOfView: ObjectIndependentView = (props) => {
9+
import './OneOfView.scss';
10+
11+
const b = block('oneof-view');
12+
13+
export interface OneOfViewProps extends ObjectIndependentViewProps {
14+
withoutIndent?: boolean;
15+
}
16+
17+
const OneOfViewComponent: React.FC<OneOfViewProps> = (props) => {
918
const {value = {}, spec, Layout, name} = props;
1019

1120
const specProperties = React.useMemo(
@@ -40,7 +49,7 @@ export const OneOfView: ObjectIndependentView = (props) => {
4049
}
4150

4251
return (
43-
<React.Fragment>
52+
<div className={b({flat: props.withoutIndent})}>
4453
{wrappedValue}
4554
{specProperties[valueKey] ? (
4655
<GroupIndent>
@@ -51,6 +60,12 @@ export const OneOfView: ObjectIndependentView = (props) => {
5160
/>
5261
</GroupIndent>
5362
) : null}
54-
</React.Fragment>
63+
</div>
5564
);
5665
};
66+
67+
export const OneOfView = OneOfViewComponent;
68+
69+
export const OneOfFlatView: ObjectIndependentView = (props) => (
70+
<OneOfViewComponent {...props} withoutIndent />
71+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './OneOfView';

src/lib/kit/constants/config.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
OneOf,
3030
OneOfCard,
3131
OneOfCardView,
32+
OneOfFlat,
33+
OneOfFlatView,
3234
OneOfView,
3335
Row,
3436
Row2,
@@ -128,6 +130,7 @@ export const dynamicConfig: DynamicFormConfig = {
128130
object: {
129131
inputs: {
130132
oneof: {Component: OneOf, independent: true},
133+
oneof_flat: {Component: OneOfFlat, independent: true},
131134
card_oneof: {Component: CardOneOf, independent: true},
132135
secret: {Component: Secret, independent: true},
133136
base: {Component: ObjectBase, independent: true},
@@ -315,6 +318,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
315318
object: {
316319
views: {
317320
oneof: {Component: OneOfView, independent: true},
321+
oneof_flat: {Component: OneOfFlatView, independent: true},
318322
card_oneof: {Component: CardOneOfView, independent: true},
319323
secret: undefined,
320324
base: {Component: ObjectBaseView, independent: true},

src/lib/kit/hooks/useOneOf.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ export const useOneOf = ({props, onTogglerChange}: UseOneOfParams) => {
6565

6666
const togglerInput = React.useMemo(() => {
6767
if (
68-
options.length > 3 ||
69-
_.some(options, ({title}) => title.length > MAX_TAB_TITLE_LENGTH)
68+
spec.viewSpec.oneOfParams?.toggler !== 'radio' &&
69+
(spec.viewSpec.oneOfParams?.toggler === 'select' ||
70+
options.length > 3 ||
71+
_.some(options, ({title}) => title.length > MAX_TAB_TITLE_LENGTH))
7072
) {
7173
return (
7274
<Select
@@ -95,7 +97,7 @@ export const useOneOf = ({props, onTogglerChange}: UseOneOfParams) => {
9597
))}
9698
</RadioButton>
9799
);
98-
}, [options, oneOfValue, onOneOfChange, name]);
100+
}, [options, oneOfValue, onOneOfChange, name, spec.viewSpec.oneOfParams?.toggler]);
99101

100102
const toggler = React.useMemo(() => {
101103
if (Layout) {

0 commit comments

Comments
 (0)