@dilicorp/ui 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ declare type Props = {
3
+ name: string;
4
+ attrs: {
5
+ [key: string]: any;
6
+ };
7
+ handleChange: (params: any) => void;
8
+ max?: number | string;
9
+ decimals?: number;
10
+ };
11
+ export declare const InputPercentage: ({ attrs, handleChange, max, decimals, name }: Props) => JSX.Element;
12
+ export {};
13
+ //# sourceMappingURL=input-percentage.d.ts.map
@@ -0,0 +1,22 @@
1
+ import { FastField, Field } from 'formik';
2
+ import React from 'react';
3
+ import numberHelper from '../../../utils/number-helper';
4
+ export const InputPercentage = ({ attrs, handleChange, max = 100, decimals = 2, name }) => {
5
+ const { maxLength = 8 } = attrs;
6
+ const handleChangePercentage = (event) => {
7
+ const { value } = event.currentTarget;
8
+ const percentage = numberHelper.percentageSanitize(value, decimals);
9
+ if (typeof max === 'number' && max < Number(percentage)) {
10
+ return;
11
+ }
12
+ handleChange({
13
+ currentTarget: {
14
+ name,
15
+ value: percentage ? `${percentage}%` : ''
16
+ }
17
+ });
18
+ };
19
+ const { fastField = true, ...restAttrs } = attrs;
20
+ const Tag = fastField ? FastField : Field;
21
+ return (React.createElement(Tag, { name: name }, ({ field }) => (React.createElement("input", { ...field, max: max, ...restAttrs, onChange: handleChangePercentage, min: "0", maxLength: maxLength }))));
22
+ };
@@ -3,7 +3,7 @@ import { FormikProps } from 'formik';
3
3
  import { FormBuilderBasicsItem } from '../form-builder.types';
4
4
  export interface FormBuildItemInput extends Omit<FormBuilderBasicsItem, 'prefix'> {
5
5
  component?: 'input';
6
- type?: 'text' | 'button' | 'checkbox' | 'email' | 'file' | 'hidden' | 'number' | 'submit' | 'reset' | 'color';
6
+ type?: 'text' | 'button' | 'checkbox' | 'email' | 'file' | 'hidden' | 'number' | 'submit' | 'reset' | 'color' | 'percentage';
7
7
  mask?: string | Array<string | RegExp>;
8
8
  multiple?: boolean;
9
9
  currency?: {
@@ -13,6 +13,7 @@ export interface FormBuildItemInput extends Omit<FormBuilderBasicsItem, 'prefix'
13
13
  suffix?: string | React.ReactNode;
14
14
  prefix?: string | React.ReactNode;
15
15
  fastField?: boolean;
16
+ decimals?: number;
16
17
  }
17
18
  declare type Props = Omit<FormBuildItemInput, 'component'> & {
18
19
  actions: FormikProps<any>;
@@ -4,8 +4,9 @@ import classNames from 'classnames';
4
4
  import { InputCurrency } from './input-currency';
5
5
  import { InputMask } from './input-mask';
6
6
  import { InputPrefixSuffix } from './input-prefix-suffix';
7
+ import { InputPercentage } from './input-percentage';
7
8
  export default function Input(props) {
8
- const { name, id = name, type = 'text', tabIndex = 0, actions, mask, multiple = false, currency, className, prefix, suffix, fastField = true, ...elements } = props;
9
+ const { name, id = name, type = 'text', tabIndex = 0, actions, mask, multiple = false, currency, className, prefix, suffix, fastField = true, decimals = 2, ...elements } = props;
9
10
  const validOptions = {
10
11
  alt: elements.alt,
11
12
  autoComplete: elements.autoComplete,
@@ -71,12 +72,15 @@ export default function Input(props) {
71
72
  ...attrsInput,
72
73
  fastField
73
74
  };
75
+ if (type === 'percentage') {
76
+ return React.createElement(InputPercentage, { ...{ name, attrs, currency, handleChange, decimals, max: elements.max } });
77
+ }
74
78
  if (currency)
75
79
  return React.createElement(InputCurrency, { ...{ name, attrs, currency, handleChange } });
76
80
  if (mask)
77
81
  return React.createElement(InputMask, { ...{ name, attrs, mask } });
78
82
  if (prefix || suffix)
79
- return React.createElement(InputPrefixSuffix, { ...{ attrs, suffix, prefix } });
83
+ return React.createElement(InputPrefixSuffix, { ...{ attrs, suffix, prefix, handleChange } });
80
84
  const Tag = fastField ? FastField : Field;
81
85
  return (React.createElement(Tag, { ...attrsInput, as: multiple ? 'textarea' : 'input', readOnly: true, onFocus: (event) => {
82
86
  event.currentTarget.readOnly = false;
@@ -3,6 +3,7 @@ declare class NumberHelper {
3
3
  sanitize(string: string): string;
4
4
  currency(value?: number, locale?: string, format?: string): string;
5
5
  currencySanitize(value: string): string | null;
6
+ percentageSanitize(value: string, decimals?: number): string | null;
6
7
  }
7
8
  declare const _default: NumberHelper;
8
9
  export default _default;
@@ -21,5 +21,16 @@ class NumberHelper {
21
21
  return `${integer}.0${decimal}`;
22
22
  return `${integer}.${decimal}`;
23
23
  }
24
+ percentageSanitize(value, decimals = 2) {
25
+ var _a;
26
+ const sanitize = (Number(this.sanitize(value))).toString();
27
+ const integer = (_a = sanitize.slice(0, -decimals)) !== null && _a !== void 0 ? _a : 0;
28
+ const decimal = sanitize.slice(-decimals);
29
+ if (value.replace(/\D/g, '') === '00' || sanitize === '')
30
+ return null;
31
+ if (decimal.length === 1)
32
+ return `${integer}.0${decimal}`;
33
+ return `${integer}.${decimal}`;
34
+ }
24
35
  }
25
36
  export default new NumberHelper();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dilicorp/ui",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A simple UI design for Dilicorp",
5
5
  "repository": {
6
6
  "type": "git",