-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: TextField component #4909
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
Open
wonderlul
wants to merge
26
commits into
callstack:v6
Choose a base branch
from
wonderlul:feat/TextField-v6
base: v6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
2ea4a30
feat: text field component
wonderlul 9de86ee
fix: code review refactor
wonderlul 4077d91
chore: example of text field
wonderlul d7d6618
chore: duplicated code removal
wonderlul 21e37eb
feat: default error icon
wonderlul d7c559d
fix: text field input opacity
wonderlul f352bfe
chore: removed unnecessary pressableStyles from filled variant
wonderlul b011a14
fix: outline padding top multiline
wonderlul 4400b52
feat: reanimated as peer dependency
wonderlul 2e03a67
chore: migration to reanimated api
wonderlul 4fda428
feat: animated placeholder opacity
wonderlul a176ac4
fix: outlined multiline icons vertical alignment
wonderlul 41a67f3
fix: counter is pushed to the trailing edge when it’s alone
wonderlul b3481b3
fix: error icon size
wonderlul 16c6906
feat: compound error and disabled states
wonderlul 16c2f86
fix: border radius clip for android
wonderlul f56b33d
chore: type annotations
wonderlul cf1d95b
feat: system font scaling adjustment
wonderlul 4f1b768
fix: icons fixed height
wonderlul 98e7495
feat: api unification for status handling
wonderlul f50860c
feat: outline style override
wonderlul 29f2b15
fix: filled outline active color
wonderlul 787abdb
chore: prop table update
wonderlul fb659ec
chore: drop dollar notation for styles and introduced stylesheet api
wonderlul 1755d64
chore: code composition refactor wip
wonderlul 73642ae
chore: code composition refactor
wonderlul 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 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 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,244 @@ | ||
| import * as React from 'react'; | ||
| import { | ||
| StyleSheet, | ||
| TextInput, | ||
| View, | ||
| type TextStyle, | ||
| type ViewStyle, | ||
| } from 'react-native'; | ||
|
|
||
| import { | ||
| Divider, | ||
| List, | ||
| Switch, | ||
| Text, | ||
| TextField, | ||
| TouchableRipple, | ||
| type TextFieldAccessoryProps, | ||
| type TextFieldVariant, | ||
| } from 'react-native-paper'; | ||
|
|
||
| import { useExampleTheme } from '../hooks/useExampleTheme'; | ||
| import ScreenWrapper from '../ScreenWrapper'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Types | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| type DemoControls = { | ||
| error: boolean; | ||
| disabled: boolean; | ||
| leadingIcon: boolean; | ||
| trailingIcon: boolean; | ||
| counter: boolean; | ||
| showPrefix: boolean; | ||
| showSuffix: boolean; | ||
| multiline: boolean; | ||
| }; | ||
|
|
||
| type DemoModifiers = { | ||
| label: string; | ||
| helperText: string; | ||
| placeholder: string; | ||
| prefix: string; | ||
| suffix: string; | ||
| }; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // TextFieldDemo | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| type TextFieldDemoProps = { | ||
| variant: TextFieldVariant; | ||
| }; | ||
|
|
||
| const TextFieldDemo = ({ variant }: TextFieldDemoProps) => { | ||
| const theme = useExampleTheme(); | ||
|
|
||
| const [value, setValue] = React.useState(''); | ||
|
|
||
| const [controls, setControls] = React.useState<DemoControls>({ | ||
| error: false, | ||
| disabled: false, | ||
| leadingIcon: false, | ||
| trailingIcon: false, | ||
| counter: false, | ||
| showPrefix: false, | ||
| showSuffix: false, | ||
| multiline: false, | ||
| }); | ||
|
|
||
| const [modifiers, setModifiers] = React.useState<DemoModifiers>({ | ||
| label: 'Label', | ||
| helperText: 'Supporting text', | ||
| placeholder: 'Placeholder', | ||
| prefix: '$', | ||
| suffix: '/100', | ||
| }); | ||
|
|
||
| const toggleControl = (key: keyof DemoControls) => | ||
| setControls((prev) => ({ ...prev, [key]: !prev[key] })); | ||
|
|
||
| const setModifier = (key: keyof DemoModifiers, text: string) => | ||
| setModifiers((prev) => ({ ...prev, [key]: text })); | ||
|
|
||
| const LeadingIcon = React.useCallback( | ||
| (props: TextFieldAccessoryProps) => ( | ||
| <TextField.Icon {...props} icon="magnify" /> | ||
| ), | ||
| [] | ||
| ); | ||
|
|
||
| const TrailingIcon = React.useCallback( | ||
| (props: TextFieldAccessoryProps) => ( | ||
| <TextField.Icon {...props} icon="close" onPress={() => setValue('')} /> | ||
| ), | ||
| [] | ||
| ); | ||
|
|
||
| const inputColor = theme.colors.onSurfaceVariant; | ||
| const borderColor = theme.colors.outlineVariant; | ||
|
|
||
| const modifierInputStyle: TextStyle = { | ||
| flex: 1, | ||
| color: inputColor, | ||
| fontSize: 14, | ||
| paddingVertical: 4, | ||
| borderBottomWidth: StyleSheet.hairlineWidth, | ||
| borderBottomColor: borderColor, | ||
| }; | ||
|
|
||
| const SWITCH_CONTROLS: { label: string; key: keyof DemoControls }[] = [ | ||
| { label: 'Error', key: 'error' }, | ||
| { label: 'Disabled', key: 'disabled' }, | ||
| { label: 'Leading icon', key: 'leadingIcon' }, | ||
| { label: 'Trailing icon', key: 'trailingIcon' }, | ||
| { label: 'Counter', key: 'counter' }, | ||
| { label: 'Prefix', key: 'showPrefix' }, | ||
| { label: 'Suffix', key: 'showSuffix' }, | ||
| { label: 'Multiline', key: 'multiline' }, | ||
| ]; | ||
|
|
||
| const MODIFIER_FIELDS: { label: string; key: keyof DemoModifiers }[] = [ | ||
| { label: 'Label', key: 'label' }, | ||
| { label: 'Helper', key: 'helperText' }, | ||
| { label: 'Placeholder', key: 'placeholder' }, | ||
| { label: 'Prefix', key: 'prefix' }, | ||
| { label: 'Suffix', key: 'suffix' }, | ||
| ]; | ||
|
|
||
| return ( | ||
| <View style={styles.demoContainer}> | ||
| {/* Live TextField */} | ||
| <TextField | ||
| variant={variant} | ||
| label={modifiers.label || undefined} | ||
| placeholder={modifiers.placeholder || undefined} | ||
| supportingText={modifiers.helperText || undefined} | ||
| error={controls.error} | ||
| editable={!controls.disabled} | ||
| value={value} | ||
| onChangeText={setValue} | ||
| multiline={controls.multiline} | ||
| counter={controls.counter} | ||
| maxLength={controls.counter ? 100 : undefined} | ||
| prefix={controls.showPrefix ? modifiers.prefix : undefined} | ||
| suffix={controls.showSuffix ? modifiers.suffix : undefined} | ||
| StartAccessory={controls.leadingIcon ? LeadingIcon : undefined} | ||
| EndAccessory={controls.trailingIcon ? TrailingIcon : undefined} | ||
| /> | ||
|
|
||
| <Divider style={styles.divider} /> | ||
|
|
||
| {/* Controls */} | ||
| <List.Subheader style={styles.subheader}>Controls</List.Subheader> | ||
| {SWITCH_CONTROLS.map(({ label, key }) => ( | ||
| <TouchableRipple key={key} onPress={() => toggleControl(key)}> | ||
| <View style={styles.switchRow}> | ||
| <Text variant="bodyMedium">{label}</Text> | ||
| <View pointerEvents="none"> | ||
| <Switch value={controls[key]} /> | ||
| </View> | ||
| </View> | ||
| </TouchableRipple> | ||
| ))} | ||
|
|
||
| <Divider style={styles.divider} /> | ||
|
|
||
| {/* Modifiers */} | ||
| <List.Subheader style={styles.subheader}>Modifiers</List.Subheader> | ||
| {MODIFIER_FIELDS.map(({ label, key }) => ( | ||
| <View key={key} style={styles.modifierRow}> | ||
| <Text variant="bodyMedium" style={styles.modifierLabel}> | ||
| {label} | ||
| </Text> | ||
| <TextInput | ||
| value={modifiers[key]} | ||
| onChangeText={(text) => setModifier(key, text)} | ||
| style={modifierInputStyle} | ||
| placeholderTextColor={theme.colors.outline} | ||
| placeholder={`Enter ${label.toLowerCase()}…`} | ||
| /> | ||
| </View> | ||
| ))} | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // TextFieldExample | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const TextFieldExample = () => { | ||
| return ( | ||
| <ScreenWrapper contentContainerStyle={styles.container}> | ||
| <List.Section title="Filled"> | ||
| <TextFieldDemo variant="filled" /> | ||
| </List.Section> | ||
| <List.Section title="Outlined"> | ||
| <TextFieldDemo variant="outlined" /> | ||
| </List.Section> | ||
| </ScreenWrapper> | ||
| ); | ||
| }; | ||
|
|
||
| TextFieldExample.title = 'TextField'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Styles | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| paddingHorizontal: 16, | ||
| paddingVertical: 8, | ||
| } satisfies ViewStyle, | ||
| demoContainer: { | ||
| gap: 4, | ||
| } satisfies ViewStyle, | ||
| divider: { | ||
| marginVertical: 8, | ||
| } satisfies ViewStyle, | ||
| subheader: { | ||
| paddingHorizontal: 0, | ||
| } satisfies TextStyle, | ||
| switchRow: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| justifyContent: 'space-between', | ||
| paddingVertical: 8, | ||
| paddingHorizontal: 8, | ||
| } satisfies ViewStyle, | ||
| modifierRow: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| gap: 12, | ||
| paddingVertical: 8, | ||
| paddingHorizontal: 8, | ||
| } satisfies ViewStyle, | ||
| modifierLabel: { | ||
| width: 80, | ||
| } satisfies TextStyle, | ||
| }); | ||
|
|
||
| export default TextFieldExample; | ||
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 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 @@ | ||
| require('react-native-reanimated').setUpTests(); |
Oops, something went wrong.
Oops, something went wrong.
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.