Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 128 additions & 21 deletions ActionButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
View,
Animated,
TouchableOpacity,
findNodeHandle,
UIManager,
Platform,
AccessibilityInfo,
} from "react-native";
import ActionButtonItem from "./ActionButtonItem";
import {
Expand All @@ -14,18 +18,52 @@ import {
getTouchableComponent,
isAndroid,
touchableBackground,
DEFAULT_ACTIVE_OPACITY
DEFAULT_ACTIVE_OPACITY,
} from "./shared";

//////////////////////
// HELPER FUNCTIONS
//////////////////////

const focusOnView = (ref) => {
if (!ref) {
console.warn('ref is null');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this console.warn should stay, witch I think it could. I think that the error message should be better then this.

return;
}
const reactTag = findNodeHandle(ref);

Platform.OS === 'android' ? UIManager.sendAccessibilityEvent(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the isAndroid-method witch is imported on line line 19 instead of this

reactTag,
8
) : AccessibilityInfo.setAccessibilityFocus(reactTag)
};

const filterActionButtons = (children) => {
const actionButtons = !Array.isArray(children) ? [children] : children;
return actionButtons.filter(actionButton => (typeof actionButton === 'object'));
};

export default class ActionButton extends Component {
constructor(props) {
super(props);

const {
children,
} = props;

this.state = {
resetToken: props.resetToken,
active: props.active
active: props.active,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I'm on #teamtrailingcomma ❤️

};

const actionButtons = filterActionButtons(children);

this.refIndexes = [];
actionButtons.forEach((button, idx) => {
this[`actionButton${idx}Ref`] = React.createRef();
this.refIndexes.push(idx);
});

this.anim = new Animated.Value(props.active ? 1 : 0);
this.timeout = null;
}
Expand All @@ -34,37 +72,65 @@ export default class ActionButton extends Component {
this.mounted = true;
}

componentWillUnmount() {
this.mounted = false;
clearTimeout(this.timeout);
}

componentWillReceiveProps(nextProps) {
if (nextProps.resetToken !== this.state.resetToken) {
if (nextProps.active === false && this.state.active === true) {
if (this.props.onReset) this.props.onReset();
const {
resetToken,
active,
} = this.state;

const {
onReset,
} = this.props;

if (nextProps.resetToken !== resetToken) {
if (nextProps.active === false && active === true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick:
The === true and === false is redundant.

Suggested change
if (nextProps.active === false && active === true) {
if (!nextProps.active && active) {

Also, personally i'm not a fan of nested if statements

if (onReset) {
onReset();
}
Animated.spring(this.anim, { toValue: 0 }).start();
setTimeout(
() =>
this.setState({ active: false, resetToken: nextProps.resetToken }),
250
);
setTimeout(() => {
this.setState({
active: false,
resetToken: nextProps.resetToken,
});
}, 250);
return;
}

if (nextProps.active === true && this.state.active === false) {
if (nextProps.active === true && active === false) {
Animated.spring(this.anim, { toValue: 1 }).start();
this.setState({ active: true, resetToken: nextProps.resetToken });
return;
}

this.setState({
resetToken: nextProps.resetToken,
active: nextProps.active
active: nextProps.active,
});
}
}

componentDidUpdate(prevProps, prevState) {
const {
active,
} = this.state;

if (prevState.active !== active && active) {
setTimeout(() => {
this.focusOnTopActionButton();
}, 500);

setTimeout(() => {
this.announceActionButtons();
}, 3000);
}
}

componentWillUnmount() {
this.mounted = false;
clearTimeout(this.timeout);
}

//////////////////////
// STYLESHEET GETTERS
//////////////////////
Expand Down Expand Up @@ -93,6 +159,45 @@ export default class ActionButton extends Component {
];
}

//////////////////////
// ACCESSIBILITY METHODS
//////////////////////

announceActionButtons() {
const {
children,
announceActionsLabel,
} = this.props;

const actionButtons = filterActionButtons(children);

const actionButtonsAccessibilityAnnouncement = actionButtons.reduce((acc, actionButton) => {
return `${acc} ${actionButton.props.accessibilityLabel}, `;
}, announceActionsLabel);

if (actionButtons.length && Platform.OS === 'ios') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick:
Maybe you should add an isIOS-method to shared.js, that checks the that Platform.OS === 'ios', and use is here

AccessibilityInfo.announceForAccessibility(actionButtonsAccessibilityAnnouncement);
}
}

focusOnTopActionButton() {
const {
active,
} = this.state;

const actionButtonRefs = this.refIndexes.reduce((acc, refIdx) => {
const buttonRef = this[`actionButton${refIdx}Ref`].current;
if (!!buttonRef) {
acc = [...acc, buttonRef];
}
return acc;
}, []);

if (active && actionButtonRefs.length) {
focusOnView(actionButtonRefs[0]);
}
}

//////////////////////
// RENDER METHODS
//////////////////////
Expand Down Expand Up @@ -142,6 +247,7 @@ export default class ActionButton extends Component {
);
}


_renderMainButton() {
const animatedViewStyle = {
transform: [
Expand Down Expand Up @@ -261,9 +367,7 @@ export default class ActionButton extends Component {

if (!this.state.active) return null;

let actionButtons = !Array.isArray(children) ? [children] : children;

actionButtons = actionButtons.filter( actionButton => (typeof actionButton == 'object') )
const actionButtons = filterActionButtons(children);

const actionStyle = {
flex: 1,
Expand All @@ -284,6 +388,7 @@ export default class ActionButton extends Component {
anim={this.anim}
{...this.props}
{...ActionButton.props}
buttonRef={this[`actionButton${idx}Ref`]}
parentSize={this.props.size}
btnColor={this.props.btnOutRange}
onPress={() => {
Expand Down Expand Up @@ -386,6 +491,7 @@ ActionButton.propTypes = {

testID: PropTypes.string,
accessibilityLabel: PropTypes.string,
announceActionsLabel: PropTypes.string,
accessible: PropTypes.bool
};

Expand Down Expand Up @@ -417,6 +523,7 @@ ActionButton.defaultProps = {
nativeFeedbackRippleColor: "rgba(255,255,255,0.75)",
testID: undefined,
accessibilityLabel: undefined,
announceActionsLabel: 'Available actions from top to bottom: ',
accessible: undefined
};

Expand All @@ -434,4 +541,4 @@ const styles = StyleSheet.create({
fontSize: 24,
backgroundColor: "transparent"
}
});
});
Loading