1- import { TableColumn , TableDataType } from "cdm/FolderModel" ;
21import {
3- ActionTypes ,
4- DataTypes ,
5- StyleVariables ,
6- WidthVariables ,
7- } from "helpers/Constants" ;
2+ DatabaseHeaderProps ,
3+ TableColumn ,
4+ TableDataType ,
5+ } from "cdm/FolderModel" ;
6+ import { ActionTypes , DataTypes , StyleVariables } from "helpers/Constants" ;
87import { dbTrim } from "helpers/StylesHelper" ;
98import ArrowUpIcon from "components/img/ArrowUp" ;
109import ArrowDownIcon from "components/img/ArrowDown" ;
@@ -20,25 +19,25 @@ import { ActionType } from "react-table";
2019import { usePopper } from "react-popper" ;
2120import { HeaderContext } from "components/contexts/HeaderContext" ;
2221import { FormControlLabel , FormGroup , Switch } from "@material-ui/core" ;
22+ import { getColumnWidthStyle } from "./styles/ColumnWidthStyle" ;
2323type HeaderMenuProps = {
24- dispatch : ( action : ActionType ) => void ;
24+ headerProps : DatabaseHeaderProps ;
2525 setSortBy : any ;
26- column : TableColumn ;
27- columns : TableColumn [ ] ;
2826 propertyIcon : any ;
2927 expanded : boolean ;
3028 setExpanded : ( expanded : boolean ) => void ;
3129 created : boolean ;
3230 referenceElement : any ;
3331 labelState : string ;
3432 setLabelState : ( label : string ) => void ;
35- initialState : TableDataType ;
3633 isInline : boolean ;
3734 setIsInline : ( isInline : boolean ) => void ;
3835} ;
3936const HeaderMenu = ( headerMenuProps : HeaderMenuProps ) => {
37+ /** state of width columns */
38+ const { columnWidthState, setColumnWidthState } = useContext ( HeaderContext ) ;
39+ /** Header props */
4040 const {
41- dispatch,
4241 setSortBy,
4342 propertyIcon,
4443 expanded,
@@ -47,15 +46,13 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
4746 referenceElement,
4847 labelState,
4948 setLabelState,
50- initialState,
5149 isInline,
5250 setIsInline,
5351 } = headerMenuProps ;
54- /** state of width columns */
55- const { columnWidthState , setColumnWidthState } = useContext ( HeaderContext ) ;
52+ const { column , columns, rows , initialState } = headerMenuProps . headerProps ;
53+ const dispatch = ( headerMenuProps . headerProps as any ) . dataDispatch ;
5654 /** Column values */
57- const { id, key, dataType } = headerMenuProps . column ;
58- const [ keyState , setkeyState ] = useState ( dbTrim ( key ) ) ;
55+ const [ keyState , setkeyState ] = useState ( dbTrim ( column . key ) ) ;
5956 const [ popperElement , setPopperElement ] = useState ( null ) ;
6057 const [ inputRef , setInputRef ] = useState ( null ) ;
6158 const { styles, attributes } = usePopper ( referenceElement , popperElement , {
@@ -96,15 +93,15 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
9693 const buttons = [
9794 {
9895 onClick : ( e : any ) => {
99- setSortBy ( [ { id : id , desc : false } ] ) ;
96+ setSortBy ( [ { id : column . id , desc : false } ] ) ;
10097 setExpanded ( false ) ;
10198 } ,
10299 icon : < ArrowUpIcon /> ,
103100 label : "Sort ascending" ,
104101 } ,
105102 {
106103 onClick : ( e : any ) => {
107- setSortBy ( [ { id : id , desc : true } ] ) ;
104+ setSortBy ( [ { id : column . id , desc : true } ] ) ;
108105 setExpanded ( false ) ;
109106 } ,
110107 icon : < ArrowDownIcon /> ,
@@ -114,9 +111,9 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
114111 onClick : ( e : any ) => {
115112 dispatch ( {
116113 type : ActionTypes . ADD_COLUMN_TO_LEFT ,
117- columnId : id ,
114+ columnId : column . id ,
118115 focus : false ,
119- columnInfo : adjustWidthOfTheColumn ( ) ,
116+ columnInfo : adjustWidthOfTheColumnsWhenAdd ( column . position - 1 ) ,
120117 } ) ;
121118 setExpanded ( false ) ;
122119 } ,
@@ -127,9 +124,9 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
127124 onClick : ( e : any ) => {
128125 dispatch ( {
129126 type : ActionTypes . ADD_COLUMN_TO_RIGHT ,
130- columnId : id ,
127+ columnId : column . id ,
131128 focus : false ,
132- columnInfo : adjustWidthOfTheColumn ( ) ,
129+ columnInfo : adjustWidthOfTheColumnsWhenAdd ( column . position + 1 ) ,
133130 } ) ;
134131 setExpanded ( false ) ;
135132 } ,
@@ -140,14 +137,11 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
140137 onClick : ( e : any ) => {
141138 dispatch ( {
142139 type : ActionTypes . DELETE_COLUMN ,
143- columnId : id ,
140+ columnId : column . id ,
144141 key : keyState ,
145142 } ) ;
146143 setExpanded ( false ) ;
147- // Adjust the width of the columns
148- columnWidthState . totalWidth =
149- columnWidthState . totalWidth - columnWidthState . widthRecord [ id ] ;
150- delete columnWidthState . widthRecord [ id ] ;
144+ delete columnWidthState . widthRecord [ column . id ] ;
151145 setColumnWidthState ( columnWidthState ) ;
152146 } ,
153147 icon : < TrashIcon /> ,
@@ -163,7 +157,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
163157 onClick : ( e : any ) => {
164158 dispatch ( {
165159 type : ActionTypes . UPDATE_COLUMN_TYPE ,
166- columnId : id ,
160+ columnId : column . id ,
167161 dataType : DataTypes . SELECT ,
168162 } ) ;
169163 setShowType ( false ) ;
@@ -176,7 +170,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
176170 onClick : ( e : any ) => {
177171 dispatch ( {
178172 type : ActionTypes . UPDATE_COLUMN_TYPE ,
179- columnId : id ,
173+ columnId : column . id ,
180174 dataType : DataTypes . TEXT ,
181175 } ) ;
182176 setShowType ( false ) ;
@@ -189,7 +183,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
189183 onClick : ( e : any ) => {
190184 dispatch ( {
191185 type : ActionTypes . UPDATE_COLUMN_TYPE ,
192- columnId : id ,
186+ columnId : column . id ,
193187 dataType : DataTypes . NUMBER ,
194188 } ) ;
195189 setShowType ( false ) ;
@@ -209,20 +203,30 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
209203 settingsReferenceElement ,
210204 settingsPopperElement ,
211205 {
212- placement : "right " ,
206+ placement : "auto " ,
213207 strategy : "fixed" ,
214208 }
215209 ) ;
216210
217211 function persistLabelChange ( ) {
212+ // trim label will get a valid yaml key
213+ const newKey = dbTrim ( labelState ) ;
218214 dispatch ( {
219215 type : ActionTypes . UPDATE_COLUMN_LABEL ,
220- columnId : id ,
221- accessor : keyState ,
216+ columnId : column . id ,
217+ accessor : newKey ,
218+ newKey : newKey ,
222219 label : labelState ,
223220 } ) ;
224221 setExpanded ( false ) ;
225- setkeyState ( dbTrim ( labelState ) ) ;
222+ setkeyState ( newKey ) ;
223+ columnWidthState . widthRecord [ newKey ] = getColumnWidthStyle (
224+ rows ,
225+ newKey ,
226+ labelState
227+ ) ;
228+ delete columnWidthState . widthRecord [ column . id ] ;
229+ setColumnWidthState ( columnWidthState ) ;
226230 }
227231
228232 function handleKeyDown ( e : any ) {
@@ -243,29 +247,25 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
243247 e . preventDefault ( ) ;
244248 }
245249
246- function adjustWidthOfTheColumn ( ) {
250+ function adjustWidthOfTheColumnsWhenAdd ( wantedPosition : number ) {
247251 const columnNumber =
248- initialState . columns . length + 1 - initialState . shadowColumns . length ;
252+ initialState . columns . length - initialState . shadowColumns . length ;
249253 const columnName = `newColumn${ columnNumber } ` ;
250254 const columnLabel = `New Column ${ columnNumber } ` ;
251- // Add width of the new column
252- columnWidthState . widthRecord [ columnName ] =
253- ( columnLabel . length + WidthVariables . ICON_SPACING ) *
254- WidthVariables . MAGIC_SPACING ;
255- // Add new width to the total width
256- columnWidthState . totalWidth =
257- columnWidthState . totalWidth +
258- ( columnLabel . length + WidthVariables . ICON_SPACING ) *
259- WidthVariables . MAGIC_SPACING ;
255+ columnWidthState . widthRecord [ columnName ] = getColumnWidthStyle (
256+ rows ,
257+ columnName ,
258+ columnLabel
259+ ) ;
260260 setColumnWidthState ( columnWidthState ) ;
261- return { name : columnName , position : columnNumber , label : columnLabel } ;
261+ return { name : columnName , position : wantedPosition , label : columnLabel } ;
262262 }
263263
264264 function handleChangeToggleInlineFrontmatter ( e : any ) {
265265 setIsInline ( e . target . checked ) ;
266266 dispatch ( {
267267 type : ActionTypes . TOGGLE_INLINE_FRONTMATTER ,
268- columnId : id ,
268+ columnId : column . id ,
269269 isInline : e . target . checked ,
270270 } ) ;
271271 }
@@ -330,7 +330,9 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
330330 < span className = "svg-icon svg-text icon-margin" >
331331 { propertyIcon }
332332 </ span >
333- < span style = { { textTransform : "capitalize" } } > { dataType } </ span >
333+ < span style = { { textTransform : "capitalize" } } >
334+ { column . dataType }
335+ </ span >
334336 </ button >
335337 { showType && (
336338 < div
0 commit comments