-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from ingresse/fixes
Add: 'InputNumeric' and derivated components;
- Loading branch information
Showing
20 changed files
with
683 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,104 @@ | ||
/* Core Packages */ | ||
import React, { memo, forwardRef, useState } from 'react'; | ||
import propTypes from 'prop-types'; | ||
|
||
/* Component Base */ | ||
import InputNumeric from '../InputNumeric/InputNumeric'; | ||
|
||
import { formatCurrency } from '../../../utils'; | ||
|
||
/* Component Itself */ | ||
const InputDiscount = memo(forwardRef(({ | ||
currency, | ||
discountType, | ||
locale, | ||
onBlur, | ||
onFocus, | ||
type, | ||
value, | ||
...props | ||
}, ref) => { | ||
const [ inFocus, setInFocus ] = useState(false); | ||
const isDynamic = (discountType === 'dynamic'); | ||
const isFixed = (discountType === 'fixed'); | ||
const min = ((isDynamic || isFixed) ? 0 : undefined); | ||
const max = (isDynamic ? 100 : undefined); | ||
const currencyMask = formatCurrency(0, locale, currency).replace(/\d|\,|\./g, ''); | ||
const before = ((inFocus && isFixed) ? currencyMask : ''); | ||
const after = ((inFocus && isDynamic) ? '%' : ''); | ||
const customType = (!inFocus ? 'text' : (type || 'number')); | ||
const customValue = ((inFocus || (!isFixed && !isDynamic) || (value === undefined) || (value === '')) ? value : (isFixed ? formatCurrency(value, locale, currency) : (value + '%'))); | ||
|
||
function handleBlur(evt) { | ||
setInFocus(false); | ||
onBlur(evt); | ||
} | ||
|
||
function handleFocus(evt) { | ||
setInFocus(true); | ||
onFocus(evt); | ||
} | ||
|
||
const customProps = { | ||
min, | ||
max, | ||
before, | ||
after, | ||
ref, | ||
type: customType, | ||
value: customValue, | ||
onBlur: handleBlur, | ||
onFocus: handleFocus, | ||
...props, | ||
}; | ||
|
||
return ( | ||
<InputNumeric | ||
{...customProps} | ||
/> | ||
); | ||
})); | ||
|
||
/* Default Properties */ | ||
InputDiscount.defaultProps = { | ||
id: '', | ||
label: '', | ||
onChange: () => {}, | ||
onBlur: () => {}, | ||
onFocus: () => {}, | ||
type: 'number', | ||
styles: {}, | ||
suggestions: [], | ||
value: undefined, | ||
defaultPadding: '10px', | ||
currency: 'BRL', | ||
locale: 'pt-BR', | ||
discountType: 'dynamic', | ||
doubleDecimal: true, | ||
}; | ||
|
||
const valueTypes = propTypes.oneOfType([ | ||
propTypes.number, | ||
propTypes.string, | ||
]); | ||
|
||
/* Properties Types */ | ||
InputDiscount.propTypes = { | ||
id: propTypes.string.isRequired, | ||
label: propTypes.string, | ||
onChange: propTypes.func.isRequired, | ||
type: propTypes.string, | ||
styles: propTypes.oneOfType([ | ||
propTypes.string, | ||
propTypes.object, | ||
]), | ||
suggestions: propTypes.array, | ||
value: valueTypes, | ||
currency: propTypes.string, | ||
locale: propTypes.string, | ||
discountType: propTypes.string, | ||
doubleDecimal: propTypes.bool, | ||
}; | ||
|
||
/* Exporting */ | ||
export default InputDiscount; |
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,95 @@ | ||
--- | ||
name : Form | Input Discount | ||
menu : › Components | ||
route: /components/form/input-discount | ||
--- | ||
|
||
import { useState } from 'react'; | ||
import { Playground, Props } from 'docz'; | ||
import { formatCurrency } from '../../../utils'; | ||
import { | ||
InputDiscount, | ||
Input, | ||
Select, | ||
Container, | ||
Segment, | ||
Row, | ||
Col, | ||
} from '../../'; | ||
|
||
# Input Discount | ||
### InputNumeric with predefined properties to discount's scenery | ||
|
||
|
||
## Example | ||
|
||
<Playground> | ||
{() => { | ||
const [ discount, setDiscount ] = useState(0); | ||
const [ discountType, setDiscountType ] = useState('choose'); | ||
const [ currencyLocale, setCurrencyLocale ] = useState('pt-BR | BRL'); | ||
const splitted = currencyLocale.split(' | '); | ||
const locale = splitted[0]; | ||
const currency = splitted[1]; | ||
const discountFixedPlaceholder = formatCurrency(new Date().getFullYear(), locale, currency); | ||
|
||
console.log('discount', discount); | ||
|
||
return ( | ||
<Container sm> | ||
<Row horizontal="center" vertical="center"> | ||
<Col xs={12} sm={6}> | ||
<Segment padding="10px 0"> | ||
<Select | ||
id="ExampleInputDiscountType" | ||
label="Discount Type" | ||
onChange={({ target }) => { | ||
setDiscountType(target.value); | ||
setDiscount(0); | ||
}} | ||
value={discountType || ''}> | ||
<option value="choose">Select one option</option> | ||
<option value="dynamic">Dynamic (Percentage)</option> | ||
<option value="fixed">Fixed (Monetary/Currency)</option> | ||
</Select> | ||
</Segment> | ||
</Col> | ||
<Col xs={12} sm={6}> | ||
<Segment padding="10px 0"> | ||
<Select | ||
id="ExampleInputDiscountCurrencyLocale" | ||
label="Discount Currency Locale" | ||
onChange={({ target }) => setCurrencyLocale(target.value)} | ||
value={currencyLocale} | ||
disabled={discountType !== 'fixed'}> | ||
<option value="pt-BR | BRL">pt-BR | BRL</option> | ||
<option value="en-US | USD">en-US | USD</option> | ||
<option value="en-EU | EUR">en-EU | EUR</option> | ||
</Select> | ||
</Segment> | ||
</Col> | ||
<Col xs={12} sm={6}> | ||
<Segment padding="10px 0"> | ||
<InputDiscount | ||
id="ExampleInputDiscount" | ||
label="Discount Amount" | ||
onChange={(e, newValue) => setDiscount(newValue)} | ||
value={discount || ''} | ||
disabled={discountType === 'choose'} | ||
placeholder={`Ex: ${(discountType === 'fixed') ? discountFixedPlaceholder : '15%'}`} | ||
discountType={discountType} | ||
locale={locale} | ||
currency={currency} | ||
/> | ||
</Segment> | ||
</Col> | ||
</Row> | ||
</Container> | ||
); | ||
}} | ||
</Playground> | ||
|
||
|
||
## Properties | ||
|
||
<Props of={InputDiscount} /> |
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.