Skip to content

Commit

Permalink
Merge pull request #22 from ingresse/fixes
Browse files Browse the repository at this point in the history
Add: 'InputNumeric' and derivated components;
  • Loading branch information
udimberto authored Aug 5, 2020
2 parents ad82d09 + cc939eb commit 6fd01f2
Show file tree
Hide file tree
Showing 20 changed files with 683 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ingresse/aphrodite",
"version": "2.1.1",
"version": "2.1.5",
"private": false,
"license": "MIT",
"description": "React UI library from ingresse.com",
Expand Down
7 changes: 5 additions & 2 deletions src/components/Form/FormControlButtonStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import styled from '@emotion/styled';

/* Framework Definitions */
import { colors } from '../../utils';
import { colors, text } from '../../utils';

/* Component Styles */
const AphFormControlButtonStyled = styled.button`
Expand All @@ -24,17 +24,20 @@ const AphFormControlButtonStyled = styled.button`
background: transparent;
text-align: center;
transition: all ease 0.3s;
&:active,
&:focus,
&:hover {
color: ${props => colors.getFromTheme(props, 'base')};
color: ${props => props.as ? null : colors.getFromTheme(props, 'base')};
border : 0;
outline : 0;
background: transparent;
}
${props => text(props, props.textSize)};
${props => (props.left ? 'left' : 'right')}: 5px;
${props => props.styles};
Expand Down
13 changes: 9 additions & 4 deletions src/components/Form/FormControlStyled.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const FormControlStyled = styled.input`
width : 100%;
margin : 0;
min-height: 50px;
padding : ${({ hasLabel }) => hasLabel ? '20px 10px 0' : '10px'};
padding-top : ${({ hasLabel, defaultPadding }) => hasLabel ? '20px' : defaultPadding || '10px'};
padding-bottom: ${({ hasLabel, defaultPadding }) => hasLabel ? '0' : defaultPadding || '10px'};
padding-right : ${({ hasAfter, defaultPadding }) => hasAfter ? '40px' : defaultPadding || '10px'};
padding-left : ${({ hasBefore, defaultPadding }) => hasBefore ? '40px' : defaultPadding || '10px'};
font-weight: ${SIZES.MD.FONT_WEIGHT};
font-size : ${SIZES.MD.FONT_SIZE};
Expand All @@ -29,9 +33,9 @@ const FormControlStyled = styled.input`
border : 0;
outline: 0;
transition-property : color, padding;
transition-property : color, padding-top, padding-bottom, padding-right;
transition-timing-function: linear;
transition-duration : 0.15s;
transition-duration : 0.3s;
will-change: color, padding;
Expand Down Expand Up @@ -84,7 +88,8 @@ const FormControlStyled = styled.input`
}
&.aph-form-control--middle {
padding: 10px;
padding-top: ${({ defaultPadding }) => defaultPadding};
padding-bottom: ${({ defaultPadding }) => defaultPadding};
}
${props => !props.error ? null : `
Expand Down
3 changes: 2 additions & 1 deletion src/components/Form/FormControlWrapperStyled.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const AphFormControlWrapperStyled = styled.div`
display : block;
.aph-form-control {
padding: ${({ hasLabel }) => !hasLabel ? null : '20px 10px 0'};
padding-top: ${({ hasLabel, defaultPadding }) => !hasLabel ? null : '20px'};
padding-bottom: ${({ hasLabel, defaultPadding }) => !hasLabel ? null : '0'};
}
${props => !props.hasError ? null : `
Expand Down
13 changes: 7 additions & 6 deletions src/components/Form/Input/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,19 @@ const Input = memo(forwardRef((props, ref) => {

/* Default Properties */
Input.defaultProps = {
id : `formControlRandomID${Math.random()}`,
label : '',
btn : null,
id: `formControlRandomID${Math.random()}`,
label: '',
btn: null,
button: null,
styles: {},
defaultPadding: '10px',
};

/* Properties Types */
Input.propTypes = {
id : propTypes.string.isRequired,
label : propTypes.string,
btn : propTypes.object,
id: propTypes.string.isRequired,
label: propTypes.string,
btn: propTypes.object,
button: propTypes.object,
styles: propTypes.oneOfType([
propTypes.string,
Expand Down
40 changes: 19 additions & 21 deletions src/components/Form/InputCurrency/InputCurrency.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,39 +84,37 @@ const InputNumber = memo(forwardRef((props, ref) => {

/* Default Properties */
InputNumber.defaultProps = {
id : '',
label : '',
btn : null,
id: '',
label: '',
btn: null,
styles: {},

inputType : 'tel',
inputType: 'tel',
thousandSeparator: '.',
decimalSeparator : ',',
prefix : 'R$ ',
value : 0,
precision : 2,
allowEmpty : true,
allowNegative : false,
prefix: 'R$ ',
value: 0,
precision: 2,
allowEmpty: true,
allowNegative: false,
};

/* Properties Types */
InputNumber.propTypes = {
id : propTypes.string.isRequired,
label : propTypes.string,
btn : propTypes.object,
type : propTypes.string,
id: propTypes.string.isRequired,
label: propTypes.string,
btn: propTypes.object,
type: propTypes.string,
styles: propTypes.oneOfType([
propTypes.string,
propTypes.object,
]),

thousandSeparator: propTypes.string,
decimalSeparator : propTypes.string,
prefix : propTypes.string,
value : propTypes.number,
precision : propTypes.number,
allowEmpty : propTypes.bool,
allowNegative : propTypes.bool,
decimalSeparator: propTypes.string,
prefix: propTypes.string,
value: propTypes.number,
precision: propTypes.number,
allowEmpty: propTypes.bool,
allowNegative: propTypes.bool,
};

/* Exporting */
Expand Down
104 changes: 104 additions & 0 deletions src/components/Form/InputDiscount/InputDiscount.jsx
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;
95 changes: 95 additions & 0 deletions src/components/Form/InputDiscount/InputDiscount.mdx
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} />
1 change: 1 addition & 0 deletions src/components/Form/InputMask/InputMask.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function InputMask(props) {
/* Default props */
InputMask.defaultProps = {
mask: '',
defaultPadding: '10px',
};

/* Properties types */
Expand Down
Loading

0 comments on commit 6fd01f2

Please sign in to comment.