|
1 | 1 | import * as React from 'react'; |
| 2 | +import { View } from 'react-native'; |
| 3 | +import { DeepPartial } from 'ts-essentials'; |
2 | 4 |
|
3 | | -import { Box } from '../Box'; |
| 5 | +import { useTheme } from '../../theme'; |
| 6 | +import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles'; |
4 | 7 | import { Label, Text } from '../Typography'; |
| 8 | +import { |
| 9 | + FormFieldStyles, |
| 10 | + GetFormFieldStyles, |
| 11 | + getFormFieldStyles, |
| 12 | +} from './FormField.styles'; |
| 13 | + |
| 14 | +export type FormFieldLabelPosition = 'top' | 'left' | 'right'; |
5 | 15 |
|
6 | 16 | export interface FormFieldProps { |
7 | | - error?: string | null; |
8 | | - label?: string | null; |
9 | | - description?: string | null; |
| 17 | + error?: string; |
| 18 | + label?: string; |
| 19 | + labelPosition?: FormFieldLabelPosition; |
| 20 | + description?: string; |
10 | 21 | children?: React.ReactNode; |
| 22 | + getStyles?: ReplaceReturnType< |
| 23 | + GetFormFieldStyles, |
| 24 | + DeepPartial<FormFieldStyles> |
| 25 | + >; |
11 | 26 | } |
12 | 27 |
|
13 | 28 | export const FormField = (props: FormFieldProps) => { |
14 | | - const { label, error, children, description, ...passThroughProps } = props; |
| 29 | + const { |
| 30 | + label, |
| 31 | + error, |
| 32 | + children, |
| 33 | + description, |
| 34 | + labelPosition = 'top', |
| 35 | + getStyles, |
| 36 | + } = props; |
| 37 | + const theme = useTheme(); |
| 38 | + |
| 39 | + const { |
| 40 | + containerStyles, |
| 41 | + descriptionTextStyle, |
| 42 | + errorTextStyle, |
| 43 | + errorWrapperStyle, |
| 44 | + labelTextStyle, |
| 45 | + labelWrapperStyle, |
| 46 | + wrapperStyle, |
| 47 | + } = mergeStyles(getFormFieldStyles, getStyles)( |
| 48 | + { |
| 49 | + labelPosition, |
| 50 | + }, |
| 51 | + theme, |
| 52 | + ); |
| 53 | + |
| 54 | + const labelContent = ( |
| 55 | + <View style={labelWrapperStyle}> |
| 56 | + <Label getStyles={() => ({ textStyle: labelTextStyle })}>{label}</Label> |
| 57 | + </View> |
| 58 | + ); |
15 | 59 |
|
16 | 60 | return ( |
17 | | - <Box> |
18 | | - {label && ( |
19 | | - <Box marginBottom={4}> |
20 | | - <Label>{label}</Label> |
21 | | - </Box> |
| 61 | + <View style={containerStyles}> |
| 62 | + {label && labelPosition === 'top' && labelContent} |
| 63 | + <View style={wrapperStyle}> |
| 64 | + {label && labelPosition === 'left' && labelContent} |
| 65 | + {children} |
| 66 | + {label && labelPosition === 'right' && labelContent} |
| 67 | + </View> |
| 68 | + {description && ( |
| 69 | + <Text |
| 70 | + color="muted" |
| 71 | + getStyles={() => ({ textStyle: descriptionTextStyle })} |
| 72 | + > |
| 73 | + {description} |
| 74 | + </Text> |
22 | 75 | )} |
23 | | - <Box marginBottom={4}> |
24 | | - {/* |
25 | | - // @ts-ignore: TODO: Find right way to type this */} |
26 | | - {React.cloneElement(children, passThroughProps)} |
27 | | - </Box> |
28 | 76 | {error && ( |
29 | | - <Box marginBottom={4}> |
30 | | - <Text color="danger">{error}</Text> |
31 | | - </Box> |
| 77 | + <View style={errorWrapperStyle}> |
| 78 | + <Text |
| 79 | + color="danger" |
| 80 | + getStyles={() => ({ textStyle: errorTextStyle })} |
| 81 | + > |
| 82 | + {error} |
| 83 | + </Text> |
| 84 | + </View> |
32 | 85 | )} |
33 | | - {description && <Text>{description}</Text>} |
34 | | - </Box> |
| 86 | + </View> |
35 | 87 | ); |
36 | 88 | }; |
0 commit comments