1- import { TableColumn , TableDataType } from "cdm/FolderModel" ;
21import {
3- ActionTypes ,
4- DataTypes ,
5- StyleVariables ,
6- WidthVariables ,
7- } from "helpers/Constants" ;
8- import { dbTrim , recalculateColumnWidth } from "helpers/StylesHelper" ;
2+ DatabaseHeaderProps ,
3+ TableColumn ,
4+ TableDataType ,
5+ } from "cdm/FolderModel" ;
6+ import { ActionTypes , DataTypes , StyleVariables } from "helpers/Constants" ;
7+ import { dbTrim } from "helpers/StylesHelper" ;
98import ArrowUpIcon from "components/img/ArrowUp" ;
109import ArrowDownIcon from "components/img/ArrowDown" ;
1110import ArrowLeftIcon from "components/img/ArrowLeft" ;
@@ -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, position } = 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 ( position - 1 ) ,
116+ columnInfo : adjustWidthOfTheColumn ( 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 ( position + 1 ) ,
129+ columnInfo : adjustWidthOfTheColumn ( column . position + 1 ) ,
133130 } ) ;
134131 setExpanded ( false ) ;
135132 } ,
@@ -140,11 +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- delete columnWidthState . widthRecord [ id ] ;
144+ delete columnWidthState . widthRecord [ column . id ] ;
148145 setColumnWidthState ( columnWidthState ) ;
149146 } ,
150147 icon : < TrashIcon /> ,
@@ -160,7 +157,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
160157 onClick : ( e : any ) => {
161158 dispatch ( {
162159 type : ActionTypes . UPDATE_COLUMN_TYPE ,
163- columnId : id ,
160+ columnId : column . id ,
164161 dataType : DataTypes . SELECT ,
165162 } ) ;
166163 setShowType ( false ) ;
@@ -173,7 +170,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
173170 onClick : ( e : any ) => {
174171 dispatch ( {
175172 type : ActionTypes . UPDATE_COLUMN_TYPE ,
176- columnId : id ,
173+ columnId : column . id ,
177174 dataType : DataTypes . TEXT ,
178175 } ) ;
179176 setShowType ( false ) ;
@@ -186,7 +183,7 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
186183 onClick : ( e : any ) => {
187184 dispatch ( {
188185 type : ActionTypes . UPDATE_COLUMN_TYPE ,
189- columnId : id ,
186+ columnId : column . id ,
190187 dataType : DataTypes . NUMBER ,
191188 } ) ;
192189 setShowType ( false ) ;
@@ -212,15 +209,24 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
212209 ) ;
213210
214211 function persistLabelChange ( ) {
212+ // trim label will get a valid yaml key
213+ const newKey = dbTrim ( labelState ) ;
215214 dispatch ( {
216215 type : ActionTypes . UPDATE_COLUMN_LABEL ,
217- columnId : id ,
218- accessor : keyState ,
216+ columnId : column . id ,
217+ accessor : newKey ,
218+ newKey : newKey ,
219219 label : labelState ,
220- columnInfo : adjustWidthOfTheColumn ( position ) ,
221220 } ) ;
222221 setExpanded ( false ) ;
223- 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 ) ;
224230 }
225231
226232 function handleKeyDown ( e : any ) {
@@ -246,17 +252,20 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
246252 initialState . columns . length - initialState . shadowColumns . length ;
247253 const columnName = `newColumn${ columnNumber } ` ;
248254 const columnLabel = `New Column ${ columnNumber } ` ;
249- setColumnWidthState (
250- recalculateColumnWidth ( columnWidthState , columnName , columnLabel )
255+ columnWidthState . widthRecord [ columnName ] = getColumnWidthStyle (
256+ rows ,
257+ columnName ,
258+ columnLabel
251259 ) ;
260+ setColumnWidthState ( columnWidthState ) ;
252261 return { name : columnName , position : wantedPosition , label : columnLabel } ;
253262 }
254263
255264 function handleChangeToggleInlineFrontmatter ( e : any ) {
256265 setIsInline ( e . target . checked ) ;
257266 dispatch ( {
258267 type : ActionTypes . TOGGLE_INLINE_FRONTMATTER ,
259- columnId : id ,
268+ columnId : column . id ,
260269 isInline : e . target . checked ,
261270 } ) ;
262271 }
@@ -321,7 +330,9 @@ const HeaderMenu = (headerMenuProps: HeaderMenuProps) => {
321330 < span className = "svg-icon svg-text icon-margin" >
322331 { propertyIcon }
323332 </ span >
324- < span style = { { textTransform : "capitalize" } } > { dataType } </ span >
333+ < span style = { { textTransform : "capitalize" } } >
334+ { column . dataType }
335+ </ span >
325336 </ button >
326337 { showType && (
327338 < div
0 commit comments