From 5f4611d65e40eae3ca6191a15f68d69ea5a1c4cb Mon Sep 17 00:00:00 2001 From: Kirill Rogovoy Date: Tue, 20 Jul 2021 21:24:52 +0300 Subject: WIP --- .../components/text-field/text-field.story.tsx | 159 +++++++++++++++++++++ app_expo/components/text-field/text-field.tsx | 98 +++++++++++++ 2 files changed, 257 insertions(+) create mode 100644 app_expo/components/text-field/text-field.story.tsx create mode 100644 app_expo/components/text-field/text-field.tsx (limited to 'app_expo/components/text-field') diff --git a/app_expo/components/text-field/text-field.story.tsx b/app_expo/components/text-field/text-field.story.tsx new file mode 100644 index 0000000..5f4d408 --- /dev/null +++ b/app_expo/components/text-field/text-field.story.tsx @@ -0,0 +1,159 @@ +/* eslint-disable react-native/no-inline-styles */ +/* eslint-disable react-native/no-color-literals */ + +import * as React from 'react' +import { storiesOf } from '@storybook/react-native' +import { StoryScreen, Story, UseCase } from '../../../storybook/views' +import { Text, TextField } from '../' +import { State } from 'react-powerplug' +import { ViewStyle, TextStyle, Alert } from 'react-native' + +declare let module + +const styleArray: ViewStyle[] = [{ paddingHorizontal: 30 }, { borderWidth: 30 }] + +const inputStyleArray: TextStyle[] = [ + { + backgroundColor: 'rebeccapurple', + color: 'white', + padding: 40, + }, + { + borderWidth: 10, + borderRadius: 4, + borderColor: '#7fff00', + }, +] +let alertWhenFocused = true + +storiesOf('TextField', module) + .addDecorator((fn) => {fn()}) + .add('Labelling', () => ( + + + + {({ state, setState }) => ( + setState({ value })} + value={state.value} + label="Name" + placeholder="omg your name" + /> + )} + + + + + + {({ state, setState }) => ( + setState({ value })} + value={state.value} + placeholderTx="storybook.placeholder" + labelTx="storybook.field" + /> + )} + + + + )) + .add('Style Overrides', () => ( + + + + {({ state, setState }) => ( + setState({ value })} + value={state.value} + label="First Name" + style={{ paddingTop: 0, paddingHorizontal: 40 }} + /> + )} + + + {({ state, setState }) => ( + setState({ value })} + value={state.value} + label="Last Name" + style={{ paddingBottom: 0 }} + /> + )} + + + + + {({ state, setState }) => ( + setState({ value })} + value={state.value} + label="Name" + inputStyle={{ + backgroundColor: 'rebeccapurple', + color: 'white', + padding: 40, + borderWidth: 10, + borderRadius: 4, + borderColor: 'hotpink', + }} + /> + )} + + + + + + + {({ state, setState }) => ( + setState({ value })} + value={state.value} + label="Name" + style={styleArray} + inputStyle={inputStyleArray} + /> + )} + + + + + )) + .add('Ref Forwarding', () => ( + + + + {({ state, setState }) => ( + setState({ value })} + value={state.value} + label="Name" + inputStyle={{ + backgroundColor: 'rebeccapurple', + color: 'white', + padding: 40, + borderWidth: 10, + borderRadius: 4, + borderColor: 'hotpink', + }} + forwardedRef={(ref) => ref} + onFocus={() => { + if (alertWhenFocused) { + // Prevent text field focus from being repeatedly triggering alert + alertWhenFocused = false + Alert.alert('Text field focuesed with forwarded ref!') + } + }} + /> + )} + + + + + )) diff --git a/app_expo/components/text-field/text-field.tsx b/app_expo/components/text-field/text-field.tsx new file mode 100644 index 0000000..1d56b95 --- /dev/null +++ b/app_expo/components/text-field/text-field.tsx @@ -0,0 +1,98 @@ +import React from 'react' +import { StyleProp, TextInput, TextInputProps, TextStyle, View, ViewStyle } from 'react-native' +import { color, spacing, typography } from '../../theme' +import { translate, TxKeyPath } from '../../i18n' +import { Text } from '../text/text' + +// the base styling for the container +const CONTAINER: ViewStyle = { + paddingVertical: spacing[3], +} + +// the base styling for the TextInput +const INPUT: TextStyle = { + fontFamily: typography.primary, + color: color.text, + minHeight: 44, + fontSize: 18, + backgroundColor: color.palette.white, +} + +// currently we have no presets, but that changes quickly when you build your app. +const PRESETS: { [name: string]: ViewStyle } = { + default: {}, +} + +export interface TextFieldProps extends TextInputProps { + /** + * The placeholder i18n key. + */ + placeholderTx?: TxKeyPath + + /** + * The Placeholder text if no placeholderTx is provided. + */ + placeholder?: string + + /** + * The label i18n key. + */ + labelTx?: TxKeyPath + + /** + * The label text if no labelTx is provided. + */ + label?: string + + /** + * Optional container style overrides useful for margins & padding. + */ + style?: StyleProp + + /** + * Optional style overrides for the input. + */ + inputStyle?: StyleProp + + /** + * Various look & feels. + */ + preset?: keyof typeof PRESETS + + forwardedRef?: any +} + +/** + * A component which has a label and an input together. + */ +export function TextField(props: TextFieldProps) { + const { + placeholderTx, + placeholder, + labelTx, + label, + preset = 'default', + style: styleOverride, + inputStyle: inputStyleOverride, + forwardedRef, + ...rest + } = props + + const containerStyles = [CONTAINER, PRESETS[preset], styleOverride] + const inputStyles = [INPUT, inputStyleOverride] + const actualPlaceholder = placeholderTx ? translate(placeholderTx) : placeholder + + return ( + + + + + ) +} -- cgit v1.2.3