Skip to content

Commit

Permalink
feat: add copy button for view layouts (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
bocembocem authored Jun 6, 2023
1 parent 8f7c4c6 commit c31fcbf
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 16 deletions.
2 changes: 2 additions & 0 deletions docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
| viewSpec.layoutOpen | `boolean` | | Expand [Layout](./config.md#layouts) at the first rendering |
| viewSpec.link | `any` | | A field containing information for forming a [link](#link) for a value |
| viewSpec.placeholder | `string` | | A short hint displayed in the field before the user enters the value |
| viewSpec.copy | `boolean` | | For `true`, will add a copy value button |

### ObjectSpec

Expand Down Expand Up @@ -113,6 +114,7 @@ type Spec = ArraySpec | BooleanSpec | NumberSpec | ObjectSpec | StringSpec;
| viewSpec.placeholder | `string` | | A short hint displayed in the field before the user enters the value |
| viewSpec.themeLabel | `'normal'` `'info'` `'danger'` `'warning'` `'success'` `'unknown'` | | Label color |
| viewSpec.fileInput | `object` | | [Parameters](#FileInput) that must be passed to file input |
| viewSpec.copy | `boolean` | | For `true`, will add a copy value button |

#### SizeParams

Expand Down
2 changes: 2 additions & 0 deletions src/lib/core/types/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface NumberSpec<LinkType = any> {
layoutOpen?: boolean;
link?: LinkType;
placeholder?: string;
copy?: boolean;
};
}

Expand Down Expand Up @@ -125,6 +126,7 @@ export interface StringSpec<LinkType = any> {
readAsMethod?: ReadAsMethod;
ignoreText?: boolean;
};
copy?: boolean;
};
}

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

.#{$ns}copy-button {
display: none;
margin: 2px 0 0 5px;
}
28 changes: 28 additions & 0 deletions src/lib/kit/components/CopyButton/CopyButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';

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

import {FormValue, Spec, isNumberSpec, isStringSpec} from '../../../core';
import {block} from '../../utils';

import './CopyButton.scss';

const b = block('copy-button');

export interface CopyButtonProps {
spec: Spec;
value: FormValue;
}

/**
*
* Use the with-copy-button scss mixin for visible on hover
*/

export const CopyButton: React.FC<CopyButtonProps> = ({spec, value}) => {
if ((isStringSpec(spec) || isNumberSpec(spec)) && spec.viewSpec.copy) {
return <ClipboardButton className={b()} text={`${value}`} size={14} />;
}

return null;
};
1 change: 1 addition & 0 deletions src/lib/kit/components/CopyButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CopyButton';
3 changes: 3 additions & 0 deletions src/lib/kit/components/ViewLayouts/ViewRow/ViewRow.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import '../../../styles/variables.scss';
@import '../../../styles/mixins.scss';

.#{$ns}view-row {
width: 100%;
Expand Down Expand Up @@ -46,4 +47,6 @@
}
}
}

@include with-copy-button();
}
2 changes: 2 additions & 0 deletions src/lib/kit/components/ViewLayouts/ViewRow/ViewRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import {FormValue, Spec, ViewLayoutProps} from '../../../../core';
import {CopyButton} from '../../../../kit';
import {block, isNotEmptyValue} from '../../../utils';

