Skip to content

Commit b7170cf

Browse files
Leslie NgoLeslie Ngo
Leslie Ngo
authored and
Leslie Ngo
committed
topic edit modal: Add "disabled" prop to ZulipTextButton
Previously, callers of ZulipTextButton haven't needed a "disabled" prop. We're adding one now to 1) prevent users from submitting an empty field in the new topic edit UI and to 2) visually indicate the text button is disabled. It's also generally useful to have a "disabled" prop on a button.
1 parent c35b883 commit b7170cf

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/common/ZulipTextButton.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* @flow strict-local */
2-
import React, { useMemo } from 'react';
2+
import React, { useMemo, useCallback } from 'react';
33
import type { Node } from 'react';
44
import { View } from 'react-native';
55

66
import type { LocalizableReactText } from '../types';
7-
import { BRAND_COLOR, createStyleSheet } from '../styles';
7+
import { BRAND_COLOR, HALF_COLOR, createStyleSheet } from '../styles';
88
import ZulipTextIntl from './ZulipTextIntl';
99
import Touchable from './Touchable';
1010

@@ -76,6 +76,13 @@ type Props = $ReadOnly<{|
7676
*/
7777
label: LocalizableReactText,
7878

79+
/**
80+
* The disabled state: https://material.io/components/buttons#text-button
81+
*
82+
* See section on states.
83+
*/
84+
disabled?: boolean,
85+
7986
onPress: () => void | Promise<void>,
8087
|}>;
8188

@@ -98,21 +105,37 @@ type Props = $ReadOnly<{|
98105
// things like project-specific styles and making any sensible adjustments
99106
// to the interface.
100107
export default function ZulipTextButton(props: Props): Node {
101-
const { variant = 'standard', leftMargin, rightMargin, label, onPress } = props;
108+
const { variant = 'standard', leftMargin, rightMargin, label, onPress, disabled } = props;
102109

103110
const variantStyles = useMemo(() => styleSheetForVariant(variant), [variant]);
104111

112+
const isNotDisabled = disabled !== true;
113+
114+
const disabledGate = useCallback(
115+
onPressCallback => () => {
116+
if (isNotDisabled) {
117+
onPressCallback();
118+
}
119+
},
120+
[isNotDisabled],
121+
);
122+
123+
const buttonDisabledColor = useMemo(
124+
() => ({ color: isNotDisabled ? BRAND_COLOR : HALF_COLOR }),
125+
[isNotDisabled],
126+
);
127+
105128
return (
106129
<Touchable
107130
style={[
108131
variantStyles.touchable,
109132
leftMargin && variantStyles.leftMargin,
110133
rightMargin && variantStyles.rightMargin,
111134
]}
112-
onPress={onPress}
135+
onPress={disabledGate(onPress)}
113136
>
114137
<View style={variantStyles.childOfTouchable}>
115-
<ZulipTextIntl style={variantStyles.text} text={label} />
138+
<ZulipTextIntl style={{ ...variantStyles.text, ...buttonDisabledColor }} text={label} />
116139
</View>
117140
</Touchable>
118141
);

0 commit comments

Comments
 (0)