|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import {HelpPopover} from '@gravity-ui/components'; |
| 4 | +import {TrashBin} from '@gravity-ui/icons'; |
| 5 | +import {Button, Icon, Text} from '@gravity-ui/uikit'; |
| 6 | + |
| 7 | +import { |
| 8 | + FieldValue, |
| 9 | + LayoutProps, |
| 10 | + Spec, |
| 11 | + StringSpec, |
| 12 | + isArrayItem, |
| 13 | + isArraySpec, |
| 14 | + isObjectSpec, |
| 15 | + withGenerateButton, |
| 16 | +} from '../../../../core'; |
| 17 | +import {ErrorWrapper, GenerateRandomValueButton} from '../../../components'; |
| 18 | +import {block} from '../../../utils'; |
| 19 | + |
| 20 | +import './Column.scss'; |
| 21 | + |
| 22 | +const b = block('column'); |
| 23 | + |
| 24 | +interface ColumnProps {} |
| 25 | + |
| 26 | +const ColumnBase = <T extends FieldValue, S extends Spec>({ |
| 27 | + name, |
| 28 | + spec, |
| 29 | + input, |
| 30 | + meta, |
| 31 | + children, |
| 32 | +}: LayoutProps<T, undefined, undefined, S> & ColumnProps) => { |
| 33 | + const arrayItem = React.useMemo(() => isArrayItem(name), [name]); |
| 34 | + const generateButton = React.useMemo(() => withGenerateButton(spec), [spec]); |
| 35 | + |
| 36 | + return ( |
| 37 | + <div className={b()}> |
| 38 | + <div className={b('first-row')}> |
| 39 | + <div className={b('first-row-inner')}> |
| 40 | + <Text className={b('title', {required: spec.required})}> |
| 41 | + {spec.viewSpec.layoutTitle} |
| 42 | + </Text> |
| 43 | + {spec.viewSpec.layoutDescription ? ( |
| 44 | + <span className={b('note')}> |
| 45 | + <Text className={b('note-inner')}> |
| 46 | + <HelpPopover |
| 47 | + htmlContent={spec.viewSpec.layoutDescription} |
| 48 | + placement={['bottom', 'top']} |
| 49 | + /> |
| 50 | + </Text> |
| 51 | + </span> |
| 52 | + ) : null} |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + <div className={b('second-row')}> |
| 56 | + <div className={b('second-row-inner')}> |
| 57 | + <ErrorWrapper |
| 58 | + name={name} |
| 59 | + meta={meta} |
| 60 | + withoutChildErrorStyles={ |
| 61 | + // TODO: remove condition spec.viewSpec.type !== 'select' |
| 62 | + (isArraySpec(spec) && spec.viewSpec.type !== 'select') || |
| 63 | + isObjectSpec(spec) |
| 64 | + } |
| 65 | + className={b('error-wrapper')} |
| 66 | + > |
| 67 | + {children} |
| 68 | + </ErrorWrapper> |
| 69 | + {generateButton ? ( |
| 70 | + <GenerateRandomValueButton |
| 71 | + spec={spec as StringSpec} |
| 72 | + onChange={input.onChange as (value: string) => void} |
| 73 | + /> |
| 74 | + ) : null} |
| 75 | + {arrayItem ? ( |
| 76 | + <Button |
| 77 | + view="flat-secondary" |
| 78 | + className={b('remove-button')} |
| 79 | + onClick={input.onDrop} |
| 80 | + qa={`${name}-remove-item`} |
| 81 | + > |
| 82 | + <Icon data={TrashBin} size={16} /> |
| 83 | + </Button> |
| 84 | + ) : null} |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export const Column = <T extends FieldValue, S extends Spec>( |
| 92 | + props: LayoutProps<T, undefined, undefined, S>, |
| 93 | +) => <ColumnBase {...props} />; |
0 commit comments