-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauth-form-translator.ts
More file actions
68 lines (61 loc) · 1.97 KB
/
Copy pathauth-form-translator.ts
File metadata and controls
68 lines (61 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* This file has been taken 1-to-1 from https://github.com/ory/kratos-selfservice-ui-node/blob/master/src/translations.ts
*
* Slightly altered to provide a more consistent interface.
*/
// Provides mappings between the `FormFields` returned in a Kratos `**/flow` request and their implications to the UX/UI.
// The functions are self-explantatory, `const translations` should be updated to reflect your `identity.schema.json`'s configuration.
import { FormField } from "@ory/kratos-client";
const translations = {
password: {
title: "Password",
position: 2,
placeholder: "Secure Password",
},
identifier: {
title: "E-Mail Address",
position: 1,
placeholder: "john.doe@example.com",
},
"traits.email": {
title: "E-Mail Address",
position: 1,
placeholder: "john.doe@example.com",
},
"traits.name.first": {
title: "First Name",
position: 3,
placeholder: "John",
},
"traits.name.last": {
title: "Last Name",
position: 4,
placeholder: "Doe",
},
};
type Translations = typeof translations;
export const getFormFieldTitle = (field: FormField): string =>
field.name && field.name in translations
? translations[field.name as keyof Translations].title
: field.name;
export const getFormFieldPosition = (field: FormField): number =>
field.name && field.name in translations
? translations[field.name as keyof Translations].position
: Infinity;
export const getFormPlaceholder = (field: FormField): string =>
field.name && field.name in translations
? translations[field.name as keyof Translations].placeholder
: "";
// This helper function translates the html input type to the corresponding partial name.
export const toFormInputPartialName = (type: string) => {
switch (type) {
case "hidden":
return "form_input_hidden";
case "password":
return "form_input_password";
case "submit":
return "form_field_button";
default:
return "form_input_default";
}
};