1- import { useMemo } from 'react' ;
2-
3- import { Palette , type PaletteOption } from '@gravity-ui/uikit' ;
1+ import { Check } from '@gravity-ui/icons' ;
2+ import { Box , Icon , Tooltip } from '@gravity-ui/uikit' ;
43
54import { cn } from 'src/classname' ;
65import { i18n } from 'src/i18n/yfm-table' ;
76
8- import { CELL_BG_COLORS } from './colors' ;
7+ import { CELL_BG_SWATCHES } from './colors' ;
98
109import './CellBgPalette.scss' ;
1110
@@ -19,40 +18,47 @@ export type CellBgPaletteProps = {
1918} ;
2019
2120export const CellBgPalette : React . FC < CellBgPaletteProps > = function YfmTableCellBgPalette ( {
22- // value,
21+ value,
2322 onSelect,
2423} ) {
25- const options = useMemo < PaletteOption [ ] > (
26- ( ) => [
27- {
28- value : NO_COLOR_VALUE ,
29- content : < span className = { b ( 'swatch' , { none : true } ) } /> ,
30- title : i18n ( 'cells.bg.none' ) ,
31- } ,
32- ...CELL_BG_COLORS . map ( ( color ) => ( {
33- value : color ,
34- content : < span className = { b ( 'swatch' , { color} ) } /> ,
35- title : i18n ( `cells.bg.${ color } ` ) ,
36- } ) ) ,
37- ] ,
38- [ ] ,
39- ) ;
40-
41- // const paletteValue = useMemo(() => (value ? [value] : [NO_COLOR_VALUE]), [value]);
42-
43- const handleUpdate = ( values : string [ ] ) => {
44- const selected = values [ 0 ] ?? NO_COLOR_VALUE ;
45- onSelect ( selected === NO_COLOR_VALUE ? null : selected ) ;
24+ const handleClick = ( swatchValue : string ) => {
25+ if ( swatchValue === value || ( swatchValue === NO_COLOR_VALUE && ! value ) ) return ;
26+ onSelect ( swatchValue === NO_COLOR_VALUE ? null : swatchValue ) ;
4627 } ;
4728
4829 return (
49- < Palette
50- multiple = { false }
51- // value={paletteValue}
52- options = { options }
53- columns = { 4 }
54- onUpdate = { handleUpdate }
55- className = { b ( ) }
56- />
30+ < div className = { b ( ) } >
31+ { CELL_BG_SWATCHES . map ( ( swatch , index ) => {
32+ const isSelected =
33+ swatch . value === NO_COLOR_VALUE ? ! value : swatch . value === value ;
34+ const isTopRow = index < CELL_BG_SWATCHES . length / 2 ;
35+
36+ return (
37+ < Tooltip
38+ key = { swatch . value }
39+ openDelay = { 200 }
40+ placement = { isTopRow ? 'top' : 'bottom' }
41+ content = { i18n ( swatch . i18nKey ) }
42+ >
43+ < Box className = { b ( 'item' ) } spacing = { { p : 1 } } >
44+ < button
45+ type = "button"
46+ className = { b ( 'swatch' , {
47+ none : swatch . value === NO_COLOR_VALUE ,
48+ color : swatch . value || undefined ,
49+ } ) }
50+ onClick = { ( ) => handleClick ( swatch . value ) }
51+ >
52+ { isSelected && (
53+ < span className = { b ( 'check' ) } >
54+ < Icon data = { Check } size = { 16 } />
55+ </ span >
56+ ) }
57+ </ button >
58+ </ Box >
59+ </ Tooltip >
60+ ) ;
61+ } ) }
62+ </ div >
5763 ) ;
5864} ;
0 commit comments