mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-09 09:58:20 -05:00
Fix: frontend validation of number fields fails upon save (#5646)
This commit is contained in:
parent
6b34f592df
commit
45e2b7f814
@ -47,22 +47,25 @@ describe('NumberComponent', () => {
|
|||||||
expect(component.value).toEqual(1002)
|
expect(component.value).toEqual(1002)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support float & monetary values', () => {
|
it('should support float, monetary values & scientific notation', () => {
|
||||||
component.writeValue(11.13)
|
const mockFn = jest.fn()
|
||||||
expect(component.value).toEqual(11)
|
component.registerOnChange(mockFn)
|
||||||
|
|
||||||
|
component.step = 1
|
||||||
|
component.onChange(11.13)
|
||||||
|
expect(mockFn).toHaveBeenCalledWith(11)
|
||||||
|
|
||||||
|
component.onChange(1.23456789e8)
|
||||||
|
expect(mockFn).toHaveBeenCalledWith(123456789)
|
||||||
|
|
||||||
|
component.step = 0.01
|
||||||
|
component.onChange(11.1)
|
||||||
|
expect(mockFn).toHaveBeenCalledWith('11.10')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should display monetary values fixed to 2 decimals', () => {
|
||||||
component.step = 0.01
|
component.step = 0.01
|
||||||
component.writeValue(11.1)
|
component.writeValue(11.1)
|
||||||
expect(component.value).toEqual('11.10')
|
expect(component.value).toEqual('11.10')
|
||||||
component.step = 0.1
|
|
||||||
component.writeValue(12.3456)
|
|
||||||
expect(component.value).toEqual(12.3456)
|
|
||||||
// float (step = .1) doesn't force 2 decimals
|
|
||||||
component.writeValue(11.1)
|
|
||||||
expect(component.value).toEqual(11.1)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should support scientific notation', () => {
|
|
||||||
component.writeValue(1.23456789e8)
|
|
||||||
expect(component.value).toEqual(123456789)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -36,9 +36,18 @@ export class NumberComponent extends AbstractInputComponent<number> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerOnChange(fn: any): void {
|
||||||
|
this.onChange = (newValue: any) => {
|
||||||
|
// number validation
|
||||||
|
if (this.step === 1 && newValue?.toString().indexOf('e') === -1)
|
||||||
|
newValue = parseInt(newValue, 10)
|
||||||
|
if (this.step === 0.01) newValue = parseFloat(newValue).toFixed(2)
|
||||||
|
fn(newValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
writeValue(newValue: any): void {
|
writeValue(newValue: any): void {
|
||||||
if (this.step === 1 && newValue?.toString().indexOf('e') === -1)
|
// Allow monetary values to be displayed with 2 decimals
|
||||||
newValue = parseInt(newValue, 10)
|
|
||||||
if (this.step === 0.01) newValue = parseFloat(newValue).toFixed(2)
|
if (this.step === 0.01) newValue = parseFloat(newValue).toFixed(2)
|
||||||
super.writeValue(newValue)
|
super.writeValue(newValue)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user