-
-
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
8
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
8 commits
Select commit
Hold shift + click to select a range
a1a20fd
feat: text field component
wonderlul 728820a
fix: code review refactor
wonderlul 3adc35e
chore: example of text field
wonderlul 947ef20
chore: duplicated code removal
wonderlul 8a2b1bf
feat: default error icon
wonderlul 70e40da
fix: text field input opacity
wonderlul 1a50b6a
chore: removed unnecessary pressableStyles from filled variant
wonderlul ec05750
fix: outline padding top multiline
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,249 @@ | ||
| 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 status = controls.error | ||
| ? 'error' | ||
| : controls.disabled | ||
| ? 'disabled' | ||
| : undefined; | ||
|
|
||
| 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} | ||
| status={status} | ||
| 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; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used this example to check against the specs and native implementation but had to extend it locally to check all states.
It would be nice to have an example for disabled fields that contain accessories and suffix/prefix or even some buttons that toggles states for a more compact example page.