1
- import * as widgets from '@jupyter-widgets/base' ;
1
+ import * as widgets from '@jupyter-widgets/base' ;
2
+ import * as d3 from 'd3-format' ;
2
3
import { cloneDeep , extend , includes as contains , each , debounce , times , map , unzip as transpose } from 'lodash' ;
3
4
import { semver_range } from './version' ;
4
5
import { RendererModel } from './renderer' ;
@@ -143,8 +144,8 @@ let SheetModel = widgets.DOMWidgetModel.extend({
143
144
cell_data . options [ 'readOnly' ] = cell . get ( 'read_only' ) ;
144
145
if ( cell . get ( 'choice' ) != null )
145
146
cell_data . options [ 'source' ] = cell . get ( 'choice' )
146
- if ( cell . get ( 'numeric_format' ) && cell . get ( 'type' ) == 'numeric' )
147
- cell_data . options [ 'numericFormat' ] = { 'pattern' : cell . get ( 'numeric_format' ) } ;
147
+ if ( cell . get ( 'numeric_format' ) && ( cell . get ( 'type' ) == 'int' || cell . get ( 'type' ) == 'float' ) )
148
+ cell_data . options [ 'numericFormat' ] = cell . get ( 'numeric_format' ) ;
148
149
if ( cell . get ( 'date_format' ) && cell . get ( 'type' ) == 'date' ) {
149
150
cell_data . options [ 'correctFormat' ] = true ;
150
151
cell_data . options [ 'dateFormat' ] = cell . get ( 'date_format' ) || cell_data . options [ 'dateFormat' ] ;
@@ -262,7 +263,7 @@ let put_values2d = function(grid, values) {
262
263
}
263
264
} ;
264
265
265
- // calls the original renderer and then applies custom styling
266
+ // Custom styled renderer that applies the default renderer then apply the given style on the cell
266
267
( Handsontable . renderers as any ) . registerRenderer ( 'styled' , function customRenderer ( hotInstance , td , row , column , prop , value , cellProperties ) {
267
268
let name = cellProperties . original_renderer || cellProperties . type || 'text' ;
268
269
let original_renderer = ( Handsontable . renderers as any ) . getRenderer ( name ) ;
@@ -272,6 +273,34 @@ let put_values2d = function(grid, values) {
272
273
} ) ;
273
274
} ) ;
274
275
276
+
277
+ // Register `int` cell type
278
+ let int_editor = Handsontable . editors . TextEditor . prototype . extend ( ) ;
279
+
280
+ function int_renderer ( hotInstance , td , row , column , prop , value , cellProperties ) {
281
+ const numeric_format = cellProperties . numericFormat || 'd' ;
282
+ td . innerHTML = d3 . format ( numeric_format ) ( value ) ;
283
+ }
284
+
285
+ function int_validator ( query , callback ) {
286
+ let is_integer = false ;
287
+ if ( typeof query == 'number' ) {
288
+ is_integer = Number . isInteger ( query ) ;
289
+ } else {
290
+ is_integer = Number . isInteger ( parseFloat ( query ) ) ;
291
+ }
292
+
293
+ console . log ( 'is_integer?' , query , is_integer ) ;
294
+ callback ( is_integer ) ;
295
+ }
296
+
297
+ ( Handsontable . cellTypes as any ) . registerCellType ( 'int' , {
298
+ editor : int_editor ,
299
+ renderer : int_renderer ,
300
+ validator : int_validator ,
301
+ allowInvalid : false
302
+ } ) ;
303
+
275
304
let SheetView = widgets . DOMWidgetView . extend ( {
276
305
render : function ( ) {
277
306
// this.widget_view_promises = {}
0 commit comments