import './ViewRow.scss';
Expand All @@ -25,6 +26,7 @@ export const ViewRow = <T extends FormValue, S extends Spec>({
<div className={b('dots')} />
</div>
<div className={b('right')}>{children}</div>
<CopyButton spec={spec} value={value} />
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
@import '../../../styles/variables.scss';
@import '../../../styles/mixins.scss';

.#{$ns}view-table-cell {
display: flex;
flex-direction: column;
justify-content: center;

& > .#{$ns}view-row:last-child {
margin-bottom: 0;
}

& > .#{$ns}view-transparent {
margin-bottom: 6px;
&__inner {
display: flex;
flex-direction: column;
justify-content: center;

&:last-child {
& > .#{$ns}view-row:last-child {
margin-bottom: 0;
}

& > .#{$ns}view-transparent {
margin-bottom: 6px;

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

@include with-copy-button();
}
20 changes: 17 additions & 3 deletions src/lib/kit/components/ViewLayouts/ViewTableCell/ViewTableCell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import {FormValue, Spec, ViewLayoutProps} from '../../../../core';
import {CopyButton} from '../../../../kit';
import {block, isNotEmptyValue} from '../../../utils';

import './ViewTableCell.scss';
Expand All @@ -11,6 +12,19 @@ export const ViewTableCell = <T extends FormValue, S extends Spec>({
value,
spec,
children,
}: ViewLayoutProps<T, S>): JSX.Element => (
<div className={b()}>{isNotEmptyValue(value, spec) ? children : '—'}</div>
);
}: ViewLayoutProps<T, S>): JSX.Element => {
const empty = isNotEmptyValue(value, spec);

return (
<div className={b()}>
{empty ? (
<React.Fragment>
<div className={b('inner')}>{children}</div>
<CopyButton spec={spec} value={value} />
</React.Fragment>
) : (
'—'
)}
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
@import '../../../styles/variables.scss';
@import '../../../styles/mixins.scss';

.#{$ns}view-transparent {
display: flex;
margin-bottom: 20px;

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

@include with-copy-button();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import {FormValue, Spec, ViewLayoutProps} from '../../../../core';
import {CopyButton} from '../../../../kit';
import {block, isNotEmptyValue} from '../../../utils';

import './ViewTransparent.scss';
Expand All @@ -12,4 +13,9 @@ export const ViewTransparent = <T extends FormValue, S extends Spec>({
spec,
children,
}: ViewLayoutProps<T, S>) =>
isNotEmptyValue(value, spec) ? <div className={b()}>{children}</div> : null;
isNotEmptyValue(value, spec) ? (
<div className={b()}>
<div>{children}</div>
<CopyButton spec={spec} value={value} />
</div>
) : null;
1 change: 1 addition & 0 deletions src/lib/kit/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './AccordeonCard';
export * from './Card';
export * from './CopyButton';
export * from './ErrorWrapper';
export * from './GroupIndent';
export * from './Inputs';
Expand Down
5 changes: 5 additions & 0 deletions src/lib/kit/constants/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
},
layouts: {
row: ViewRow,
row_verbose: ViewRow,
accordeon: ViewAccordeon,
section: ViewSection,
section2: ViewSection2,
Expand All @@ -306,6 +307,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
},
layouts: {
row: ViewRow,
row_verbose: ViewRow,
table_item: ViewTableCell,
},
},
Expand All @@ -315,6 +317,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
},
layouts: {
row: ViewRow,
row_verbose: ViewRow,
table_item: ViewTableCell,
transparent: ViewTransparent,
},
Expand All @@ -331,6 +334,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
},
layouts: {
row: ViewRow,
row_verbose: ViewRow,
accordeon: ViewAccordeon,
section: ViewSection,
section2: ViewSection2,
Expand All @@ -354,6 +358,7 @@ export const dynamicViewConfig: DynamicViewConfig = {
},
layouts: {
row: ViewRow,
row_verbose: ViewRow,
table_item: ViewTableCell,
transparent: ViewTransparent,
section: ViewSection,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/kit/styles/mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import './variables.scss';

@mixin with-copy-button() {
&:hover > .#{$ns}copy-button {
display: block;
}
}
7 changes: 4 additions & 3 deletions src/stories/Editor.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ const spec: ObjectSpec = {
properties: {
id: {
type: SpecTypes.Number,
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'id'},
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'id', copy: true},
},
name: {
type: SpecTypes.String,
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Name'},
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Name', copy: true},
},
description: {
type: SpecTypes.String,
viewSpec: {type: 'textarea', layout: 'row', layoutTitle: 'Description'},
viewSpec: {type: 'textarea', layout: 'row', layoutTitle: 'Description', copy: true},
},
settings: {
type: SpecTypes.Boolean,
Expand Down Expand Up @@ -102,6 +102,7 @@ const spec: ObjectSpec = {
type: 'base',
layout: 'row',
layoutTitle: 'Label',
copy: true,
},
},
viewSpec: {
Expand Down
1 change: 1 addition & 0 deletions src/stories/StringMonaco.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const excludeOptions = [
'viewSpec.placeholder',
'viewSpec.themeLabel',
'viewSpec.fileInput',
'viewSpec.copy',
];

const template = (spec: StringSpec = baseSpec) => {
Expand Down
1 change: 1 addition & 0 deletions src/stories/StringPassword.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const excludeOptions = [
'viewSpec.monacoParams',
'viewSpec.themeLabel',
'viewSpec.fileInput',
'viewSpec.copy',
];

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

const template = (spec: StringSpec = baseSpec) => {
Expand Down
9 changes: 9 additions & 0 deletions src/stories/components/InputPreview/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ const fileInput: ObjectSpec = {
},
};

const copy: BooleanSpec = {
type: SpecTypes.Boolean,
viewSpec: {type: 'base', layout: 'row', layoutTitle: 'Copy'},
};

export const getArrayOptions = (): ObjectSpec => ({
type: SpecTypes.Object,
required: true,
Expand Down Expand Up @@ -427,6 +432,7 @@ export const getNumberOptions = (): ObjectSpec => ({
layoutDescription,
layoutOpen,
placeholder,
copy,
},
[
'disabled',
Expand All @@ -436,6 +442,7 @@ export const getNumberOptions = (): ObjectSpec => ({
'layoutDescription',
'layoutOpen',
'placeholder',
'copy',
],
),
},
Expand Down Expand Up @@ -512,6 +519,7 @@ export const getStringOptions = (): ObjectSpec => ({
placeholder,
themeLabel,
fileInput,
copy,
},
[
'disabled',
Expand All @@ -525,6 +533,7 @@ export const getStringOptions = (): ObjectSpec => ({
'placeholder',
'themeLabel',
'fileInput',
'copy',
],
),
},
Expand Down

0 comments on commit c31fcbf

Please sign in to comment.