mui-language-picker 1.2.0 → 1.2.3
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 +31 -0
- package/dist/ChangeName.js +11 -10
- package/dist/LanguageChoice.d.ts +2 -0
- package/dist/LanguageChoice.js +3 -1
- package/dist/LanguagePicker.d.ts +2 -0
- package/dist/LanguagePicker.js +12 -14
- package/dist/getDisplayName.d.ts +3 -0
- package/dist/getDisplayName.js +17 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
ILanguagePickerStrings,
|
|
21
21
|
languagePickerStrings_en,
|
|
22
22
|
LangTag,
|
|
23
|
+
DisplayName,
|
|
23
24
|
} from "mui-language-picker";
|
|
24
25
|
|
|
25
26
|
const MyComponent = (props: any) => {
|
|
@@ -28,6 +29,10 @@ const MyComponent = (props: any) => {
|
|
|
28
29
|
const [fontName, setFontName] = React.useState("");
|
|
29
30
|
const [tag, setTag] = React.useState<LangTag>()
|
|
30
31
|
|
|
32
|
+
const displayName = (name: string, tag?:LangTag) => {
|
|
33
|
+
return tag.localname || tag.name
|
|
34
|
+
}
|
|
35
|
+
|
|
31
36
|
return (
|
|
32
37
|
<LanguagePicker
|
|
33
38
|
value={bcp47}
|
|
@@ -36,6 +41,7 @@ const MyComponent = (props: any) => {
|
|
|
36
41
|
setName={setLgName}
|
|
37
42
|
font={fontName}
|
|
38
43
|
setFont={setFontName}
|
|
44
|
+
displayName={displayName}
|
|
39
45
|
setInfo={setTag}
|
|
40
46
|
t={languagePickerStrings_en}
|
|
41
47
|
/>
|
|
@@ -57,6 +63,7 @@ Output should be a Language Picker when entered opens a dialog
|
|
|
57
63
|
| setName\* | (name: string) => void | callback to change language name |
|
|
58
64
|
| font | string | font family name |
|
|
59
65
|
| setFont\* | (font: string) => void | callback to change font family name |
|
|
66
|
+
| displayName\* | DisplayName | function to control display of name |
|
|
60
67
|
| setInfo\* | (tag: LangTag) => void | callback to receive tag information |
|
|
61
68
|
| disabled\* | boolean | true if control disabled |
|
|
62
69
|
| t | ILanguagePickerStrings | localization strings (see below) |
|
|
@@ -88,6 +95,30 @@ export const languagePickerStrings_en = {
|
|
|
88
95
|
} as ILanguagePickerStrings;
|
|
89
96
|
```
|
|
90
97
|
|
|
98
|
+
### Information returned by setInfo
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
export interface LangTag {
|
|
102
|
+
full: string;
|
|
103
|
+
iana?: string;
|
|
104
|
+
iso639_3?: string;
|
|
105
|
+
localname?: string;
|
|
106
|
+
name: string;
|
|
107
|
+
names?: string[];
|
|
108
|
+
nophonvars?: boolean;
|
|
109
|
+
region?: string;
|
|
110
|
+
regionname?: string;
|
|
111
|
+
regions?: string[];
|
|
112
|
+
script: string;
|
|
113
|
+
sldr: boolean;
|
|
114
|
+
tag: string;
|
|
115
|
+
tags?: string[];
|
|
116
|
+
variants?: string[];
|
|
117
|
+
defaultFont?: string;
|
|
118
|
+
fonts?: string[];
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
91
122
|
### Change control background
|
|
92
123
|
If the theme involves using a dark background, the control background can be changed with css. See also [material-ui](https://mui.com/) documentation.
|
|
93
124
|
```css
|
package/dist/ChangeName.js
CHANGED
|
@@ -34,29 +34,30 @@ var DialogActions_1 = __importDefault(require("@mui/material/DialogActions"));
|
|
|
34
34
|
var DialogContent_1 = __importDefault(require("@mui/material/DialogContent"));
|
|
35
35
|
var DialogContentText_1 = __importDefault(require("@mui/material/DialogContentText"));
|
|
36
36
|
var DialogTitle_1 = __importDefault(require("@mui/material/DialogTitle"));
|
|
37
|
+
var localization_1 = require("./localization");
|
|
37
38
|
function ChangeName(_a) {
|
|
38
39
|
var isOpen = _a.isOpen, onClose = _a.onClose, curName = _a.curName, onNewName = _a.onNewName, t = _a.t;
|
|
39
40
|
var _b = React.useState(isOpen), open = _b[0], setOpen = _b[1];
|
|
40
41
|
var _c = React.useState(curName || ''), name = _c[0], setName = _c[1];
|
|
41
|
-
var handleClose = function () {
|
|
42
|
-
|
|
42
|
+
var handleClose = function (save) { return function () {
|
|
43
|
+
if (save && name)
|
|
44
|
+
onNewName && onNewName(name);
|
|
43
45
|
onClose && onClose();
|
|
44
46
|
setOpen(false);
|
|
45
|
-
};
|
|
47
|
+
}; };
|
|
46
48
|
var handleNameChange = function (event) {
|
|
47
49
|
setName(event.target.value);
|
|
48
50
|
};
|
|
49
51
|
React.useEffect(function () {
|
|
50
52
|
setOpen(isOpen);
|
|
51
53
|
}, [isOpen]);
|
|
52
|
-
return (React.createElement(Dialog_1.default, { open: open, onClose: handleClose },
|
|
53
|
-
React.createElement(DialogTitle_1.default, null, (t === null || t === void 0 ? void 0 : t.changeName) ||
|
|
54
|
+
return (React.createElement(Dialog_1.default, { open: open, onClose: handleClose() },
|
|
55
|
+
React.createElement(DialogTitle_1.default, null, (t === null || t === void 0 ? void 0 : t.changeName) || localization_1.languagePickerStrings_en.changeName),
|
|
54
56
|
React.createElement(DialogContent_1.default, null,
|
|
55
|
-
React.createElement(DialogContentText_1.default, null, (t === null || t === void 0 ? void 0 : t.nameInstruction) ||
|
|
56
|
-
|
|
57
|
-
React.createElement(TextField_1.default, { autoFocus: true, margin: "dense", id: "name", label: (t === null || t === void 0 ? void 0 : t.newName) || 'New Language Name', fullWidth: true, value: name, onChange: handleNameChange })),
|
|
57
|
+
React.createElement(DialogContentText_1.default, null, (t === null || t === void 0 ? void 0 : t.nameInstruction) || localization_1.languagePickerStrings_en.nameInstruction),
|
|
58
|
+
React.createElement(TextField_1.default, { autoFocus: true, margin: "dense", id: "name", label: (t === null || t === void 0 ? void 0 : t.newName) || localization_1.languagePickerStrings_en.newName, fullWidth: true, value: name, onChange: handleNameChange })),
|
|
58
59
|
React.createElement(DialogActions_1.default, null,
|
|
59
|
-
React.createElement(Button_1.default, { onClick: handleClose }, t === null || t === void 0 ? void 0 : t.cancel),
|
|
60
|
-
React.createElement(Button_1.default, { onClick: handleClose }, (t === null || t === void 0 ? void 0 : t.change) ||
|
|
60
|
+
React.createElement(Button_1.default, { onClick: handleClose() }, (t === null || t === void 0 ? void 0 : t.cancel) || localization_1.languagePickerStrings_en.cancel),
|
|
61
|
+
React.createElement(Button_1.default, { onClick: handleClose(true) }, (t === null || t === void 0 ? void 0 : t.change) || localization_1.languagePickerStrings_en.change))));
|
|
61
62
|
}
|
|
62
63
|
exports.default = ChangeName;
|
package/dist/LanguageChoice.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { ILanguagePickerStrings } from './model';
|
|
3
3
|
import { LangTag, ScriptName } from './langPicker/types';
|
|
4
|
+
import { DisplayName } from './getDisplayName';
|
|
4
5
|
interface IProps {
|
|
5
6
|
list: number[];
|
|
6
7
|
choose: (tag: LangTag) => void;
|
|
7
8
|
secondary?: boolean;
|
|
8
9
|
langTags: LangTag[];
|
|
9
10
|
scriptName: ScriptName;
|
|
11
|
+
displayName?: DisplayName;
|
|
10
12
|
t: ILanguagePickerStrings;
|
|
11
13
|
}
|
|
12
14
|
export declare function LanguageChoice(props: IProps): JSX.Element;
|
package/dist/LanguageChoice.js
CHANGED
|
@@ -28,8 +28,10 @@ var react_1 = __importStar(require("react"));
|
|
|
28
28
|
var material_1 = require("@mui/material");
|
|
29
29
|
var lodash_1 = require("lodash");
|
|
30
30
|
var GrowingSpacer_1 = require("./GrowingSpacer");
|
|
31
|
+
var getDisplayName_1 = require("./getDisplayName");
|
|
31
32
|
function LanguageChoice(props) {
|
|
32
33
|
var list = props.list, langTags = props.langTags, scriptName = props.scriptName, secondary = props.secondary, t = props.t, choose = props.choose;
|
|
34
|
+
var displayName = props.displayName;
|
|
33
35
|
var dense = (0, react_1.useState)(true)[0];
|
|
34
36
|
var _a = (0, react_1.useState)(window.innerHeight), height = _a[0], setHeight = _a[1];
|
|
35
37
|
var handleChoose = function (tag) { return function () {
|
|
@@ -66,7 +68,7 @@ function LanguageChoice(props) {
|
|
|
66
68
|
var tag = refTags[r];
|
|
67
69
|
return (react_1.default.createElement(material_1.ListItem, { key: "".concat(tag.tag, " ").concat(i), button: true, onClick: handleChoose(tag), onKeyDown: handleKeydown(tag) },
|
|
68
70
|
react_1.default.createElement(material_1.ListItemText, { primary: react_1.default.createElement(material_1.Box, { sx: { display: 'flex' } },
|
|
69
|
-
react_1.default.createElement(material_1.Typography, null,
|
|
71
|
+
react_1.default.createElement(material_1.Typography, null, (0, getDisplayName_1.getDisplayName)(tag.name, tag, displayName)),
|
|
70
72
|
react_1.default.createElement(GrowingSpacer_1.GrowingSpacer, null),
|
|
71
73
|
react_1.default.createElement(material_1.Typography, null, tag.tag)), secondary: secondary ? detail(tag) : null })));
|
|
72
74
|
});
|
package/dist/LanguagePicker.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { ILanguagePickerStrings } from './model';
|
|
3
3
|
import { LangTag } from './langPicker/types';
|
|
4
|
+
import { DisplayName } from './getDisplayName';
|
|
4
5
|
interface IStateProps {
|
|
5
6
|
t: ILanguagePickerStrings;
|
|
6
7
|
}
|
|
@@ -8,6 +9,7 @@ interface IProps extends IStateProps {
|
|
|
8
9
|
value: string;
|
|
9
10
|
name: string;
|
|
10
11
|
font: string;
|
|
12
|
+
displayName?: DisplayName;
|
|
11
13
|
setCode?: (code: string) => void;
|
|
12
14
|
setName?: (name: string) => void;
|
|
13
15
|
setFont?: (font: string) => void;
|
package/dist/LanguagePicker.js
CHANGED
|
@@ -30,6 +30,7 @@ var langTags_1 = require("./langTags");
|
|
|
30
30
|
var useDebounce_1 = __importDefault(require("./useDebounce"));
|
|
31
31
|
var GrowingSpacer_1 = require("./GrowingSpacer");
|
|
32
32
|
var ChangeName_1 = __importDefault(require("./ChangeName"));
|
|
33
|
+
var getDisplayName_1 = require("./getDisplayName");
|
|
33
34
|
var MAXOPTIONS = 50;
|
|
34
35
|
var menuWidth = { width: 200 };
|
|
35
36
|
var StyledDialog = (0, material_1.styled)(material_1.Dialog)(function () { return ({
|
|
@@ -42,6 +43,7 @@ var LanguagePicker = function (props) {
|
|
|
42
43
|
var _a;
|
|
43
44
|
var disabled = props.disabled;
|
|
44
45
|
var value = props.value, name = props.name, font = props.font, setCode = props.setCode, setName = props.setName, setFont = props.setFont, setInfo = props.setInfo, t = props.t;
|
|
46
|
+
var displayName = props.displayName;
|
|
45
47
|
var _b = react_1.default.useState(false), open = _b[0], setOpen = _b[1];
|
|
46
48
|
var _c = react_1.default.useState(value), curValue = _c[0], setCurValue = _c[1];
|
|
47
49
|
var _d = react_1.default.useState(name), curName = _d[0], setCurName = _d[1];
|
|
@@ -61,6 +63,7 @@ var LanguagePicker = function (props) {
|
|
|
61
63
|
var IpaTag = 'fonipa';
|
|
62
64
|
if (!LgScriptName_1.scriptName.hasOwnProperty(IpaTag))
|
|
63
65
|
LgScriptName_1.scriptName[IpaTag] = t.phonetic;
|
|
66
|
+
var respFormat = function (name, tagVal) { return "".concat(name, " (").concat(tagVal, ")"); };
|
|
64
67
|
var handleClickOpen = function (e) {
|
|
65
68
|
if (disabled)
|
|
66
69
|
return;
|
|
@@ -69,10 +72,8 @@ var LanguagePicker = function (props) {
|
|
|
69
72
|
var found = (0, bcp47_1.bcp47Find)(curValue);
|
|
70
73
|
if (curValue !== 'und') {
|
|
71
74
|
if (found && !Array.isArray(found)) {
|
|
72
|
-
var tagName = curName;
|
|
73
|
-
|
|
74
|
-
tagName = "".concat(found.localname, " / ").concat(curName);
|
|
75
|
-
setResponse(tagName + ' (' + curValue + ')');
|
|
75
|
+
var tagName = (0, getDisplayName_1.getDisplayName)(curName, found, displayName);
|
|
76
|
+
setResponse(respFormat(tagName, curValue));
|
|
76
77
|
setTag(found);
|
|
77
78
|
selectFont(found);
|
|
78
79
|
setDefaultScript(found.script);
|
|
@@ -81,7 +82,7 @@ var LanguagePicker = function (props) {
|
|
|
81
82
|
else {
|
|
82
83
|
var key = curValue.toLocaleLowerCase();
|
|
83
84
|
if ((0, LgExact_1.hasExact)(key)) {
|
|
84
|
-
setResponse(curName
|
|
85
|
+
setResponse(respFormat(curName, curValue));
|
|
85
86
|
var langTag = langTags_1.langTags[(0, LgExact_1.getExact)(key)[0]];
|
|
86
87
|
setTag(langTag);
|
|
87
88
|
selectFont(langTag);
|
|
@@ -114,10 +115,8 @@ var LanguagePicker = function (props) {
|
|
|
114
115
|
};
|
|
115
116
|
var displayTag = function (tagP, val) {
|
|
116
117
|
if (tagP && tagP.name) {
|
|
117
|
-
var tagName = tagP.name;
|
|
118
|
-
|
|
119
|
-
tagName = "".concat(tagP.localname, " / ").concat(tagP.name);
|
|
120
|
-
setResponse(tagName + ' (' + tagP.tag + ')');
|
|
118
|
+
var tagName = (0, getDisplayName_1.getDisplayName)(tagP.name, tagP, displayName);
|
|
119
|
+
setResponse(respFormat(tagName, tagP.tag));
|
|
121
120
|
setCurValue(val ? val : tagP.tag);
|
|
122
121
|
setCurName(tagP.name);
|
|
123
122
|
}
|
|
@@ -187,10 +186,9 @@ var LanguagePicker = function (props) {
|
|
|
187
186
|
};
|
|
188
187
|
var handleSetName = function (name) {
|
|
189
188
|
setCurName(name);
|
|
190
|
-
var tagName = name;
|
|
191
|
-
if (tag
|
|
192
|
-
tagName
|
|
193
|
-
setResponse(tagName);
|
|
189
|
+
var tagName = (0, getDisplayName_1.getDisplayName)(name, tag, displayName);
|
|
190
|
+
if (tag)
|
|
191
|
+
setResponse(respFormat(tagName, tag.tag));
|
|
194
192
|
};
|
|
195
193
|
react_1.default.useEffect(function () {
|
|
196
194
|
if (response === '')
|
|
@@ -354,7 +352,7 @@ var LanguagePicker = function (props) {
|
|
|
354
352
|
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
355
353
|
react_1.default.createElement(material_1.FormGroup, { row: true, sx: { justifyContent: 'flex-end' } },
|
|
356
354
|
react_1.default.createElement(material_1.FormControlLabel, { sx: { flexDirection: 'row-reverse', mr: 0 }, control: react_1.default.createElement(material_1.Checkbox, { checked: secondary, onChange: function (event) { return setSecondary(event.target.checked); }, value: "secondary" }), label: t.details })),
|
|
357
|
-
react_1.default.createElement(LanguageChoice_1.default, { list: list_1, secondary: secondary, choose: handleChoose, langTags: langTags_1.langTags, scriptName: LgScriptName_1.scriptName, t: t })));
|
|
355
|
+
react_1.default.createElement(LanguageChoice_1.default, { list: list_1, secondary: secondary, choose: handleChoose, langTags: langTags_1.langTags, scriptName: LgScriptName_1.scriptName, displayName: displayName, t: t })));
|
|
358
356
|
}
|
|
359
357
|
}
|
|
360
358
|
return react_1.default.createElement(react_1.default.Fragment, null);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDisplayName = void 0;
|
|
4
|
+
var getDisplayName = function (curName, tag, displayName) {
|
|
5
|
+
// If user setup a custom language name
|
|
6
|
+
if (tag && curName !== tag.name)
|
|
7
|
+
return curName;
|
|
8
|
+
// if caller provided a custom name formatter
|
|
9
|
+
if (displayName)
|
|
10
|
+
return displayName(curName, tag);
|
|
11
|
+
// By default show the local name if present and the name
|
|
12
|
+
var tagName = curName;
|
|
13
|
+
if ((tag === null || tag === void 0 ? void 0 : tag.localname) && tag.localname.toLowerCase() !== curName.toLowerCase())
|
|
14
|
+
tagName = "".concat(tag.localname, " / ").concat(curName);
|
|
15
|
+
return tagName;
|
|
16
|
+
};
|
|
17
|
+
exports.getDisplayName = getDisplayName;
|