-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDifferenceNavigation.tsx
More file actions
138 lines (129 loc) · 3.99 KB
/
DifferenceNavigation.tsx
File metadata and controls
138 lines (129 loc) · 3.99 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React from "react";
import { DifferenceNavigationProps } from "./DifferenceNavigation.types";
import { Button } from "../Button";
import { titleCase } from "../../utils/TitleCase";
import { Label } from "../Label";
const DifferenceNavigation: React.FC<DifferenceNavigationProps> = ({
differenceId,
setDifferenceFocus,
totalDifferences,
keyword = "variation",
plural = "variations",
buttonColour,
buttonTextColour,
buttonShadowColour,
buttonHoverColour,
buttonHoverTextColour,
}) => {
if (totalDifferences === 0) {
return (
<p className="govuk-body govuk-!-font-size-20 govuk-!-text-align-centre">
No {`${plural}`} found
</p>
);
}
// CSS custom properties set directly on each <button> element via Button's
// ...attributes spread, so they sit on the same element that
// .app-difference-navigation-button targets — no inheritance from a parent
// div needed. Only written when a prop is explicitly provided.
const inlineVars = {
...(buttonColour && { "--diff-nav-button-colour": buttonColour }),
...(buttonTextColour && {
"--diff-nav-button-text-colour": buttonTextColour,
}),
...(buttonShadowColour && {
"--diff-nav-button-shadow-colour": buttonShadowColour,
}),
...(buttonHoverColour && {
"--diff-nav-button-hover-colour": buttonHoverColour,
}),
...(buttonHoverTextColour && {
"--diff-nav-button-hover-text-colour": buttonHoverTextColour,
}),
} as React.CSSProperties;
const hasInlineVars = Object.keys(inlineVars).length > 0;
const isPreviousDisabled = differenceId <= 1;
const isNextDisabled = differenceId === totalDifferences;
const renderButton = (
id: string,
onClick: () => void,
disabled: boolean,
content: React.ReactNode,
) => (
<Button
id={id}
onClick={onClick}
data-testid={id}
disabled={disabled}
className="app-difference-navigation-button"
{...(hasInlineVars && { style: inlineVars })}
>
{content}
</Button>
);
return (
<div className="govuk-grid-row">
<div className="govuk-grid-column-one-third">
<div className="govuk-!-text-align-left">
{renderButton(
`previous-${keyword}`,
() => setDifferenceFocus(differenceId - 1),
isPreviousDisabled,
<>
{" "}
<svg
className="govuk-button__start-icon back-button"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
aria-hidden="true"
focusable="false"
transform="rotate(180)"
>
<path
fill="currentColor"
d="M8.122 24l-4.122-4 8-8-8-8 4.122-4 11.878 12z"
/>
</svg>
Previous{" "}
</>,
)}
</div>
</div>
<div className="govuk-grid-column-one-third">
<Label className="govuk-!-text-align-centre">
{titleCase(keyword)} {differenceId} of {totalDifferences}
</Label>
</div>
<div className="govuk-grid-column-one-third">
<div className="govuk-!-text-align-right">
{renderButton(
`next-${keyword}`,
() => setDifferenceFocus(differenceId + 1),
isNextDisabled,
<>
{" "}
Next
<svg
className="govuk-button__start-icon back-button"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
aria-hidden="true"
focusable="false"
>
<path
fill="currentColor"
d="M8.122 24l-4.122-4 8-8-8-8 4.122-4 11.878 12z"
/>
</svg>
</>,
)}
</div>
</div>
</div>
);
};
export default DifferenceNavigation;