Skip to content

Commit 953fec4

Browse files
Merge pull request #7116 from valadas/issue/7114
Fixed an issue where language flags would not show in PersonaBar
2 parents fbf12ce + 0c4bb58 commit 953fec4

3 files changed

Lines changed: 94 additions & 36 deletions

File tree

Dnn.AdminExperience/ClientSide/Dnn.React.Common/.storybook/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ const config = {
7272
require.resolve("less-loader"),
7373
],
7474
});
75+
76+
// Ensure Storybook handles image imports the same way the runtime build does.
77+
config.module.rules.push({
78+
test: /\.(png|jpe?g|gif|webp|ico)$/i,
79+
type: 'javascript/auto',
80+
use: [
81+
{
82+
loader: require.resolve('file-loader'),
83+
options: {
84+
esModule: true,
85+
name: 'static/media/[name].[hash].[ext]'
86+
}
87+
}
88+
]
89+
});
7590
return config;
7691
},
7792
};

Dnn.AdminExperience/ClientSide/Dnn.React.Common/src/Flag/flag.stories.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,30 @@ import Flag from "./index";
44

55
export default {
66
component: Flag,
7+
render: ({...args}) => <Flag {...args} onClick={action("Clicked")} />
78
};
89

9-
export const WithContent = () => (
10-
<Flag title="Test" culture="en-US" onClick={action("Clicked")} />
11-
);
10+
// export const WithContent = () => (
11+
// <Flag title="Test" culture="en-US" onClick={action("Clicked")} />
12+
// );
13+
14+
export const EnUs = {
15+
args: {
16+
title: "English (United States)",
17+
culture: "en-US",
18+
},
19+
};
20+
21+
export const FrCa = {
22+
args: {
23+
title: "French (Canada)",
24+
culture: "fr-CA",
25+
},
26+
};
27+
28+
export const Missing = {
29+
args: {
30+
title: "Missing Flag",
31+
culture: "ab-CD",
32+
},
33+
};

Dnn.AdminExperience/ClientSide/Dnn.React.Common/src/Flag/index.jsx

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
1-
import React, { Component } from "react";
1+
import React, { useState, useEffect } from "react";
22
import PropTypes from "prop-types";
33

4-
const getUrl = (code) => {
5-
try {
6-
return require(`./img/flags/${code}.png`);
7-
} catch (error) {
8-
return require("./img/flags/none.png");
9-
}
10-
};
11-
12-
const getStyle = (code, isGeneric) => ({
13-
backgroundColor: isGeneric ? "#78BEDB" : "transparent",
4+
const getStyle = (url) => ({
5+
position: "relative",
6+
display: "inline-flex",
7+
alignItems: "center",
8+
justifyContent: "center",
9+
width: "27px",
10+
height: "18px",
11+
marginRight: "5px",
12+
backgroundColor: "transparent",
13+
backgroundImage: url ? `url(${url})` : "none",
1414
backgroundRepeat: "no-repeat",
15-
backgroundImage: isGeneric ? "none" : `url(${getUrl(code)})`,
16-
backgroundPositionY: "50%",
17-
backgroundPositionX: "50%",
15+
backgroundPosition: "center",
16+
backgroundSize: "contain",
1817
color: "#FFF",
19-
textTransform: "uppercase",
20-
display: "inline-block",
21-
marginRight: "5px",
2218
fontWeight: "bold",
23-
width: "27px",
24-
height: "18px",
25-
lineHeight: "18px",
26-
verticalAlign: "middle",
27-
textAlign: "center"
19+
textTransform: "uppercase",
2820
});
2921

30-
class Flag extends Component {
31-
constructor(props) {
32-
super(props);
33-
}
22+
const overlayStyle = {
23+
position: "absolute",
24+
left: 0,
25+
top: 0,
26+
width: "100%",
27+
height: "100%",
28+
display: "flex",
29+
alignItems: "center",
30+
justifyContent: "center",
31+
fontSize: "8px",
32+
fontWeight: "900",
33+
color: "#000",
34+
textTransform: "uppercase",
35+
whiteSpace: "nowrap",
36+
transform: "scale(0.9, 1.1)",
37+
};
38+
39+
function Flag({ culture = "", onClick, title }) {
40+
const [flagUrl, setFlagUrl] = useState(undefined);
41+
const [isFallback, setIsFallback] = useState(false);
42+
43+
useEffect(() => {
44+
try {
45+
setFlagUrl(require(`./img/flags/${culture}.png`).default);
46+
setIsFallback(false);
47+
} catch {
48+
try {
49+
setFlagUrl(require("./img/flags/none.png").default);
50+
setIsFallback(true);
51+
} catch {
52+
setFlagUrl(undefined);
53+
setIsFallback(true);
54+
}
55+
}
56+
}, [culture]);
3457

35-
render() {
36-
const isGeneric = !this.props.culture.includes("-");
37-
return <div
38-
onClick={this.props.onClick}
39-
title={this.props.title}
40-
style={getStyle(this.props.culture, isGeneric)}>{isGeneric ? this.props.culture : ""}</div>;
41-
}
58+
return (
59+
<div onClick={onClick} title={title} style={getStyle(flagUrl)}>
60+
{isFallback && culture ? <div style={overlayStyle}>{culture}</div> : null}
61+
</div>
62+
);
4263
}
4364

4465
Flag.propTypes = {

0 commit comments

Comments
 (0)