Skip to content

Commit

Permalink
feat(): add format offer option for bal-input
Browse files Browse the repository at this point in the history
  • Loading branch information
nobilo committed Mar 24, 2022
1 parent e4ebdb6 commit bec0c3e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ export namespace Components {
*/
"inverted": boolean;
/**
* Mask of the input field. It defines what the user can enter and how the format looks like. Currently, only for Switzerland formatted. Formatting for 'contract-number': ' 00/0.000.000' Formatting for 'claim-number': ('73/001217/16.9') //
* Mask of the input field. It defines what the user can enter and how the format looks like. Currently, only for Switzerland formatted. Formatting for 'contract-number': '00/0.000.000' Formatting for 'claim-number': ('73/001217/16.9') Formatting for 'offer-number': ('98/7.654.321') //
*/
"mask"?: 'contract-number' | 'claim-number' | 'offer-number';
/**
Expand Down Expand Up @@ -3199,7 +3199,7 @@ declare namespace LocalJSX {
*/
"inverted"?: boolean;
/**
* Mask of the input field. It defines what the user can enter and how the format looks like. Currently, only for Switzerland formatted. Formatting for 'contract-number': ' 00/0.000.000' Formatting for 'claim-number': ('73/001217/16.9') //
* Mask of the input field. It defines what the user can enter and how the format looks like. Currently, only for Switzerland formatted. Formatting for 'contract-number': '00/0.000.000' Formatting for 'claim-number': ('73/001217/16.9') Formatting for 'offer-number': ('98/7.654.321') //
*/
"mask"?: 'contract-number' | 'claim-number' | 'offer-number';
/**
Expand Down
30 changes: 27 additions & 3 deletions packages/components/src/components/form/bal-input/bal-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ export class Input implements ComponentInterface, FormInput<string | undefined>

/**
* Mask of the input field. It defines what the user can enter and how the format looks like. Currently, only for Switzerland formatted.
* Formatting for 'contract-number': ' 00/0.000.000'
* Formatting for 'contract-number': '00/0.000.000'
* Formatting for 'claim-number': ('73/001217/16.9')
* Formatting for 'offer-number': ('98/7.654.321')
// */
@Prop() mask?: 'contract-number' | 'claim-number' | 'offer-number' = undefined

Expand Down Expand Up @@ -311,6 +312,14 @@ export class Input implements ComponentInterface, FormInput<string | undefined>
input.value = this.formatPolicy(this.inputValue)
break
}
case 'offer-number': {
this.inputValue = input.value.replace(/\D/g, '')
if (this.inputValue.length > MAX_LENGTH_OFFER_NUMBER) {
this.inputValue = this.inputValue.substring(0, MAX_LENGTH_OFFER_NUMBER)
}
input.value = this.formatOffer(this.inputValue)
break
}
case 'claim-number': {
this.inputValue = input.value.replace(/\D/g, '')
if (this.inputValue.length > MAX_LENGTH_CLAIM_NUMBER) {
Expand All @@ -333,7 +342,7 @@ export class Input implements ComponentInterface, FormInput<string | undefined>
* @output 73/001217/16.9
* @private
*/
private formatClaim(value: string) {
private formatClaim(value: string): string {
if (!value) {
return ''
}
Expand Down Expand Up @@ -362,14 +371,28 @@ export class Input implements ComponentInterface, FormInput<string | undefined>
* @output 00/0.000.000
* @private
*/
private formatPolicy(value: string) {
private formatPolicy(value: string): string {
if (!value) {
return ''
}
let newValue = `${value}`.trim()
if (newValue[0] !== '0') {
newValue = `0${value}`
}
return this.formatOffer(newValue)
}

/**
*
* @param value: input number
* @output 98/7.654.321
* @private
*/
private formatOffer(value: string): string {
if (!value) {
return ''
}
const newValue = `${value}`.trim()
const parts = [
newValue.substring(0, 2),
newValue.substring(2, 3),
Expand Down Expand Up @@ -516,4 +539,5 @@ export class Input implements ComponentInterface, FormInput<string | undefined>

let InputIds = 0
const MAX_LENGTH_CONTRACT_NUMBER = 9
const MAX_LENGTH_OFFER_NUMBER = 9
const MAX_LENGTH_CLAIM_NUMBER = 11
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,35 @@ InvalidInput.parameters = {
export const ContractNumberInput = Template.bind({})
ContractNumberInput.args = {
mask: 'contract-number',
placeholder: 'Enter only numbers which will be formatted',
}
ContractNumberInput.parameters = {
...component.sourceCode(ContractNumberInput),
controls: {
exclude: excludedControls,
},
}

export const ClaimNumberInput = Template.bind({})
ClaimNumberInput.args = {
mask: 'claim-number',
placeholder: 'Enter only numbers which will be formatted',
}
ClaimNumberInput.parameters = {
...component.sourceCode(ClaimNumberInput),
controls: {
exclude: excludedControls,
},
}

export const OfferNumberInput = Template.bind({})
OfferNumberInput.args = {
mask: 'offer-number',
placeholder: 'Enter only numbers which will be formatted',
}
OfferNumberInput.parameters = {
...component.sourceCode(OfferNumberInput),
controls: {
exclude: excludedControls,
},
}

0 comments on commit bec0c3e

Please sign in to comment.