Skip to content

Commit 61b5832

Browse files
committed
Add dense styling. Update README.
1 parent 32552ee commit 61b5832

File tree

7 files changed

+83
-54
lines changed

7 files changed

+83
-54
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class App extends React.Component {
5353
}
5454
```
5555

56+
### Dense Styling
57+
58+
To use the "dense" styling that matches the [Material Design Style Guide](https://www.google.com/design/spec/components/text-fields.html#text-fields-labels), you can set the prop `dense` to `true`. By default, this prop is set to `false`.
59+
60+
![demo dense](/demo-dense.gif)
61+
5662
## Props
5763

5864
Below are the props you can pass to the React Component to customize the TextInput.
@@ -64,10 +70,10 @@ highlightColor | string | | This string represents the hex code, rgb, or rgba co
6470
duration | number | `200` | A number representing the duration of floating label and underline animations in milliseconds.
6571
labelColor | string | `#9E9E9E` | This string represents the hex code, rgb, or rgba color of the textInput label when it is inactive.
6672
borderColor | string | `#E0E0E0` | This string represents the hex code, rgb, or rgba color of the textInput underline when it is inactive.
73+
dense | bool | `false` | If true, it will render the "dense" input field which is smaller in height and has smaller font sizes. You can view more [here](https://www.google.com/design/spec/components/text-fields.html#text-fields-labels).
6774

6875
## TODO
6976

7077
- [ ] Support multi-line TextInput fields
71-
- [ ] Support dense style
7278
- [ ] Support character limit
7379
- [ ] Add option for dark theme

demo-dense.gif

95.3 KB
Loading

demo.gif

-33 KB
Loading

examples/FloatingLabel/index.ios.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class FloatingLabel extends Component {
3636
onChangeText={(text) => {
3737
this.inputs.name = text;
3838
}}
39-
value={'Jam Man'}
39+
value={'Jane'}
40+
dense={true}
4041
/>
4142
<TextField
4243
label={'Address'}
@@ -48,6 +49,7 @@ class FloatingLabel extends Component {
4849
onSubmitEditing={() => {
4950
this.refs.cityInput.focus();
5051
}}
52+
dense={true}
5153
/>
5254
<TextField
5355
label={'City'}
@@ -63,6 +65,7 @@ class FloatingLabel extends Component {
6365
onChangeText={(text) => {
6466
this.inputs.state = text;
6567
}}
68+
value={'WA'}
6669
/>
6770
<TextField
6871
label={'Zip'}

examples/FloatingLabel/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
},
88
"dependencies": {
99
"react-native": "^0.19.0",
10-
"react-native-md-textinput": "1.6.0"
10+
"react-native-md-textinput": "^1.6.0"
1111
}
1212
}

lib/FloatingLabel.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ import React, {
1111
export default class FloatingLabel extends Component {
1212
constructor(props: Object) {
1313
super(props);
14-
let posTop = (props.hasValue) ? 16 : 34;
15-
let fontSize = (props.hasValue) ? 10 : 16;
14+
if(props.dense) {
15+
this.posTop = 12;
16+
this.posBottom = 32;
17+
this.fontLarge = 13;
18+
this.fontSmall = 13;
19+
} else {
20+
this.posTop = 16;
21+
this.posBottom = 37;
22+
this.fontLarge = 16;
23+
this.fontSmall = 12;
24+
}
25+
let posTop = (props.hasValue) ? this.posTop : this.posBottom;
26+
let fontSize = (props.hasValue) ? this.fontSmall : this.fontLarge;
1627
this.state = {
1728
top: new Animated.Value(posTop),
1829
fontSize: new Animated.Value(fontSize)
@@ -24,23 +35,23 @@ export default class FloatingLabel extends Component {
2435
floatLabel() {
2536
Animated.parallel([
2637
Animated.timing(this.state.top, {
27-
toValue: 16,
38+
toValue: this.posTop,
2839
duration: this.props.duration
2940
}),
3041
Animated.timing(this.state.fontSize, {
31-
toValue: 10,
42+
toValue: this.fontSmall,
3243
duration: this.props.duration
3344
})
3445
]).start();
3546
}
3647
sinkLabel() {
3748
Animated.parallel([
3849
Animated.timing(this.state.top, {
39-
toValue: 34,
50+
toValue: this.posBottom,
4051
duration: this.props.duration
4152
}),
4253
Animated.timing(this.state.fontSize, {
43-
toValue: 16,
54+
toValue: this.fontLarge,
4455
duration: this.props.duration
4556
})
4657
]).start();
@@ -74,13 +85,14 @@ FloatingLabel.propTypes = {
7485
duration: PropTypes.number,
7586
label: PropTypes.string,
7687
labelColor: PropTypes.string,
77-
highlightColor: PropTypes.string
88+
highlightColor: PropTypes.string,
89+
dense: PropTypes.bool
7890
};
7991

8092
const styles = StyleSheet.create({
8193
labelText: {
8294
position: 'absolute',
83-
left: 1,
95+
left: 0,
8496
backgroundColor: 'rgba(0,0,0,0)'
8597
}
8698
});

lib/TextField.js

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,39 @@ export default class TextField extends Component {
3434
onBlur,
3535
onChangeText,
3636
value,
37+
dense,
3738
...props
3839
} = this.props;
3940
return (
40-
<View style={styles.wrapper} ref="wrapper">
41-
<View style={[styles.inputWrapper, this.state.isFocused && styles.inputWrapperActive]}>
42-
<TextInput
43-
style={styles.textInput}
44-
onFocus={() => {
45-
this.setState({isFocused: true});
46-
this.refs.floatingLabel.floatLabel();
47-
this.refs.underline.expandLine();
48-
onFocus && onFocus();
49-
}}
50-
onBlur={() => {
51-
this.setState({isFocused: false});
52-
!this.state.text.length && this.refs.floatingLabel.sinkLabel();
53-
this.refs.underline.shrinkLine();
54-
onBlur && onBlur();
55-
}}
56-
onChangeText={(text) => {
57-
this.setState({text});
58-
onChangeText && onChangeText(text);
59-
}}
60-
ref="input"
61-
value={this.state.text}
62-
{...props}
63-
/>
64-
<Underline
65-
ref="underline"
66-
highlightColor={highlightColor}
67-
duration={duration}
68-
borderColor={borderColor}
69-
/>
70-
</View>
41+
<View style={dense ? styles.denseWrapper : styles.wrapper} ref="wrapper">
42+
<TextInput
43+
style={dense ? styles.denseTextInput : styles.textInput}
44+
onFocus={() => {
45+
this.setState({isFocused: true});
46+
this.refs.floatingLabel.floatLabel();
47+
this.refs.underline.expandLine();
48+
onFocus && onFocus();
49+
}}
50+
onBlur={() => {
51+
this.setState({isFocused: false});
52+
!this.state.text.length && this.refs.floatingLabel.sinkLabel();
53+
this.refs.underline.shrinkLine();
54+
onBlur && onBlur();
55+
}}
56+
onChangeText={(text) => {
57+
this.setState({text});
58+
onChangeText && onChangeText(text);
59+
}}
60+
ref="input"
61+
value={this.state.text}
62+
{...props}
63+
/>
64+
<Underline
65+
ref="underline"
66+
highlightColor={highlightColor}
67+
duration={duration}
68+
borderColor={borderColor}
69+
/>
7170
<FloatingLabel
7271
isFocused={this.state.isFocused}
7372
ref="floatingLabel"
@@ -76,6 +75,7 @@ export default class TextField extends Component {
7675
labelColor={labelColor}
7776
highlightColor={highlightColor}
7877
duration={duration}
78+
dense={dense}
7979
hasValue={(this.state.text.length) ? true : false}
8080
/>
8181
</View>
@@ -90,32 +90,40 @@ TextField.propTypes = {
9090
onFocus: PropTypes.func,
9191
onBlur: PropTypes.func,
9292
onChangeText: PropTypes.func,
93-
value: PropTypes.string
93+
value: PropTypes.string,
94+
dense: PropTypes.bool
9495
};
9596

9697
TextField.defaultProps = {
9798
duration: 200,
9899
labelColor: '#9E9E9E',
99100
borderColor: '#E0E0E0',
100-
value: ''
101+
value: '',
102+
dense: false
101103
};
102104

103105
const styles = StyleSheet.create({
104106
wrapper: {
105107
height: 72,
106-
flex: 1,
107-
paddingTop: 28,
108-
paddingBottom: 8,
108+
paddingTop: 30,
109+
paddingBottom: 7,
109110
position: 'relative'
110111
},
112+
denseWrapper: {
113+
height: 60,
114+
paddingTop: 28,
115+
paddingBottom: 4,
116+
position: 'relative'
117+
},
111118
textInput: {
112-
flex: 1,
113119
fontSize: 16,
114-
lineHeight: 16
120+
height: 34,
121+
lineHeight: 34
115122
},
116-
inputWrapper: {
117-
borderWidth: 0,
118-
height: 32,
119-
flex: 1
120-
}
123+
denseTextInput: {
124+
fontSize: 13,
125+
height: 27,
126+
lineHeight: 24,
127+
paddingBottom: 3
128+
}
121129
});

0 commit comments

Comments
 (0)