@processhub-lib/react 1.0.1 → 1.0.2
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.
- package/README.md +93 -0
- package/package.json +5 -2
- package/src/scripts/generate-endpoints.ts +184 -0
- package/src/ui/Select/index.tsx +3 -2
- package/src/utils/applyMask.tsx +49 -0
- package/src/utils/formatar.tsx +173 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/isDocumento.tsx +46 -0
- package/dist/context/ThemeContext.d.ts +0 -22
- package/dist/index-DN3Jte8S.cjs +0 -30
- package/dist/index-DRwba4Gg.js +0 -1170
- package/dist/index.cjs.js +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.es.js +0 -10
- package/dist/style.css +0 -1
- package/dist/ui/Button/colors.d.ts +0 -162
- package/dist/ui/Button/index.d.ts +0 -3
- package/dist/ui/Button/types/button.d.ts +0 -16
- package/dist/ui/Input/index.d.ts +0 -3
- package/dist/ui/Input/types/input.d.ts +0 -10
- package/dist/ui/Select/index.d.ts +0 -3
- package/dist/ui/Select/types/select.d.ts +0 -16
- package/dist/ui/index.d.ts +0 -3
- package/dist/ui.cjs.js +0 -1
- package/dist/ui.d.ts +0 -1
- package/dist/ui.es.js +0 -6
- package/dist/utils/classnames.d.ts +0 -16
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils.cjs.js +0 -1
- package/dist/utils.d.ts +0 -1
- package/dist/utils.es.js +0 -29
package/README.md
CHANGED
|
@@ -1,6 +1,99 @@
|
|
|
1
|
+
# lib-react
|
|
2
|
+
|
|
1
3
|
npm version patch
|
|
2
4
|
|
|
3
5
|
npm publish --access public --tag beta
|
|
4
6
|
npm publish --access public --tag latest
|
|
5
7
|
|
|
6
8
|
npm config set //registry.npmjs.org/:\_authToken=TOKEN
|
|
9
|
+
|
|
10
|
+
## Getting started
|
|
11
|
+
|
|
12
|
+
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
|
|
13
|
+
|
|
14
|
+
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
|
|
15
|
+
|
|
16
|
+
## Add your files
|
|
17
|
+
|
|
18
|
+
* [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
|
|
19
|
+
* [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
cd existing_repo
|
|
23
|
+
git remote add origin https://gitlab.com/processhub/lib-react.git
|
|
24
|
+
git branch -M main
|
|
25
|
+
git push -uf origin main
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Integrate with your tools
|
|
29
|
+
|
|
30
|
+
* [Set up project integrations](https://gitlab.com/processhub/lib-react/-/settings/integrations)
|
|
31
|
+
|
|
32
|
+
## Collaborate with your team
|
|
33
|
+
|
|
34
|
+
* [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
|
|
35
|
+
* [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
|
|
36
|
+
* [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
|
|
37
|
+
* [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
|
|
38
|
+
* [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
|
|
39
|
+
|
|
40
|
+
## Test and Deploy
|
|
41
|
+
|
|
42
|
+
Use the built-in continuous integration in GitLab.
|
|
43
|
+
|
|
44
|
+
* [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
|
|
45
|
+
* [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
|
|
46
|
+
* [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
|
|
47
|
+
* [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
|
|
48
|
+
* [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
|
|
49
|
+
|
|
50
|
+
***
|
|
51
|
+
|
|
52
|
+
# Editing this README
|
|
53
|
+
|
|
54
|
+
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
|
|
55
|
+
|
|
56
|
+
## Suggestions for a good README
|
|
57
|
+
|
|
58
|
+
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
|
|
59
|
+
|
|
60
|
+
## Name
|
|
61
|
+
Choose a self-explaining name for your project.
|
|
62
|
+
|
|
63
|
+
## Description
|
|
64
|
+
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
|
|
65
|
+
|
|
66
|
+
## Badges
|
|
67
|
+
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
|
|
68
|
+
|
|
69
|
+
## Visuals
|
|
70
|
+
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
|
|
71
|
+
|
|
72
|
+
## Installation
|
|
73
|
+
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
|
|
74
|
+
|
|
75
|
+
## Usage
|
|
76
|
+
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
|
|
77
|
+
|
|
78
|
+
## Support
|
|
79
|
+
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
|
|
80
|
+
|
|
81
|
+
## Roadmap
|
|
82
|
+
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
|
83
|
+
|
|
84
|
+
## Contributing
|
|
85
|
+
State if you are open to contributions and what your requirements are for accepting them.
|
|
86
|
+
|
|
87
|
+
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
|
|
88
|
+
|
|
89
|
+
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
|
|
90
|
+
|
|
91
|
+
## Authors and acknowledgment
|
|
92
|
+
Show your appreciation to those who have contributed to the project.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
For open source projects, say how it is licensed.
|
|
96
|
+
|
|
97
|
+
## Project status
|
|
98
|
+
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
|
|
99
|
+
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@processhub-lib/react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
|
+
"gen:endpoints": "tsx src/scripts/generate-endpoints.ts",
|
|
6
7
|
"build": "vite build"
|
|
7
8
|
},
|
|
8
9
|
"main": "./dist/index.cjs.js",
|
|
@@ -27,9 +28,9 @@
|
|
|
27
28
|
"./style.css": "./dist/style.css"
|
|
28
29
|
},
|
|
29
30
|
"peerDependencies": {
|
|
31
|
+
"@phosphor-icons/react": "^2.1.10",
|
|
30
32
|
"react": "^18.3.1",
|
|
31
33
|
"react-dom": "^18.3.1",
|
|
32
|
-
"@phosphor-icons/react": "^2.1.10",
|
|
33
34
|
"uuid": "^13.0.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
@@ -38,6 +39,8 @@
|
|
|
38
39
|
"@types/react-dom": "^18.3.0",
|
|
39
40
|
"react": "^18.3.1",
|
|
40
41
|
"react-dom": "^18.3.1",
|
|
42
|
+
"ts-node": "^10.9.2",
|
|
43
|
+
"tsx": "^4.21.0",
|
|
41
44
|
"typescript": "^5.5.3",
|
|
42
45
|
"typescript-eslint": "^8.0.1",
|
|
43
46
|
"vite": "^5.4.19",
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
|
|
8
|
+
const INPUT_FILE = path.resolve(__dirname, "../../endpoints.json");
|
|
9
|
+
const OUTPUT_FILE = path.resolve(__dirname, "../../endpoints.ts");
|
|
10
|
+
|
|
11
|
+
const KEY_MAPPING: Record<string, string> = {
|
|
12
|
+
lista: "list",
|
|
13
|
+
busca: "search",
|
|
14
|
+
incluir: "create",
|
|
15
|
+
alterar: "update",
|
|
16
|
+
deletar: "delete",
|
|
17
|
+
posicao: "position",
|
|
18
|
+
etapa: "etapas",
|
|
19
|
+
form: "form",
|
|
20
|
+
usuario: "user",
|
|
21
|
+
auth: "auth",
|
|
22
|
+
cidade: "city",
|
|
23
|
+
pais: "country",
|
|
24
|
+
provincia: "province",
|
|
25
|
+
tipo: "type",
|
|
26
|
+
status: "status",
|
|
27
|
+
pergunta: "questions",
|
|
28
|
+
lead: "lead",
|
|
29
|
+
apoio: "support",
|
|
30
|
+
organizacao: "organization",
|
|
31
|
+
atualizar: "update",
|
|
32
|
+
resposta: "response",
|
|
33
|
+
reservar: "reserve",
|
|
34
|
+
cancelar: "cancel",
|
|
35
|
+
vendedor: "seller",
|
|
36
|
+
inativar: "inactivate",
|
|
37
|
+
ativar: "activate",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function mapKey(key: string): string {
|
|
41
|
+
return KEY_MAPPING[key] || key;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function cleanPath(urlPath: string[]): string[] {
|
|
45
|
+
// Remove empty strings and common prefixes
|
|
46
|
+
return urlPath.filter(
|
|
47
|
+
(segment) =>
|
|
48
|
+
segment &&
|
|
49
|
+
segment !== "api" &&
|
|
50
|
+
// segment !== "v1" && // Keep version
|
|
51
|
+
!segment.startsWith("{{")
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function setNestedValue(obj: any, path: string[], value: string) {
|
|
56
|
+
let current = obj;
|
|
57
|
+
for (let i = 0; i < path.length; i++) {
|
|
58
|
+
const segment = path[i];
|
|
59
|
+
const key = mapKey(segment);
|
|
60
|
+
|
|
61
|
+
if (i === path.length - 1) {
|
|
62
|
+
// We are at the leaf, trying to set a value
|
|
63
|
+
if (current[key] && typeof current[key] === "object") {
|
|
64
|
+
// Collision: key is already an object (container)
|
|
65
|
+
// Assign to .list
|
|
66
|
+
current[key].list = value;
|
|
67
|
+
} else {
|
|
68
|
+
// If it's a string, we overwrite (or it's the first time)
|
|
69
|
+
current[key] = value;
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
// We are traversing down
|
|
73
|
+
if (!current[key]) {
|
|
74
|
+
current[key] = {};
|
|
75
|
+
} else if (typeof current[key] === "string") {
|
|
76
|
+
// Collision: key is a string (leaf), but we need to go deeper
|
|
77
|
+
// Convert string to object with .list
|
|
78
|
+
current[key] = { list: current[key] };
|
|
79
|
+
}
|
|
80
|
+
current = current[key];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function generateEndpoints() {
|
|
86
|
+
try {
|
|
87
|
+
const content = fs.readFileSync(INPUT_FILE, "utf-8");
|
|
88
|
+
const collection = JSON.parse(content);
|
|
89
|
+
const endpoints: any = {};
|
|
90
|
+
|
|
91
|
+
function traverse(items: any[]) {
|
|
92
|
+
items.forEach((item) => {
|
|
93
|
+
if (item.item) {
|
|
94
|
+
traverse(item.item);
|
|
95
|
+
} else if (item.request && item.request.url) {
|
|
96
|
+
const url = item.request.url;
|
|
97
|
+
let pathSegments: string[] = [];
|
|
98
|
+
|
|
99
|
+
if (Array.isArray(url.path)) {
|
|
100
|
+
pathSegments = url.path;
|
|
101
|
+
} else if (typeof url === "string") {
|
|
102
|
+
// Handle raw string if necessary, but Postman v2.1 usually has object
|
|
103
|
+
// If it's a string we might need to parse it
|
|
104
|
+
const urlObj = new URL(
|
|
105
|
+
url.replace("{{develop}}", "http://placeholder")
|
|
106
|
+
);
|
|
107
|
+
pathSegments = urlObj.pathname.split("/").filter(Boolean);
|
|
108
|
+
} else if (url.raw) {
|
|
109
|
+
// Fallback to raw if path array is missing/empty but raw exists
|
|
110
|
+
// Remove query params
|
|
111
|
+
const rawPath = url.raw.split("?")[0];
|
|
112
|
+
pathSegments = rawPath.split("/").filter(Boolean);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const cleanedPath = cleanPath(pathSegments);
|
|
116
|
+
|
|
117
|
+
if (cleanedPath.length > 0) {
|
|
118
|
+
// Reconstruct the full path for the value
|
|
119
|
+
// We want the path starting with /api/v1...
|
|
120
|
+
// The user example shows: '/api/v1/funil/lista'
|
|
121
|
+
// So we should find where 'api' starts in the original segments
|
|
122
|
+
|
|
123
|
+
let fullPath = "";
|
|
124
|
+
if (Array.isArray(url.path)) {
|
|
125
|
+
fullPath = "/" + url.path.join("/");
|
|
126
|
+
} else if (url.raw) {
|
|
127
|
+
const urlStr = url.raw.split("?")[0];
|
|
128
|
+
// Try to extract from api/v1
|
|
129
|
+
const match = urlStr.match(/(api\/v1\/.*)/);
|
|
130
|
+
if (match) {
|
|
131
|
+
fullPath = "/" + match[1];
|
|
132
|
+
} else {
|
|
133
|
+
fullPath = urlStr;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Ensure it starts with /api/v1 if possible, or just use what we have
|
|
138
|
+
// The user example has /api/v1 prefix.
|
|
139
|
+
|
|
140
|
+
setNestedValue(endpoints, cleanedPath, fullPath);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (collection.item) {
|
|
147
|
+
traverse(collection.item);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Fix quotes to single quotes to match style if needed, but JSON.stringify uses double.
|
|
151
|
+
// The user example used single quotes for values and keys (where possible).
|
|
152
|
+
// Let's try to make it prettier.
|
|
153
|
+
|
|
154
|
+
// We can use a regex to replace double quotes with single quotes for values,
|
|
155
|
+
// and remove quotes from keys if they are valid identifiers.
|
|
156
|
+
|
|
157
|
+
// Actually, JSON.stringify is fine, but let's try to match the user's requested format strictly.
|
|
158
|
+
// The user example:
|
|
159
|
+
// export const ENDPOINTS = {
|
|
160
|
+
// funil: {
|
|
161
|
+
// list: '/api/v1/funil/lista',
|
|
162
|
+
// ...
|
|
163
|
+
|
|
164
|
+
// Let's use a custom stringifier or just simple replacement.
|
|
165
|
+
|
|
166
|
+
const finalOutput = `// ⚠️ Arquivo gerado automaticamente — NÃO EDITE
|
|
167
|
+
|
|
168
|
+
export const ENDPOINTS = ${JSON.stringify(endpoints, null, 2)
|
|
169
|
+
.replace(/"([^"]+)":/g, "$1:")
|
|
170
|
+
.replace(/: "([^"]+)"/g, ": '$1'")} as const;
|
|
171
|
+
|
|
172
|
+
type DeepValueOf<T> = T extends object ? DeepValueOf<T[keyof T]> : T;
|
|
173
|
+
export type Route = DeepValueOf<typeof ENDPOINTS>;
|
|
174
|
+
`;
|
|
175
|
+
|
|
176
|
+
fs.writeFileSync(OUTPUT_FILE, finalOutput);
|
|
177
|
+
console.log(`Successfully generated endpoints to ${OUTPUT_FILE}`);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error("Error generating endpoints:", error);
|
|
180
|
+
process.exit(1);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
generateEndpoints();
|
package/src/ui/Select/index.tsx
CHANGED
|
@@ -109,8 +109,9 @@ export function Select<T>({
|
|
|
109
109
|
title=""
|
|
110
110
|
icon={<BroomIcon />}
|
|
111
111
|
positionIcon="left"
|
|
112
|
-
onClick={() => {
|
|
113
|
-
|
|
112
|
+
onClick={(e) => {
|
|
113
|
+
e?.stopPropagation();
|
|
114
|
+
onChange?.({} as T);
|
|
114
115
|
setSearch("");
|
|
115
116
|
}}
|
|
116
117
|
className="m-0"
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Aplica uma máscara numérica ao valor informado.
|
|
3
|
+
*
|
|
4
|
+
* @param value - String com dígitos (ex: "12345678901")
|
|
5
|
+
* @param tipo Opcional - Tipo de máscara ("cnpj" ou "cpf")
|
|
6
|
+
* @param mask Opcional - Máscara no formato com 'x' para dígitos (ex: "xxx.xxx.xxx-xx")
|
|
7
|
+
* @returns Valor formatado conforme máscara (ex: "123.456.789-01")
|
|
8
|
+
*/
|
|
9
|
+
export function applyMask(
|
|
10
|
+
value: string,
|
|
11
|
+
tipo?: "cnpj" | "cpf" | "outro",
|
|
12
|
+
mask?: string,
|
|
13
|
+
): string {
|
|
14
|
+
if (tipo?.toLocaleLowerCase() === "cnpj") {
|
|
15
|
+
return applyMask(value, "outro", "xx.xxx.xxx/xxxx-xx");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (tipo?.toLocaleLowerCase() === "cpf") {
|
|
19
|
+
return applyMask(value, "outro", "xxx.xxx.xxx-xx");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Remove tudo que não for dígito
|
|
23
|
+
const digits = value.replace(/\D/g, "");
|
|
24
|
+
let result = "";
|
|
25
|
+
let digitIndex = 0;
|
|
26
|
+
|
|
27
|
+
if (mask) {
|
|
28
|
+
for (let i = 0; i < mask.length; i++) {
|
|
29
|
+
if (mask[i] === "x") {
|
|
30
|
+
if (digitIndex < digits.length) {
|
|
31
|
+
result += digits[digitIndex];
|
|
32
|
+
digitIndex++;
|
|
33
|
+
} else {
|
|
34
|
+
// Se não tiver mais dígitos, não adiciona nada, mas não coloca os caracteres fixos
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
// Adiciona o caractere fixo apenas se houver dígito correspondente depois dele
|
|
39
|
+
if (digitIndex < digits.length) {
|
|
40
|
+
result += mask[i];
|
|
41
|
+
} else {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @file formatar.tsx
|
|
4
|
+
* @description Utilitário unificado para formatação de dados (Data, Massa, Moeda, Porcentagem).
|
|
5
|
+
* Este módulo não possui dependências externas.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Unidades de massa suportadas para conversão e exibição */
|
|
9
|
+
export type UnidadeMassa = "kg" | "t" | "kt" | "Mt";
|
|
10
|
+
|
|
11
|
+
/** Fatores de conversão de massa tendo kg como base */
|
|
12
|
+
const MASSA_FATOR: Record<UnidadeMassa, number> = {
|
|
13
|
+
kg: 1,
|
|
14
|
+
t: 1_000,
|
|
15
|
+
kt: 1_000_000,
|
|
16
|
+
Mt: 1_000_000_000,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Tipos de formatação disponíveis no sistema */
|
|
20
|
+
export type TipoFormato = "data" | "massa" | "moeda" | "porcentagem";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Interface para as opções de configuração dos formatadores.
|
|
24
|
+
* Permite personalizar a exibição de acordo com o tipo de dado.
|
|
25
|
+
*
|
|
26
|
+
* @property formato - Template para formatação de data (ex: "DD/MM/YYYY", "YYYY-MM-DD HH:mm"). Padrão: "DD/MM/YYYY"
|
|
27
|
+
* @property unidade - Unidade de massa para conversão/exibição. Padrão: "t"
|
|
28
|
+
* @property locale - Localidade para Intl (ex: "pt-BR", "en-US"). Padrão: "pt-BR"
|
|
29
|
+
* @property minimumFractionDigits - Mínimo de casas decimais.
|
|
30
|
+
* @property maximumFractionDigits - Máximo de casas decimais.
|
|
31
|
+
* @property currency - Moeda para formatação monetária (ex: "BRL", "USD"). Padrão: "BRL"
|
|
32
|
+
* @property notation - Notação numérica (standard, scientific, engineering, compact).
|
|
33
|
+
* @property compactDisplay - Exibição compacta (short, long).
|
|
34
|
+
*/
|
|
35
|
+
export interface FormatarOptions {
|
|
36
|
+
formato?: string;
|
|
37
|
+
unidade?: UnidadeMassa;
|
|
38
|
+
locale?: string;
|
|
39
|
+
minimumFractionDigits?: number;
|
|
40
|
+
maximumFractionDigits?: number;
|
|
41
|
+
currency?: string;
|
|
42
|
+
notation?: "standard" | "scientific" | "engineering" | "compact";
|
|
43
|
+
compactDisplay?: "short" | "long";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Método unificado para formatação de diversos tipos de dados no sistema.
|
|
48
|
+
* Suporta datas (nativamente), massa, moeda e porcentagem.
|
|
49
|
+
*
|
|
50
|
+
* @param tipo - O tipo de dado a ser formatado ("data", "massa", "moeda", "porcentagem")
|
|
51
|
+
* @param valor - O valor bruto (String, Number ou Date)
|
|
52
|
+
* @param opcoes - Opções de customização (locale, formato, unidade, etc)
|
|
53
|
+
* @returns String formatada seguindo as regras do tipo e localidade
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // Formatação de Moeda
|
|
57
|
+
* formatar("moeda", 1500.5) // "R$ 1.500,50"
|
|
58
|
+
* formatar("moeda", 1500.5, { notation: "compact" }) // "1,5 mil R$"
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* // Formatação de Data
|
|
62
|
+
* formatar("data", new Date()) // "19/03/2026"
|
|
63
|
+
* formatar("data", "2026-12-25", { formato: "DD/MM" }) // "25/12"
|
|
64
|
+
* formatar("data", "25/12/2026 15:30", { formato: "YYYY-MM-DD HH:mm" }) // "2026-12-25 15:30"
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // Formatação de Massa
|
|
68
|
+
* formatar("massa", 5000) // "5,0000 t"
|
|
69
|
+
* formatar("massa", 5000, { unidade: "kg" }) // "5.000,0000 kg"
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // Formatação de Porcentagem
|
|
73
|
+
* formatar("porcentagem", 75.5) // "75,5%"
|
|
74
|
+
* formatar("porcentagem", 100) // "100%"
|
|
75
|
+
*/export const formatar = (
|
|
76
|
+
tipo: TipoFormato,
|
|
77
|
+
valor: any,
|
|
78
|
+
opcoes: FormatarOptions = {},
|
|
79
|
+
): string => {
|
|
80
|
+
const {
|
|
81
|
+
locale = "pt-BR",
|
|
82
|
+
formato = "DD/MM/YYYY",
|
|
83
|
+
unidade = "t",
|
|
84
|
+
minimumFractionDigits,
|
|
85
|
+
maximumFractionDigits,
|
|
86
|
+
currency = "BRL",
|
|
87
|
+
} = opcoes;
|
|
88
|
+
|
|
89
|
+
/** Converte valores para número, tratando vírgulas de strings como ponto decimal */
|
|
90
|
+
const parseNumber = (v: any): number => {
|
|
91
|
+
if (typeof v === "number") return v;
|
|
92
|
+
if (typeof v === "string") return Number(v.replace(",", "."));
|
|
93
|
+
return NaN;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
switch (tipo) {
|
|
97
|
+
case "data": {
|
|
98
|
+
if (!valor) return "";
|
|
99
|
+
|
|
100
|
+
const parseDate = (v: any): Date => {
|
|
101
|
+
if (v instanceof Date) return v;
|
|
102
|
+
if (typeof v === "string") {
|
|
103
|
+
const match = v.match(
|
|
104
|
+
/^(\d{2})\/(\d{2})\/(\d{4})(?: (\d{2}):(\d{2})(?::(\d{2}))?)?$/,
|
|
105
|
+
);
|
|
106
|
+
if (match) {
|
|
107
|
+
const y = Number(match[3]);
|
|
108
|
+
const m = Number(match[2]) - 1;
|
|
109
|
+
const day = Number(match[1]);
|
|
110
|
+
const h = Number(match[4] || 0);
|
|
111
|
+
const min = Number(match[5] || 0);
|
|
112
|
+
const s = Number(match[6] || 0);
|
|
113
|
+
return new Date(y, m, day, h, min, s);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return new Date(v);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const d = parseDate(valor);
|
|
120
|
+
if (isNaN(d.getTime())) return "";
|
|
121
|
+
|
|
122
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
123
|
+
const tokens: Record<string, string> = {
|
|
124
|
+
YYYY: String(d.getFullYear()),
|
|
125
|
+
YY: String(d.getFullYear()).slice(-2),
|
|
126
|
+
MM: pad(d.getMonth() + 1),
|
|
127
|
+
DD: pad(d.getDate()),
|
|
128
|
+
HH: pad(d.getHours()),
|
|
129
|
+
mm: pad(d.getMinutes()),
|
|
130
|
+
ss: pad(d.getSeconds()),
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return formato.replace(
|
|
134
|
+
/YYYY|YY|MM|DD|HH|mm|ss/g,
|
|
135
|
+
(match) => tokens[match],
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
case "massa": {
|
|
139
|
+
const num = parseNumber(valor);
|
|
140
|
+
if (isNaN(num)) return "Valor inválido";
|
|
141
|
+
const valorConvertido = num / MASSA_FATOR[unidade];
|
|
142
|
+
const formatado = new Intl.NumberFormat(locale, {
|
|
143
|
+
minimumFractionDigits: minimumFractionDigits ?? 4,
|
|
144
|
+
maximumFractionDigits: maximumFractionDigits ?? 4,
|
|
145
|
+
}).format(valorConvertido);
|
|
146
|
+
return `${formatado} ${unidade}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
case "moeda": {
|
|
150
|
+
const num = parseNumber(valor);
|
|
151
|
+
if (isNaN(num)) return "Valor inválido";
|
|
152
|
+
return new Intl.NumberFormat(locale, {
|
|
153
|
+
notation: opcoes.notation ?? "standard",
|
|
154
|
+
compactDisplay: opcoes.compactDisplay ?? "short",
|
|
155
|
+
style: "currency",
|
|
156
|
+
currency,
|
|
157
|
+
}).format(num);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
case "porcentagem": {
|
|
161
|
+
const num = parseNumber(valor);
|
|
162
|
+
if (isNaN(num)) return "0%";
|
|
163
|
+
return new Intl.NumberFormat(locale, {
|
|
164
|
+
style: "percent",
|
|
165
|
+
minimumFractionDigits: minimumFractionDigits ?? 0,
|
|
166
|
+
maximumFractionDigits: maximumFractionDigits ?? 1,
|
|
167
|
+
}).format(num / 100);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
default:
|
|
171
|
+
return String(valor);
|
|
172
|
+
}
|
|
173
|
+
};
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Valida CPF ou CNPJ de forma unificada, identificando automaticamente o tipo pelo tamanho.
|
|
3
|
+
*
|
|
4
|
+
* @param documento - CPF ou CNPJ com ou sem formatação.
|
|
5
|
+
* @returns `true` se válido, `false` caso contrário.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* isDocumento("123.456.789-00") // true ou false
|
|
9
|
+
* isDocumento("12.345.678/0001-90") // true ou false
|
|
10
|
+
*/
|
|
11
|
+
export function isDocumento(documento: string): boolean {
|
|
12
|
+
const doc = documento.replace(/[^\d]+/g, "");
|
|
13
|
+
|
|
14
|
+
// Elimina documentos com todos os dígitos iguais
|
|
15
|
+
if (/^(\d)\1+$/.test(doc)) return false;
|
|
16
|
+
|
|
17
|
+
// Validação de CPF
|
|
18
|
+
if (doc.length === 11) {
|
|
19
|
+
for (let j = 9; j <= 10; j++) {
|
|
20
|
+
let soma = 0;
|
|
21
|
+
for (let i = 0; i < j; i++) {
|
|
22
|
+
soma += parseInt(doc.charAt(i)) * (j + 1 - i);
|
|
23
|
+
}
|
|
24
|
+
let resto = (soma * 10) % 11;
|
|
25
|
+
if (resto === 10 || resto === 11) resto = 0;
|
|
26
|
+
if (resto !== parseInt(doc.charAt(j))) return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Validação de CNPJ
|
|
32
|
+
if (doc.length === 14) {
|
|
33
|
+
const b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
34
|
+
for (let i = 12; i <= 13; i++) {
|
|
35
|
+
let n = 0;
|
|
36
|
+
for (let j = 0; j < i; j++) {
|
|
37
|
+
n += parseInt(doc.charAt(j)) * b[j + 13 - i];
|
|
38
|
+
}
|
|
39
|
+
const resultado = (n %= 11) < 2 ? 0 : 11 - n;
|
|
40
|
+
if (parseInt(doc.charAt(i)) !== resultado) return false;
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { default as React, ReactNode } from 'react';
|
|
2
|
-
|
|
3
|
-
export interface ColorSet {
|
|
4
|
-
bg: string;
|
|
5
|
-
hover: string;
|
|
6
|
-
text: string;
|
|
7
|
-
border: string;
|
|
8
|
-
}
|
|
9
|
-
export interface Theme {
|
|
10
|
-
[key: string]: {
|
|
11
|
-
solid: ColorSet;
|
|
12
|
-
outline: ColorSet;
|
|
13
|
-
ghost: ColorSet;
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
export declare const useTheme: () => Theme;
|
|
17
|
-
interface ProcessHubProviderProps {
|
|
18
|
-
children: ReactNode;
|
|
19
|
-
theme?: Partial<Theme>;
|
|
20
|
-
}
|
|
21
|
-
export declare const ProcessHubProvider: React.FC<ProcessHubProviderProps>;
|
|
22
|
-
export {};
|