Skip to content

Commit

Permalink
feat: Add Phone field
Browse files Browse the repository at this point in the history
  • Loading branch information
lhguerra committed Mar 22, 2020
1 parent 450b129 commit 79fefd9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"prettier": "^1.19.1"
},
"dependencies": {
"@react-navigation/native": "^5.0.9"
"@react-navigation/native": "^5.0.9",
"react-native-masked-text": "^1.13.0"
}
}
55 changes: 55 additions & 0 deletions src/components/Form/PhoneField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react'
import { TextInputMask } from 'react-native-masked-text'
import { View } from 'react-native'

import TextInput from '../TextInput'
import Label from '../Label'
import Spacer from '../Spacer'
import Footnote from '../Footnote'
import { useTheme } from '../../lib/theme'

const PhoneInput = props => (
<TextInputMask
type={'cel-phone'}
options={{
maskType: 'BRL',
withDDD: true,
dddMask: '(99) '
}}
customTextInput={TextInput}
{...props}
/>
)

const PhoneField = ({
field = {},
placeholder,
form = {},
label,
...props
}) => {
const { colors } = useTheme()
const { name, value } = field
const { error, handleChange, handleBlur } = form
const baseErrorStyle = {
color: colors.negative,
}
return (
<View>
<Label>{label || name}</Label>
<Spacer size="xs" />
<PhoneInput
error={error}
placeholder={placeholder}
onChangeText={handleChange && handleChange(name)}
onBlur={handleChange && handleBlur(name)}
value={value}
{...props}
/>
<Footnote style={baseErrorStyle}>{error}</Footnote>
<Spacer size="s" />
</View>
)
}

export default PhoneField
4 changes: 3 additions & 1 deletion src/components/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function TextField({
field = {},
placeholder,
form ={},
label
label,
...props
}) {
const {colors} = useTheme()
const {name, value} = field
Expand All @@ -28,6 +29,7 @@ function TextField({
onChangeText={handleChange && handleChange(name)}
onBlur={handleChange && handleBlur(name)}
value={value}
{...props}
/>
<Footnote style={baseErrorStyle}>{error}</Footnote>
<Spacer size="s" />
Expand Down
7 changes: 2 additions & 5 deletions src/components/TextInput.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, {useState} from 'react'
import React from 'react'
import {TextInput as BaseTextInput} from 'react-native'
import {useTheme} from '../lib/theme'

function TextInput({style, error, ...props}) {
const [focus, setFocus] = useState(false)
const {colors} = useTheme()
const baseStyle = {
padding: 12,
borderColor: error ? colors.negative : focus ? colors.link : colors.border,
borderColor: error ? colors.negative : colors.border,
borderWidth: 1,
borderRadius: 14,
fontSize: 16,
Expand All @@ -21,8 +20,6 @@ function TextInput({style, error, ...props}) {
return (
<BaseTextInput
style={[baseStyle, style]}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
placeholderTextColor={colors.placeholder}
{...props}
/>
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export {default as TextInput} from "./components/TextInput"
export {default as Theme} from "./components/Theme"
export {default as DrawerContent} from "./components/DrawerContent"
export {default as Screen} from "./components/Screen"
export {default as PhoneField} from "@vela/ui/src/components/Form/PhoneField"
export {useTheme} from "./lib/theme"

0 comments on commit 79fefd9

Please sign in to comment.