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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
import * as React from 'react';
import { Button, Component, Styles, Text, Types, UserInterface, View } from 'reactxp';
import { colors } from '../../config.json';
import styles from './AppButtonStyles';
import ImageView from './ImageView';
const ButtonContext = React.createContext({
textAdjustment: 0,
textRef: React.createRef<PrivateLabel>(),
});
interface ILabelProps {
children?: React.ReactText;
}
interface IPrivateLabelProps {
textAdjustment: number;
children?: React.ReactText;
}
class PrivateLabel extends Component<IPrivateLabelProps> {
public render() {
const { textAdjustment, children } = this.props;
const textAdjustmentStyle = Styles.createViewStyle(
{
paddingRight: textAdjustment > 0 ? textAdjustment : 0,
paddingLeft: textAdjustment < 0 ? Math.abs(textAdjustment) : 0,
},
false,
);
return (
<View style={[styles.labelContainer, textAdjustmentStyle]}>
<Text style={styles.label}>{children}</Text>
</View>
);
}
}
export class Label extends Component<ILabelProps> {
public render() {
return (
<ButtonContext.Consumer>
{(context) => (
<PrivateLabel ref={context.textRef} textAdjustment={context.textAdjustment}>
{this.props.children}
</PrivateLabel>
)}
</ButtonContext.Consumer>
);
}
}
interface IIconProps {
source: string;
width?: number;
height?: number;
}
export class Icon extends Component<IIconProps> {
public render() {
return (
<ImageView
source={this.props.source}
width={this.props.width}
height={this.props.height}
tintColor={colors.white}
/>
);
}
}
interface IProps {
children?: React.ReactNode;
style?: Types.ButtonStyleRuleSet;
disabled?: boolean;
onPress?: () => void;
}
interface IState {
hovered: boolean;
textAdjustment: number;
}
class BaseButton extends Component<IProps, IState> {
public state: IState = {
hovered: false,
textAdjustment: 0,
};
private containerRef = React.createRef<View>();
private textViewRef = React.createRef<PrivateLabel>();
public componentDidMount() {
this.forceUpdateTextAdjustment();
}
public render() {
const { children, style, ...otherProps } = this.props;
return (
<ButtonContext.Provider
value={{
textAdjustment: this.state.textAdjustment,
textRef: this.textViewRef,
}}>
<Button
{...otherProps}
style={[styles.common, this.backgroundStyle(), style]}
onHoverStart={this.onHoverStart}
onHoverEnd={this.onHoverEnd}>
<View style={styles.content} ref={this.containerRef} onLayout={this.onLayout}>
{React.Children.map(children, (child) =>
typeof child === 'string' ? <Label>{child as string}</Label> : child,
)}
</View>
</Button>
</ButtonContext.Provider>
);
}
protected backgroundStyle = (): Types.ButtonStyleRuleSet => {
throw new Error('Implement backgroundStyle in subclasses.');
};
protected onHoverStart = () => (!this.props.disabled ? this.setState({ hovered: true }) : null);
protected onHoverEnd = () => (!this.props.disabled ? this.setState({ hovered: false }) : null);
private async forceUpdateTextAdjustment() {
const containerView = this.containerRef.current;
if (containerView) {
const containerLayout = await UserInterface.measureLayoutRelativeToAncestor(
containerView,
this,
);
this.updateTextAdjustment(containerLayout);
}
}
private async updateTextAdjustment(containerLayout: Types.LayoutInfo) {
const labelView = this.textViewRef.current;
if (labelView) {
// calculate the title layout frame
const labelLayout = await UserInterface.measureLayoutRelativeToAncestor(labelView, this);
// calculate the remaining space at the right hand side
const trailingSpace = containerLayout.width - (labelLayout.x + labelLayout.width);
// calculate text adjustment
const textAdjustment = labelLayout.x - trailingSpace;
// re-render the view with the new text adjustment if it changed
if (this.state.textAdjustment !== textAdjustment) {
this.setState({ textAdjustment });
}
}
}
private onLayout = async (containerLayout: Types.ViewOnLayoutEvent) => {
this.updateTextAdjustment(containerLayout);
};
}
export class RedButton extends BaseButton {
protected backgroundStyle = () => (this.state.hovered ? styles.redHover : styles.red);
}
export class GreenButton extends BaseButton {
protected backgroundStyle = () => (this.state.hovered ? styles.greenHover : styles.green);
}
export class BlueButton extends BaseButton {
protected backgroundStyle = () => (this.state.hovered ? styles.blueHover : styles.blue);
}
export class TransparentButton extends BaseButton {
protected backgroundStyle = () =>
this.state.hovered ? styles.transparentHover : styles.transparent;
}
export class RedTransparentButton extends BaseButton {
protected backgroundStyle = () =>
this.state.hovered ? styles.redTransparentHover : styles.redTransparent;
}
|