-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reactify number #1174
Merged
Merged
Reactify number #1174
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ae4ca82
Use Aksel text as number component and improve validation and localiz…
lotorvik 989cf87
Remove console logs
lotorvik 92fcf4d
Empty number do not trigger validation
lotorvik d0ccd47
Only two decimals
lotorvik 1fcf9fa
Update texts
lotorvik 1e0fca7
Fixes after PR
lotorvik c9e1f0a
Fixes after PR
lotorvik 7e50dd7
Remove test for code not in use
lotorvik 2b44422
Remove test for code not in use
lotorvik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/shared-components/src/formio/components/base/editForm/display/editFormInputType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Component } from '@navikt/skjemadigitalisering-shared-domain'; | ||
|
||
const editFormInputType = (): Component => { | ||
return { | ||
type: 'select', | ||
label: 'Input type', | ||
key: 'inputType', | ||
dataSrc: 'values', | ||
data: { | ||
values: [ | ||
{ label: 'Desimaltall', value: 'decimal' }, | ||
{ label: 'Heltall', value: 'numeric' }, | ||
], | ||
}, | ||
}; | ||
}; | ||
|
||
export default editFormInputType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/shared-components/src/formio/components/extensions/email/Email.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 69 additions & 6 deletions
75
packages/shared-components/src/formio/components/extensions/number/Number.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,84 @@ | ||
import FormioNumber from 'formiojs/components/number/Number'; | ||
import { InputMode, numberUtils, TEXTS } from '@navikt/skjemadigitalisering-shared-domain'; | ||
import BaseComponent from '../../base/BaseComponent'; | ||
import TextField from '../../core/textfield/TextField'; | ||
import numberForm from './Number.form'; | ||
|
||
class Number extends FormioNumber { | ||
// TODO: Try and see if can get delimiter (tusenskilletegn) default set for all numbers. | ||
class Number extends TextField { | ||
static schema() { | ||
return FormioNumber.schema({ | ||
return BaseComponent.schema({ | ||
label: 'Tall', | ||
type: 'number', | ||
key: 'number', | ||
fieldSize: 'input--m', | ||
key: 'tall', | ||
spellcheck: false, | ||
inputType: 'decimal', | ||
}); | ||
} | ||
|
||
static editForm() { | ||
return numberForm(); | ||
} | ||
|
||
getInputMode(): InputMode { | ||
return this.component?.inputType || 'decimal'; | ||
} | ||
|
||
getMinValue() { | ||
return this.component?.validate?.min; | ||
} | ||
|
||
getMaxValue() { | ||
return this.component?.validate?.max; | ||
} | ||
|
||
checkComponentValidity(data, dirty, row, options = {}) { | ||
const validity = super.checkComponentValidity(data, dirty, row, options); | ||
|
||
if (validity) { | ||
const errorMessage = this.validateNumber(); | ||
if (errorMessage) { | ||
return this.setComponentValidity([this.createError(errorMessage, undefined)], dirty, undefined); | ||
} | ||
} | ||
|
||
return validity; | ||
} | ||
|
||
validateNumber() { | ||
if (this.getValue() === '' || this.getValue() === undefined) { | ||
return; | ||
} | ||
|
||
if (this.getInputMode() === 'decimal') { | ||
if (!numberUtils.isValidDecimal(this.getValue())) return this.translateWithLabel(TEXTS.validering.decimal); | ||
} else if (this.getInputMode() === 'numeric') { | ||
if (!numberUtils.isValidInteger(this.getValue())) return this.translateWithLabel(TEXTS.validering.integer); | ||
} | ||
|
||
if (!numberUtils.isBiggerOrEqualMin(this.getValue(), this.getMinValue())) { | ||
return this.translateWithLabel(TEXTS.validering.min, { min: numberUtils.toLocaleString(this.getMinValue()) }); | ||
} | ||
|
||
if (!numberUtils.isSmallerOrEqualMax(this.getValue(), this.getMaxValue())) { | ||
return this.translateWithLabel(TEXTS.validering.max, { max: numberUtils.toLocaleString(this.getMaxValue()) }); | ||
} | ||
} | ||
|
||
translateWithLabel(key: string, options = {}) { | ||
return this.t(key, { field: this.getLabel({ labelTextOnly: true }), ...options }); | ||
} | ||
|
||
handleChange(value: string): any { | ||
return super.handleChange(this.replaceCommasAndSpaces(value)); | ||
} | ||
|
||
setValue(value: string): any { | ||
// Need to format the number if it is saved or if you come back from summary page. | ||
super.setValue(numberUtils.toLocaleString(value)); | ||
} | ||
|
||
replaceCommasAndSpaces(value: string) { | ||
return value?.replace(/,/g, '.').replace(/\s/g, ''); | ||
} | ||
} | ||
|
||
export default Number; |
2 changes: 1 addition & 1 deletion
2
packages/shared-components/src/formio/template/templates/navdesign/field/form.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sjekk any