@ssplib/react-components 0.0.91 → 0.0.93

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.
@@ -1,7 +1,8 @@
1
1
  /// <reference types="react" />
2
- export default function CheckBox({ name, title, xs, sm, md }: {
2
+ export default function CheckBox({ name, title, defaultValue, xs, sm, md }: {
3
3
  name: string;
4
4
  title: string | JSX.Element;
5
+ defaultValue?: boolean;
5
6
  xs?: number;
6
7
  sm?: number;
7
8
  md?: number;
@@ -7,9 +7,9 @@ const material_1 = require("@mui/material");
7
7
  const react_1 = require("react");
8
8
  const form_1 = require("../../../context/form");
9
9
  const react_2 = __importDefault(require("react"));
10
- function CheckBox({ name, title, xs = 12, sm, md }) {
10
+ function CheckBox({ name, title, defaultValue = false, xs = 12, sm, md }) {
11
11
  const context = (0, react_1.useContext)(form_1.FormContext);
12
12
  return (react_2.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
13
- react_2.default.createElement(material_1.FormControlLabel, Object.assign({ control: react_2.default.createElement(material_1.Checkbox, { size: 'small' }), label: title }, context === null || context === void 0 ? void 0 : context.formRegister(name)))));
13
+ react_2.default.createElement(material_1.FormControlLabel, Object.assign({ control: react_2.default.createElement(material_1.Checkbox, { size: 'small', defaultChecked: defaultValue }), label: title }, context === null || context === void 0 ? void 0 : context.formRegister(name)))));
14
14
  }
15
15
  exports.default = CheckBox;
@@ -1,8 +1,9 @@
1
1
  import { ReactElement } from 'react';
2
- export default function CheckBoxWarning({ name, title, customWarning, xs, sm, md }: {
2
+ export default function CheckBoxWarning({ name, title, customWarning, defaultValue, xs, sm, md, }: {
3
3
  name: string;
4
4
  title: string;
5
5
  customWarning?: ReactElement;
6
+ defaultValue?: boolean;
6
7
  xs?: number;
7
8
  sm?: number;
8
9
  md?: number;
@@ -30,10 +30,10 @@ const material_1 = require("@mui/material");
30
30
  const Typography_1 = __importDefault(require("@mui/material/Typography"));
31
31
  const react_1 = __importStar(require("react"));
32
32
  const form_1 = require("../../../context/form");
33
- function CheckBoxWarning({ name, title, customWarning, xs = 12, sm, md }) {
33
+ function CheckBoxWarning({ name, title, customWarning, defaultValue = false, xs = 12, sm, md, }) {
34
34
  const context = (0, react_1.useContext)(form_1.FormContext);
35
35
  return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
36
- react_1.default.createElement(material_1.FormControlLabel, { control: react_1.default.createElement(material_1.Checkbox, Object.assign({ size: 'small' }, context.formRegister(name))), label: title }),
36
+ react_1.default.createElement(material_1.FormControlLabel, { control: react_1.default.createElement(material_1.Checkbox, Object.assign({ size: 'small' }, context.formRegister(name), { defaultChecked: defaultValue })), label: title }),
37
37
  context.formWatch(name) && (react_1.default.createElement(material_1.Card, { sx: { bgcolor: '#FFFBF5', color: '#F59E0B', padding: 1, paddingLeft: 2 } }, customWarning ? (customWarning) : (react_1.default.createElement(Typography_1.default, null,
38
38
  react_1.default.createElement("b", null, "Aten\u00E7\u00E3o"),
39
39
  " Eventos da natureza ",
@@ -1,10 +1,11 @@
1
1
  /// <reference types="react" />
2
- export default function DatePicker({ name, required, title, xs, sm, md, minDt, maxDt, ...props }: {
2
+ export default function DatePicker({ name, required, title, xs, sm, md, minDt, defaultValue, maxDt, ...props }: {
3
3
  minDt?: string;
4
4
  maxDt?: string;
5
5
  name: string;
6
6
  title?: string;
7
7
  required?: boolean;
8
+ defaultValue?: string;
8
9
  xs?: number;
9
10
  sm?: number;
10
11
  md?: number;
@@ -49,19 +49,25 @@ const react_1 = __importStar(require("react"));
49
49
  const form_1 = require("../../../context/form");
50
50
  function DatePicker(_a) {
51
51
  var _b;
52
- var { name, required = false, title, xs = 12, sm, md, minDt, maxDt } = _a, props = __rest(_a, ["name", "required", "title", "xs", "sm", "md", "minDt", "maxDt"]);
52
+ var { name, required = false, title, xs = 12, sm, md, minDt, defaultValue = '', maxDt } = _a, props = __rest(_a, ["name", "required", "title", "xs", "sm", "md", "minDt", "defaultValue", "maxDt"]);
53
53
  const context = (0, react_1.useContext)(form_1.FormContext);
54
- const [value, setValue] = (0, react_1.useState)(null);
54
+ const [value, setValue] = (0, react_1.useState)((0, dayjs_1.default)(defaultValue, 'DD/MM/YYYY'));
55
55
  const handleChange = (newValue) => {
56
+ console.log('mudou');
56
57
  setValue(newValue);
57
- context === null || context === void 0 ? void 0 : context.formSetValue(name, newValue === null || newValue === void 0 ? void 0 : newValue.format('DD/MM/YYYY'));
58
58
  };
59
+ (0, react_1.useEffect)(() => {
60
+ if (!value)
61
+ return;
62
+ context.formSetValue(name, value.format('DD/MM/YYYY'));
63
+ }, [value]);
59
64
  return (react_1.default.createElement(react_1.default.Fragment, null,
60
65
  react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
61
66
  title && react_1.default.createElement(material_1.InputLabel, null, title),
62
67
  react_1.default.createElement(x_date_pickers_1.LocalizationProvider, { adapterLocale: pt_br_1.default, dateAdapter: AdapterDayjs_1.AdapterDayjs },
63
68
  react_1.default.createElement(x_date_pickers_2.DatePicker, { minDate: (0, dayjs_1.default)(minDt, 'DD/MM/YYYY'), maxDate: (0, dayjs_1.default)(maxDt, 'DD/MM/YYYY'), format: 'DD/MM/YYYY', value: value, onChange: handleChange, disableHighlightToday: true, sx: {
64
69
  outline: (0, lodash_get_1.default)(context.errors, name) ? '1px solid #a51c30' : '',
70
+ width: '100%',
65
71
  div: {
66
72
  input: {
67
73
  paddingX: 2,
@@ -1,8 +1,9 @@
1
1
  /// <reference types="react" />
2
- export default function TimePicker({ name, required, title, xs, sm, md }: {
2
+ export default function TimePicker({ name, required, title, defaultValue, xs, sm, md, }: {
3
3
  name: string;
4
4
  title?: string;
5
5
  required?: boolean;
6
+ defaultValue?: string;
6
7
  xs?: number;
7
8
  sm?: number;
8
9
  md?: number;
@@ -29,24 +29,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const material_1 = require("@mui/material");
30
30
  const x_date_pickers_1 = require("@mui/x-date-pickers");
31
31
  const AdapterDayjs_1 = require("@mui/x-date-pickers/AdapterDayjs");
32
+ const dayjs_1 = __importDefault(require("dayjs"));
32
33
  const pt_br_1 = __importDefault(require("dayjs/locale/pt-br"));
33
34
  const lodash_get_1 = __importDefault(require("lodash.get"));
34
35
  const react_1 = __importStar(require("react"));
35
36
  const form_1 = require("../../../context/form");
36
37
  const lodash_hasin_1 = __importDefault(require("lodash.hasin"));
37
- function TimePicker({ name, required = false, title, xs = 12, sm, md }) {
38
+ function TimePicker({ name, required = false, title, defaultValue = '', xs = 12, sm, md, }) {
38
39
  var _a;
39
40
  const context = (0, react_1.useContext)(form_1.FormContext);
40
- const [value, setValue] = (0, react_1.useState)(null);
41
+ const [value, setValue] = (0, react_1.useState)((0, dayjs_1.default)(defaultValue, 'HH:mm'));
41
42
  const handleChange = (newValue) => {
42
43
  setValue(newValue);
43
44
  context === null || context === void 0 ? void 0 : context.formSetValue(name, newValue === null || newValue === void 0 ? void 0 : newValue.format('HH:mm'));
44
45
  };
46
+ (0, react_1.useEffect)(() => {
47
+ if (!value)
48
+ return;
49
+ context.formSetValue(name, value.format('HH:mm'));
50
+ }, []);
45
51
  return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
46
52
  title && react_1.default.createElement(material_1.InputLabel, { required: required }, title),
47
53
  react_1.default.createElement(x_date_pickers_1.LocalizationProvider, { adapterLocale: pt_br_1.default, dateAdapter: AdapterDayjs_1.AdapterDayjs },
48
54
  react_1.default.createElement(x_date_pickers_1.TimePicker, { value: value, ampm: false, onChange: handleChange, sx: {
49
55
  outline: (0, lodash_get_1.default)(context.errors, name) ? '1px solid #a51c30' : '',
56
+ width: '100%',
50
57
  div: {
51
58
  input: {
52
59
  paddingX: 2,
@@ -1,9 +1,10 @@
1
1
  /// <reference types="react" />
2
- export default function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch, required, xs, sm, md, }: {
2
+ export default function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch, required, defaultValue, xs, sm, md, }: {
3
3
  name: string;
4
4
  url: string;
5
5
  title: string;
6
- customLoadingText: string;
6
+ customLoadingText?: string;
7
+ defaultValue?: number;
7
8
  required?: boolean;
8
9
  shouldRefetch?: boolean;
9
10
  xs?: number;
@@ -31,18 +31,39 @@ const react_1 = __importStar(require("react"));
31
31
  const form_1 = require("../../../context/form");
32
32
  const auth_1 = require("../../../context/auth");
33
33
  const lodash_get_1 = __importDefault(require("lodash.get"));
34
- function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch = true, required = false, xs = 12, sm, md, }) {
34
+ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch = true, required = false, defaultValue, xs = 12, sm, md, }) {
35
35
  const context = (0, react_1.useContext)(form_1.FormContext);
36
36
  const [loading, setLoading] = (0, react_1.useState)(true);
37
37
  const [list, setList] = (0, react_1.useState)([]);
38
38
  const [loadingText, setLoadingText] = (0, react_1.useState)('Carregando...');
39
39
  const { user } = (0, react_1.useContext)(auth_1.AuthContext);
40
+ (0, react_1.useEffect)(() => {
41
+ if (defaultValue) {
42
+ fetch(url, {
43
+ method: 'GET',
44
+ headers: {
45
+ Authorization: `Bearer ${user === null || user === void 0 ? void 0 : user.token}`,
46
+ },
47
+ }).then((res) => {
48
+ if (res.ok) {
49
+ res.json().then((j) => {
50
+ setList(j.body.data);
51
+ setLoading(false);
52
+ context === null || context === void 0 ? void 0 : context.formSetValue(name, j.body.data[defaultValue].id);
53
+ });
54
+ }
55
+ else {
56
+ setLoadingText('Erro ao carregar dados');
57
+ }
58
+ });
59
+ }
60
+ }, []);
40
61
  function onFocus() {
41
- if (!shouldRefetch && list.length > 0)
62
+ if (!defaultValue && !shouldRefetch && list.length > 0)
42
63
  return;
43
64
  setLoading(true);
44
65
  setList([]);
45
- setLoadingText(customLoadingText);
66
+ customLoadingText && setLoadingText(customLoadingText);
46
67
  fetch(url, {
47
68
  method: 'GET',
48
69
  headers: {
@@ -60,6 +81,9 @@ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch
60
81
  }
61
82
  });
62
83
  }
84
+ if (defaultValue && list.length <= 0)
85
+ return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
86
+ react_1.default.createElement(material_1.TextField, { size: 'small', fullWidth: true, placeholder: loadingText, disabled: true })));
63
87
  return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
64
88
  title && react_1.default.createElement(material_1.InputLabel, { required: required }, title),
65
89
  react_1.default.createElement("input", Object.assign({ type: 'text' }, context === null || context === void 0 ? void 0 : context.formRegister(name, {
@@ -68,7 +92,7 @@ function FetchAutoComplete({ name, url, title, customLoadingText, shouldRefetch
68
92
  return 'Este campo é obrigatório';
69
93
  },
70
94
  }), { hidden: true })),
71
- react_1.default.createElement(material_1.Autocomplete, { loading: loading, loadingText: loadingText, options: list, isOptionEqualToValue: (op, value) => op.id === value.id, onChange: (e, v) => context === null || context === void 0 ? void 0 : context.formSetValue(name, v ? v.id : ''), renderInput: (params) => {
95
+ react_1.default.createElement(material_1.Autocomplete, { loading: loading, loadingText: loadingText, options: list, defaultValue: defaultValue ? list[defaultValue] : undefined, isOptionEqualToValue: (op, value) => op.id === value.id, onChange: (e, v) => context === null || context === void 0 ? void 0 : context.formSetValue(name, v ? v.id : ''), renderInput: (params) => {
72
96
  var _a;
73
97
  return (react_1.default.createElement(material_1.TextField, Object.assign({}, params, { size: 'small', fullWidth: true, placeholder: title, onFocus: onFocus, required: true, error: (0, lodash_get_1.default)(context === null || context === void 0 ? void 0 : context.errors, name) ? true : false, helperText: (_a = (0, lodash_get_1.default)(context === null || context === void 0 ? void 0 : context.errors, name)) === null || _a === void 0 ? void 0 : _a.message })));
74
98
  }, size: 'small', fullWidth: true })));
@@ -1,11 +1,12 @@
1
1
  import React from 'react';
2
- export declare function Input({ type, numberMask, xs, sm, inputMinLength, inputMaxLength, md, ...props }: {
2
+ export declare function Input({ type, numberMask, xs, sm, inputMinLength, inputMaxLength, defaultValue, md, ...props }: {
3
3
  type: 'cnpj' | 'cpf' | 'input' | 'email' | 'cpf_cnpj' | 'phone' | 'input' | 'number' | 'rg' | 'password' | 'cep';
4
4
  name: string;
5
5
  title?: string;
6
6
  required?: boolean;
7
7
  numberMask?: string;
8
8
  customPlaceholder?: string;
9
+ defaultValue?: string;
9
10
  inputMinLength?: number;
10
11
  inputMaxLength?: number;
11
12
  xs?: number;
@@ -44,7 +44,7 @@ const react_1 = __importStar(require("react"));
44
44
  const MaskInput_1 = __importDefault(require("./MaskInput"));
45
45
  const form_1 = require("../../../context/form");
46
46
  function Input(_a) {
47
- var { type = 'input', numberMask = '000000000000000', xs = 12, sm, inputMinLength = 3, inputMaxLength = 255, md } = _a, props = __rest(_a, ["type", "numberMask", "xs", "sm", "inputMinLength", "inputMaxLength", "md"]);
47
+ var { type = 'input', numberMask = '000000000000000', xs = 12, sm, inputMinLength = 3, inputMaxLength = 255, defaultValue = '', md } = _a, props = __rest(_a, ["type", "numberMask", "xs", "sm", "inputMinLength", "inputMaxLength", "defaultValue", "md"]);
48
48
  const context = (0, react_1.useContext)(form_1.FormContext);
49
49
  const chooseInput = (0, react_1.useCallback)(() => {
50
50
  const inputConfig = {
@@ -104,26 +104,26 @@ function Input(_a) {
104
104
  switch (type) {
105
105
  case 'input':
106
106
  case 'email':
107
- return react_1.default.createElement(material_1.TextField, Object.assign({}, formConfig));
107
+ return react_1.default.createElement(material_1.TextField, Object.assign({}, formConfig, { defaultValue: defaultValue }));
108
108
  case 'password':
109
109
  return react_1.default.createElement(material_1.TextField, Object.assign({}, formConfig, { type: 'password' }));
110
110
  case 'number':
111
- return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, maskProps: {
111
+ return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, defaultValue: defaultValue, maskProps: {
112
112
  mask: numberMask,
113
113
  } }));
114
114
  case 'cep':
115
- return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, maskProps: {
115
+ return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, defaultValue: defaultValue, maskProps: {
116
116
  mask: '00000-000',
117
117
  } }));
118
118
  case 'phone':
119
- return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, maskProps: {
119
+ return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, defaultValue: defaultValue, maskProps: {
120
120
  mask: '(00) [#]0000-0000',
121
121
  definitions: {
122
122
  '#': /^9$/,
123
123
  },
124
124
  } }));
125
125
  case 'cpf_cnpj':
126
- return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, maskProps: {
126
+ return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, defaultValue: defaultValue, maskProps: {
127
127
  mask: '000.000.000-00[0]',
128
128
  }, onMask: (value, setMask) => {
129
129
  if (value.length > 14)
@@ -132,15 +132,15 @@ function Input(_a) {
132
132
  setMask('000.000.000-00[0]');
133
133
  } }));
134
134
  case 'cpf':
135
- return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, maskProps: {
135
+ return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, defaultValue: defaultValue, maskProps: {
136
136
  mask: '000.000.000-00',
137
137
  } }));
138
138
  case 'cnpj':
139
- return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, maskProps: {
139
+ return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, defaultValue: defaultValue, maskProps: {
140
140
  mask: '00.000.000/0000-00',
141
141
  } }));
142
142
  case 'rg':
143
- return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, maskProps: {
143
+ return (react_1.default.createElement(MaskInput_1.default, { formConfig: formConfig, defaultValue: defaultValue, maskProps: {
144
144
  mask: '00000[000000]',
145
145
  } }));
146
146
  }
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  export default function MaskInput(props: {
3
3
  formConfig: object;
4
+ defaultValue?: string;
4
5
  maskProps: {
5
6
  mask: string | RegExp;
6
7
  definitions?: {
@@ -60,7 +60,7 @@ function MaskInput(props) {
60
60
  const name = props.formConfig.name;
61
61
  const value = e.target.value;
62
62
  context.formSetValue(name, value);
63
- }, onFocus: (e) => setMaskValue(e.target.value), onChange: onChangeInput, InputProps: {
63
+ }, defaultValue: props.defaultValue, onFocus: (e) => setMaskValue(e.target.value), onChange: onChangeInput, InputProps: {
64
64
  inputComponent: TextMaskCustom,
65
65
  inputProps: { maskProps: props.maskProps, onMask: props.onMask, maskValue, setMaskValue },
66
66
  }, disabled: props.disabled, fullWidth: true })));
@@ -1,9 +1,10 @@
1
1
  /// <reference types="react" />
2
- export default function MultInput({ name, required, title, customPlaceholder, xs, sm, inputMinLength, inputMaxLength, md, ...props }: {
2
+ export default function MultInput({ name, required, title, customPlaceholder, defaultValue, xs, sm, inputMinLength, inputMaxLength, md, ...props }: {
3
3
  name: string;
4
4
  title?: string;
5
5
  customPlaceholder?: string;
6
6
  required?: boolean;
7
+ defaultValue?: string;
7
8
  inputMinLength?: number;
8
9
  inputMaxLength?: number;
9
10
  xs?: number;
@@ -44,11 +44,11 @@ const react_1 = __importStar(require("react"));
44
44
  const form_1 = require("../../../context/form");
45
45
  function MultInput(_a) {
46
46
  var _b;
47
- var { name, required = false, title, customPlaceholder, xs = 12, sm, inputMinLength = 3, inputMaxLength = 255, md } = _a, props = __rest(_a, ["name", "required", "title", "customPlaceholder", "xs", "sm", "inputMinLength", "inputMaxLength", "md"]);
47
+ var { name, required = false, title, customPlaceholder, defaultValue = '', xs = 12, sm, inputMinLength = 3, inputMaxLength = 255, md } = _a, props = __rest(_a, ["name", "required", "title", "customPlaceholder", "defaultValue", "xs", "sm", "inputMinLength", "inputMaxLength", "md"]);
48
48
  const context = (0, react_1.useContext)(form_1.FormContext);
49
49
  return (react_1.default.createElement(material_1.Grid, Object.assign({ item: true }, { xs, sm, md }),
50
50
  title && (react_1.default.createElement(InputLabel_1.default, { required: required, sx: { textTransform: 'capitalize' } }, title)),
51
- react_1.default.createElement(material_1.TextField, Object.assign({ multiline: true, fullWidth: true, rows: 3 }, context.formRegister(name, {
51
+ react_1.default.createElement(material_1.TextField, Object.assign({ multiline: true, fullWidth: true, rows: 3, defaultValue: defaultValue }, context.formRegister(name, {
52
52
  validate: (v, f) => {
53
53
  if (required && v.length <= 0)
54
54
  return 'Este campo é obrigatório';
@@ -47,7 +47,7 @@ function ToggleVisibility(_a) {
47
47
  props.unregisterNameList.forEach((x) => context.formUnregister(x));
48
48
  }
49
49
  };
50
- }, []);
50
+ }, [props.unregisterNameList]);
51
51
  return react_1.default.createElement(react_1.default.Fragment, null);
52
52
  }
53
53
  exports.ToggleVisibility = ToggleVisibility;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssplib/react-components",
3
- "version": "0.0.91",
3
+ "version": "0.0.93",
4
4
  "description": "SSP React Components",
5
5
  "main": "index.js",
6
6
  "author": "Pedro Henrique <sr.hudrick@gmail.com>",