-
Notifications
You must be signed in to change notification settings - Fork 4k
[TS Migration] Migrate CheckboxWithLabel.js component to TypeScript #32814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arosiclair
merged 5 commits into
Expensify:main
from
JKobrynski:migrateCheckboxWithLabelToTypeScript
Dec 18, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49a272b
migrate CheckboxWithLabel to TypeScript
JKobrynski b5e7179
refactor optional children prop
JKobrynski 9629f75
apply suggested changes
JKobrynski 702f3ee
Merge branch 'main' into migrateCheckboxWithLabelToTypeScript
JKobrynski 3440231
replace deprecated checkbox role
JKobrynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,107 @@ | ||||||||
| import React, {ComponentType, ForwardedRef, useState} from 'react'; | ||||||||
| import {StyleProp, View, ViewStyle} from 'react-native'; | ||||||||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||||||||
| import variables from '@styles/variables'; | ||||||||
| import Checkbox from './Checkbox'; | ||||||||
| import FormHelpMessage from './FormHelpMessage'; | ||||||||
| import PressableWithFeedback from './Pressable/PressableWithFeedback'; | ||||||||
| import Text from './Text'; | ||||||||
|
|
||||||||
| type RequiredLabelProps = | ||||||||
| | { | ||||||||
| /** Text that appears next to check box */ | ||||||||
| label: string; | ||||||||
|
|
||||||||
| /** Component to display for label | ||||||||
| * If label is provided, LabelComponent is not required | ||||||||
| */ | ||||||||
| LabelComponent?: ComponentType; | ||||||||
| } | ||||||||
| | { | ||||||||
| /** Component to display for label */ | ||||||||
| LabelComponent: ComponentType; | ||||||||
|
|
||||||||
| /** Text that appears next to check box | ||||||||
| * If LabelComponent is provided, label is not required | ||||||||
| */ | ||||||||
| label?: string; | ||||||||
| }; | ||||||||
|
|
||||||||
| type CheckboxWithLabelProps = RequiredLabelProps & { | ||||||||
| /** Whether the checkbox is checked */ | ||||||||
| isChecked?: boolean; | ||||||||
|
|
||||||||
| /** Called when the checkbox or label is pressed */ | ||||||||
| onInputChange?: (value?: boolean) => void; | ||||||||
|
|
||||||||
| /** Container styles */ | ||||||||
| style?: StyleProp<ViewStyle>; | ||||||||
|
|
||||||||
| /** Error text to display */ | ||||||||
| errorText?: string; | ||||||||
|
|
||||||||
| /** Value for checkbox. This prop is intended to be set by Form.js only */ | ||||||||
| value?: boolean; | ||||||||
|
|
||||||||
| /** The default value for the checkbox */ | ||||||||
| defaultValue?: boolean; | ||||||||
|
|
||||||||
| /** The ID used to uniquely identify the input in a Form */ | ||||||||
| /* eslint-disable-next-line react/no-unused-prop-types */ | ||||||||
| inputID?: string; | ||||||||
|
|
||||||||
| /** Saves a draft of the input value when used in a form */ | ||||||||
| // eslint-disable-next-line react/no-unused-prop-types | ||||||||
| shouldSaveDraft?: boolean; | ||||||||
|
JKobrynski marked this conversation as resolved.
|
||||||||
|
|
||||||||
| /** An accessibility label for the checkbox */ | ||||||||
| accessibilityLabel?: string; | ||||||||
| }; | ||||||||
|
|
||||||||
| function CheckboxWithLabel( | ||||||||
| {errorText = '', isChecked: isCheckedProp = false, defaultValue = false, onInputChange = () => {}, LabelComponent, label, accessibilityLabel, style, value}: CheckboxWithLabelProps, | ||||||||
| ref: ForwardedRef<View>, | ||||||||
| ) { | ||||||||
| const styles = useThemeStyles(); | ||||||||
| // We need to pick the first value that is strictly a boolean | ||||||||
| // https://github.com/Expensify/App/issues/16885#issuecomment-1520846065 | ||||||||
| const [isChecked, setIsChecked] = useState(() => [value, defaultValue, isCheckedProp].find((item) => typeof item === 'boolean')); | ||||||||
|
|
||||||||
| const toggleCheckbox = () => { | ||||||||
| onInputChange(!isChecked); | ||||||||
| setIsChecked(!isChecked); | ||||||||
| }; | ||||||||
|
|
||||||||
| return ( | ||||||||
| <View style={style}> | ||||||||
| <View style={[styles.flexRow, styles.alignItemsCenter, styles.breakWord]}> | ||||||||
| <Checkbox | ||||||||
| isChecked={isChecked} | ||||||||
| onPress={toggleCheckbox} | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Why did you remove
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a valid prop for |
||||||||
| style={[styles.checkboxWithLabelCheckboxStyle]} | ||||||||
| hasError={!!errorText} | ||||||||
| ref={ref} | ||||||||
| accessibilityLabel={accessibilityLabel ?? label ?? ''} | ||||||||
| /> | ||||||||
| <PressableWithFeedback | ||||||||
| tabIndex={-1} | ||||||||
| accessible={false} | ||||||||
| onPress={toggleCheckbox} | ||||||||
| pressDimmingValue={variables.checkboxLabelActiveOpacity} | ||||||||
| // We want to disable hover dimming | ||||||||
| hoverDimmingValue={variables.checkboxLabelHoverOpacity} | ||||||||
| style={[styles.flexRow, styles.alignItemsCenter, styles.noSelect, styles.w100]} | ||||||||
| wrapperStyle={[styles.ml3, styles.pr2, styles.w100, styles.flexWrap, styles.flexShrink1]} | ||||||||
| > | ||||||||
| {label && <Text style={[styles.ml1]}>{label}</Text>} | ||||||||
| {LabelComponent && <LabelComponent />} | ||||||||
| </PressableWithFeedback> | ||||||||
| </View> | ||||||||
| <FormHelpMessage message={errorText} /> | ||||||||
| </View> | ||||||||
| ); | ||||||||
| } | ||||||||
|
|
||||||||
| CheckboxWithLabel.displayName = 'CheckboxWithLabel'; | ||||||||
|
|
||||||||
| export default React.forwardRef(CheckboxWithLabel); | ||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.