Skip to content

Commit 13702de

Browse files
authored
feat(Select): added additional params for select (#109)
1 parent 31480c3 commit 13702de

19 files changed

+205
-9
lines changed

docs/spec.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
3232
| viewSpec.placeholder | `string` | | A short hint displayed in the field before the user enters the value |
3333
| viewSpec.addButtonPosition | `"down"/"right"` | | The location of the button adding a new element to the array. Default value "down". |
3434
| viewSpec.hideInput | `boolean` | | Hide input |
35+
| viewSpec.selectParams | `object` | | [Parameters](#selectparams) additional options for the selector |
3536

3637
### BooleanSpec
3738

@@ -123,6 +124,7 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
123124
| viewSpec.copy | `boolean` | | For `true`, will add a copy value button |
124125
| viewSpec.hideInput | `boolean` | | Hide input |
125126
| viewSpec.textContentParams | `object` | | [Parameters](#textcontentparams) that must be passed to text content |
127+
| viewSpec.selectParams | `object` | | [Parameters](#selectparams) additional options for the selector |
126128

127129
#### SizeParams
128130

@@ -162,6 +164,13 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
162164
| icon | `string` | | Icon name from the [library](https://gravity-ui.com/icons) |
163165
| 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 |
164166

167+
#### SelectParams
168+
169+
| Property | Type | Required | Description |
170+
| :---------------- | :----------------------- | :------: | :------------------------------ |
171+
| filterPlaceholder | `string` | | Placeholder for filter |
172+
| meta | `Record<string, string>` | | Additional text for enum values |
173+
165174
#### Link
166175

167176
A component that serves as a wrapper for the value, if necessary, rendering the value as a link.

src/lib/core/types/specs.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export interface ArraySpec<LinkType = any> {
3232
placeholder?: string;
3333
addButtonPosition?: 'down' | 'right';
3434
hideInput?: boolean;
35+
selectParams?: {
36+
filterPlaceholder?: string;
37+
meta?: Record<string, string>;
38+
};
3539
};
3640
}
3741

@@ -141,6 +145,10 @@ export interface StringSpec<LinkType = any> {
141145
ignoreText?: boolean;
142146
};
143147
copy?: boolean;
148+
selectParams?: {
149+
filterPlaceholder?: string;
150+
meta?: Record<string, string>;
151+
};
144152
};
145153
}
146154

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
.#{$ns}multi-select {
44
max-width: 305px;
5+
6+
&__meta-text {
7+
display: block;
8+
}
59
}

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import {Select} from '@gravity-ui/uikit';
3+
import {Select, Text} from '@gravity-ui/uikit';
44

55
import {ArrayInput, FieldArrayValue, transformArrIn, transformArrOut} from '../../../../core';
66
import {block} from '../../../utils';
@@ -19,12 +19,34 @@ export const MultiSelect: ArrayInput = ({name, input, spec}) => {
1919
spec.enum?.map((id) => ({
2020
id,
2121
value: id,
22-
content: spec.description?.[id] || id,
22+
text: spec.description?.[id] || id,
23+
content: spec.viewSpec.selectParams?.meta?.[id] ? (
24+
<div key={id}>
25+
<Text>{spec.description?.[id] || id}</Text>
26+
<Text color="secondary" className={b('meta-text')}>
27+
{spec.viewSpec.selectParams.meta[id]}
28+
</Text>
29+
</div>
30+
) : (
31+
spec.description?.[id] || id
32+
),
2333
key: id,
2434
})),
25-
[spec.enum, spec.description],
35+
[spec.enum, spec.description, spec.viewSpec.selectParams?.meta],
2636
);
2737

38+
const renderOption = React.useCallback((option: {value: string; content?: React.ReactNode}) => {
39+
return <React.Fragment key={option.value}>{option.content || option.value}</React.Fragment>;
40+
}, []);
41+
42+
const getOptionHeight = React.useCallback(() => {
43+
if (spec.viewSpec.selectParams?.meta) {
44+
return 44;
45+
}
46+
47+
return 28;
48+
}, [spec.viewSpec.selectParams?.meta]);
49+
2850
const handleToggle = React.useCallback(
2951
(open: boolean) => {
3052
if (open) {
@@ -54,6 +76,9 @@ export const MultiSelect: ArrayInput = ({name, input, spec}) => {
5476
disabled={spec.viewSpec.disabled}
5577
placeholder={spec.viewSpec.placeholder}
5678
filterable={filterable}
79+
filterPlaceholder={spec.viewSpec.selectParams?.filterPlaceholder}
80+
renderOption={renderOption}
81+
getOptionHeight={getOptionHeight}
5782
multiple
5883
qa={name}
5984
/>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
.#{$ns}select {
44
// TODO: Remove this styles
55
max-width: 305px;
6+
7+
&__meta-text {
8+
display: block;
9+
}
610
}

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import {Select as SelectBase} from '@gravity-ui/uikit';
3+
import {Select as SelectBase, Text} from '@gravity-ui/uikit';
44

55
import {StringInput} from '../../../../core';
66
import {block} from '../../../utils';
@@ -19,12 +19,34 @@ export const Select: StringInput = ({name, input, spec}) => {
1919
spec.enum?.map((id) => ({
2020
id,
2121
value: id,
22-
content: spec.description?.[id] || id,
22+
text: spec.description?.[id] || id,
23+
content: spec.viewSpec.selectParams?.meta?.[id] ? (
24+
<div key={id}>
25+
<Text>{spec.description?.[id] || id}</Text>
26+
<Text color="secondary" className={b('meta-text')}>
27+
{spec.viewSpec.selectParams.meta[id]}
28+
</Text>
29+
</div>
30+
) : (
31+
spec.description?.[id] || id
32+
),
2333
key: id,
2434
})),
25-
[spec.enum, spec.description],
35+
[spec.enum, spec.description, spec.viewSpec.selectParams?.meta],
2636
);
2737

38+
const renderOption = React.useCallback((option: {value: string; content?: React.ReactNode}) => {
39+
return <React.Fragment key={option.value}>{option.content || option.value}</React.Fragment>;
40+
}, []);
41+
42+
const getOptionHeight = React.useCallback(() => {
43+
if (spec.viewSpec.selectParams?.meta) {
44+
return 44;
45+
}
46+
47+
return 28;
48+
}, [spec.viewSpec.selectParams?.meta]);
49+
2850
const handleChange = React.useCallback((v: string[]) => onChange(v[0]), [onChange]);
2951

3052
const handleToggle = React.useCallback(
@@ -49,6 +71,9 @@ export const Select: StringInput = ({name, input, spec}) => {
4971
disabled={spec.viewSpec.disabled}
5072
placeholder={spec.viewSpec.placeholder}
5173
filterable={filterable}
74+
filterPlaceholder={spec.viewSpec.selectParams?.filterPlaceholder}
75+
getOptionHeight={getOptionHeight}
76+
renderOption={renderOption}
5277
qa={name}
5378
/>
5479
);

src/stories/ArrayBase.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const excludeOptions = [
3838
'viewSpec.type',
3939
'viewSpec.table',
4040
'viewSpec.placeholder',
41+
'viewSpec.selectParams',
4142
];
4243

4344
const template = () => {

src/stories/ArraySelect.stories.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,39 @@ export default {
1313

1414
const spec: ArraySpec = {
1515
type: SpecTypes.Array,
16-
enum: ['foo', 'bar', 'rab', 'oof'],
16+
enum: ['foo', 'bar', 'rab', 'oof', 'fooBar', 'fooOof', 'barFoo', 'barOof', 'fooFoo', 'barBar'],
1717
description: {
1818
foo: 'Option 1',
1919
bar: 'Option 2',
2020
rab: 'Option 3',
2121
oof: 'Option 4',
22+
fooBar: 'Option 5',
23+
fooOof: 'Option 6',
24+
barFoo: 'Option 7',
25+
barOof: 'Option 8',
26+
fooFoo: 'Option 9',
27+
barBar: 'Option 10',
2228
},
2329
viewSpec: {
2430
type: 'select',
2531
layout: 'row',
2632
layoutTitle: 'Select',
2733
placeholder: 'placeholder text',
34+
selectParams: {
35+
filterPlaceholder: 'filter placeholder',
36+
meta: {
37+
foo: 'Additional text 1',
38+
bar: 'Additional text 2',
39+
rab: 'Additional text 3',
40+
oof: 'Additional text 4',
41+
fooBar: 'Additional text 5',
42+
fooOof: 'Additional text 6',
43+
barFoo: 'Additional text 7',
44+
barOof: 'Additional text 8',
45+
fooFoo: 'Additional text 9',
46+
barBar: 'Additional text 10',
47+
},
48+
},
2849
},
2950
};
3051

src/stories/ArrayTable.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const excludeOptions = [
6161
'viewSpec.placeholder',
6262
'viewSpec.itemPrefix',
6363
'viewSpec.addButtonPosition',
64+
'viewSpec.selectParams',
6465
];
6566

6667
const value = [

src/stories/StringBase.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const excludeOptions = [
2727
'viewSpec.monacoParams',
2828
'viewSpec.textContentParams',
2929
'viewSpec.fileInput',
30+
'viewSpec.selectParams',
3031
];
3132

3233
const template = (spec: StringSpec = baseSpec) => {

0 commit comments

Comments
 (0)