-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathPreviewHeader.tsx
More file actions
175 lines (145 loc) · 6.11 KB
/
PreviewHeader.tsx
File metadata and controls
175 lines (145 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React from 'react';
import {ChevronsExpandUpRight, LayoutHeader, LayoutSideContent, Xmark} from '@gravity-ui/icons';
import {Button, Icon, TextInput, type TextInputProps} from '@gravity-ui/uikit';
import block from 'bem-cn-lite';
import {i18n} from 'i18n';
import _debounce from 'lodash/debounce';
import {formatNumber} from 'shared/modules/format-units/formatUnit';
import type {DatasetPreviewView} from 'units/datasets/store/types';
import {VIEW_PREVIEW} from '../../constants';
import {ROWS_MAX_COUNT} from './constants';
import {shouldFetchPreview} from './utils';
import './PreviewHeader.scss';
const b = block('preview-header');
const ICON_WIDTH = 24;
interface Props {
amountPreviewRows: number;
view: string;
toggleViewPreview: (args: {view: DatasetPreviewView}) => void;
closePreview: () => void;
changeAmountPreviewRows: (args: {amountPreviewRows: number}) => void;
refetchPreviewDataset: () => void;
}
interface State {
internalAmountPreviewRows: string;
}
const TEXT_INPUT_CONTROL_PROPS: TextInputProps['controlProps'] = {
min: 0,
step: 1,
};
class PreviewHeader extends React.Component<Props, State> {
static defaultProps = {};
private debouncedChangeAmountPreviewRows = _debounce(this.props.refetchPreviewDataset, 1000);
constructor(props: Props) {
super(props);
this.state = {
internalAmountPreviewRows: props.amountPreviewRows.toString(),
};
}
render() {
const {view} = this.props;
const {internalAmountPreviewRows} = this.state;
return (
<div className={b({view})}>
<span className={b('label', {bold: true})}>
{i18n('dataset.dataset-editor.modify', 'section_preview')}
</span>
<div className={b('amount-preview-rows')}>
<span>{i18n('dataset.dataset-editor.modify', 'field_display-rows')}</span>
<TextInput
className={b('amount-preview-rows-inp')}
type="number"
value={internalAmountPreviewRows}
onUpdate={this.changeAmountPreviewRows}
controlProps={TEXT_INPUT_CONTROL_PROPS}
/>
<span className={b('fade-text')}>
{i18n('dataset.dataset-editor.modify', 'label_max-amount-rows', {
count: formatNumber(ROWS_MAX_COUNT),
})}
</span>
</div>
<div className={b('resize-panel')}>
{view !== VIEW_PREVIEW.FULL && (
<Button
className={b('preview-switcher-btn')}
view="flat"
title={i18n('dataset.dataset-editor.modify', 'button_preview-full')}
onClick={this.togglePreviewFull}
>
<Icon data={ChevronsExpandUpRight} width={ICON_WIDTH} />
</Button>
)}
{view !== VIEW_PREVIEW.RIGHT && (
<Button
className={b('preview-switcher-btn', {right: true})}
view="flat"
title={i18n('dataset.dataset-editor.modify', 'button_preview-right')}
onClick={this.togglePreviewRight}
>
<Icon
data={LayoutSideContent}
className={b('switcher-icon-right')}
width={ICON_WIDTH}
/>
</Button>
)}
{view !== VIEW_PREVIEW.BOTTOM && (
<Button
className={b('preview-switcher-btn', {bottom: true})}
view="flat"
title={i18n('dataset.dataset-editor.modify', 'button_preview-bottom')}
onClick={this.togglePreviewBottom}
>
<Icon
data={LayoutHeader}
className={b('switcher-icon-bottom')}
width={ICON_WIDTH}
/>
</Button>
)}
<Button
className={b('preview-switcher-btn')}
view="flat"
title={i18n('dataset.dataset-editor.modify', 'button_preview-close')}
onClick={this.closePreview}
>
<Icon data={Xmark} width={ICON_WIDTH} />
</Button>
</div>
</div>
);
}
changeAmountPreviewRows = (amountPreviewRowsString: string) => {
const {changeAmountPreviewRows} = this.props;
if (this.debouncedChangeAmountPreviewRows) {
this.debouncedChangeAmountPreviewRows.cancel();
}
if (shouldFetchPreview(amountPreviewRowsString)) {
this.debouncedChangeAmountPreviewRows();
}
this.setState({internalAmountPreviewRows: amountPreviewRowsString});
const amountPreviewRowsNumber = parseInt(amountPreviewRowsString, 10);
if (Number.isNaN(amountPreviewRowsNumber)) {
return;
}
changeAmountPreviewRows({amountPreviewRows: amountPreviewRowsNumber});
};
togglePreviewFull = () => {
const {toggleViewPreview} = this.props;
toggleViewPreview({view: VIEW_PREVIEW.FULL});
};
togglePreviewBottom = () => {
const {toggleViewPreview} = this.props;
toggleViewPreview({view: VIEW_PREVIEW.BOTTOM});
};
togglePreviewRight = () => {
const {toggleViewPreview} = this.props;
toggleViewPreview({view: VIEW_PREVIEW.RIGHT});
};
closePreview = () => {
const {closePreview} = this.props;
closePreview();
};
}
export default PreviewHeader;