summaryrefslogtreecommitdiff
path: root/app/components/text-field/text-field.story.tsx
blob: 20c73a4d51663b818edaf67b08123527c14517f6 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* 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) => <StoryScreen>{fn()}</StoryScreen>)
  .add('Labelling', () => (
    <Story>
      <UseCase
        text="Normal text"
        usage="Use placeholder and label to set the text."
      >
        <State initial={{ value: '' }}>
          {({ state, setState }) => (
            <TextField
              onChangeText={(value) => setState({ value })}
              value={state.value}
              label="Name"
              placeholder="omg your name"
            />
          )}
        </State>
      </UseCase>

      <UseCase
        text="i18n text"
        usage="Use placeholderTx and labelTx for i18n lookups"
      >
        <State initial={{ value: '' }}>
          {({ state, setState }) => (
            <TextField
              onChangeText={(value) => setState({ value })}
              value={state.value}
              placeholderTx="storybook.placeholder"
              labelTx="storybook.field"
            />
          )}
        </State>
      </UseCase>
    </Story>
  ))
  .add('Style Overrides', () => (
    <Story>
      <UseCase
        noPad
        text="Container Styles"
        usage="Useful for applying margins when laying out a form to remove padding if the form brings its own."
      >
        <State initial={{ value: 'Inigo' }}>
          {({ state, setState }) => (
            <TextField
              onChangeText={(value) => setState({ value })}
              value={state.value}
              label="First Name"
              style={{ paddingTop: 0, paddingHorizontal: 40 }}
            />
          )}
        </State>
        <State initial={{ value: 'Montoya' }}>
          {({ state, setState }) => (
            <TextField
              onChangeText={(value) => setState({ value })}
              value={state.value}
              label="Last Name"
              style={{ paddingBottom: 0 }}
            />
          )}
        </State>
      </UseCase>
      <UseCase
        text="Input Styles"
        usage="Useful for 1-off exceptions.  Try to steer towards presets for this kind of thing."
      >
        <State initial={{ value: 'fancy colour' }}>
          {({ state, setState }) => (
            <TextField
              onChangeText={(value) => setState({ value })}
              value={state.value}
              label="Name"
              inputStyle={{
                backgroundColor: 'rebeccapurple',
                color: 'white',
                padding: 40,
                borderWidth: 10,
                borderRadius: 4,
                borderColor: 'hotpink',
              }}
            />
          )}
        </State>
        <Text text="* attention designers:  i am so sorry" preset="secondary" />
      </UseCase>

      <UseCase
        text="Style array"
        usage="Useful for 1-off exceptions, but using style arrays."
      >
        <State initial={{ value: 'fancy colour' }}>
          {({ state, setState }) => (
            <TextField
              onChangeText={(value) => setState({ value })}
              value={state.value}
              label="Name"
              style={styleArray}
              inputStyle={inputStyleArray}
            />
          )}
        </State>
        <Text text="* attention designers:  i am so sorry" preset="secondary" />
      </UseCase>
    </Story>
  ))
  .add('Ref Forwarding', () => (
    <Story>
      <UseCase text="Ref Forwarding" usage="">
        <State initial={{ value: 'fancy colour' }}>
          {({ state, setState }) => (
            <TextField
              onChangeText={(value) => 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!')
                }
              }}
            />
          )}
        </State>
        <Text text="* attention designers:  i am so sorry" preset="secondary" />
      </UseCase>
    </Story>
  ))