Skip to content

Commit 7122b2d

Browse files
Leslie NgoLeslie Ngo
Leslie Ngo
authored and
Leslie Ngo
committed
topic edit modal: Add "disabled", "isPressHandledWhenDisabled" 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. We're also adding "isPressHandledWhenDisabled" to avoid passing a handler that does nothing when the button is disabled.
1 parent 77a645f commit 7122b2d

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/common/ZulipTextButton.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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

@@ -14,7 +14,7 @@ import Touchable from './Touchable';
1414
// micromanage its styles.
1515
type Variant = 'standard';
1616

17-
const styleSheetForVariant = (variant: Variant) =>
17+
const styleSheetForVariant = (variant: Variant, disabled: boolean) =>
1818
createStyleSheet({
1919
// See https://material.io/components/buttons#specs.
2020
touchable: {
@@ -47,7 +47,7 @@ const styleSheetForVariant = (variant: Variant) =>
4747
// > label isn’t fully capitalized, it should use a different color,
4848
// > style, or layout from other text.
4949
textTransform: 'uppercase',
50-
color: BRAND_COLOR,
50+
color: disabled ? HALF_COLOR : BRAND_COLOR,
5151

5252
textAlign: 'center',
5353
textAlignVertical: 'center',
@@ -76,6 +76,18 @@ type Props = $ReadOnly<{|
7676
*/
7777
label: LocalizableReactText,
7878

79+
/**
80+
* True just if the button is in the disabled state.
81+
* https://material.io/design/interaction/states.html#disabled
82+
*/
83+
disabled?: boolean,
84+
85+
/**
86+
* Whether `onPress` is used even when
87+
* `disabled` is true.
88+
*/
89+
isPressHandledWhenDisabled?: boolean,
90+
7991
onPress: () => void | Promise<void>,
8092
|}>;
8193

@@ -98,9 +110,20 @@ type Props = $ReadOnly<{|
98110
// things like project-specific styles and making any sensible adjustments
99111
// to the interface.
100112
export default function ZulipTextButton(props: Props): Node {
101-
const { variant = 'standard', leftMargin, rightMargin, label, onPress } = props;
113+
const {
114+
variant = 'standard',
115+
leftMargin,
116+
rightMargin,
117+
label,
118+
onPress,
119+
disabled = false,
120+
isPressHandledWhenDisabled = false,
121+
} = props;
102122

103-
const variantStyles = useMemo(() => styleSheetForVariant(variant), [variant]);
123+
const variantStyles = useMemo(
124+
() => styleSheetForVariant(variant, disabled === true),
125+
[variant, disabled],
126+
);
104127

105128
return (
106129
<Touchable
@@ -109,7 +132,7 @@ export default function ZulipTextButton(props: Props): Node {
109132
leftMargin && variantStyles.leftMargin,
110133
rightMargin && variantStyles.rightMargin,
111134
]}
112-
onPress={onPress}
135+
onPress={disabled && !isPressHandledWhenDisabled ? undefined : onPress}
113136
>
114137
<View style={variantStyles.childOfTouchable}>
115138
<ZulipTextIntl style={variantStyles.text} text={label} />

0 commit comments

Comments
 (0)