| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import '../editing/editable_string.dart'; | 5 import '../editing/editable_string.dart'; |
| 6 import '../editing/editable_text.dart'; | 6 import '../editing/editable_text.dart'; |
| 7 import '../editing/keyboard.dart'; | 7 import '../editing/keyboard.dart'; |
| 8 import '../fn.dart'; | 8 import '../fn.dart'; |
| 9 import '../theme/colors.dart'; | 9 import '../theme/colors.dart'; |
| 10 | 10 |
| 11 typedef void ValueChanged(value); | 11 typedef void ValueChanged(value); |
| 12 | 12 |
| 13 class Input extends Component { | 13 class Input extends Component { |
| 14 static final Style _style = new Style(''' | 14 static final Style _style = new Style(''' |
| 15 display: paragraph; | 15 display: paragraph; |
| 16 transform: translateX(0); |
| 16 margin: 8px; | 17 margin: 8px; |
| 17 padding: 8px; | 18 padding: 8px; |
| 18 border-bottom: 1px solid ${Grey[200]}; | 19 border-bottom: 1px solid ${Grey[200]}; |
| 19 align-self: center; | 20 align-self: center; |
| 20 height: 1.2em; | 21 height: 1.2em; |
| 21 white-space: pre; | 22 white-space: pre; |
| 22 overflow: hidden;''' | 23 overflow: hidden;''' |
| 23 ); | 24 ); |
| 24 | 25 |
| 26 static final Style _placeholderStyle = new Style(''' |
| 27 top: 8px; |
| 28 left: 8px; |
| 29 color: ${Grey[200]}; |
| 30 position: absolute;''' |
| 31 ); |
| 32 |
| 25 static final String _focusedInlineStyle = ''' | 33 static final String _focusedInlineStyle = ''' |
| 26 padding: 7px; | 34 padding: 7px; |
| 27 border-bottom: 2px solid ${Blue[500]};'''; | 35 border-bottom: 2px solid ${Blue[500]};'''; |
| 28 | 36 |
| 29 ValueChanged onChanged; | 37 ValueChanged onChanged; |
| 30 String value; | 38 String value; |
| 39 String placeholder; |
| 31 bool focused = false; | 40 bool focused = false; |
| 32 | 41 |
| 33 bool _isAttachedToKeyboard = false; | 42 bool _isAttachedToKeyboard = false; |
| 34 EditableString _editableValue; | 43 EditableString _editableValue; |
| 35 | 44 |
| 36 Input({Object key, this.value: '', this.focused}) | 45 Input({Object key, this.value: '', this.placeholder, this.focused}) |
| 37 : super(key: key, stateful: true) { | 46 : super(key: key, stateful: true) { |
| 38 _editableValue = new EditableString(text: value, | 47 _editableValue = new EditableString(text: value, |
| 39 onUpdated: _handleTextUpdated); | 48 onUpdated: _handleTextUpdated); |
| 40 } | 49 } |
| 41 | 50 |
| 42 void _handleTextUpdated() { | 51 void _handleTextUpdated() { |
| 43 setState(() {}); | 52 setState(() {}); |
| 44 if (value != _editableValue.text) { | 53 if (value != _editableValue.text) { |
| 45 value = _editableValue.text; | 54 value = _editableValue.text; |
| 46 if (onChanged != null) | 55 if (onChanged != null) |
| 47 onChanged(value); | 56 onChanged(value); |
| 48 } | 57 } |
| 49 } | 58 } |
| 50 | 59 |
| 51 void didUnmount() { | 60 void didUnmount() { |
| 52 if (_isAttachedToKeyboard) | 61 if (_isAttachedToKeyboard) |
| 53 keyboard.hide(); | 62 keyboard.hide(); |
| 54 } | 63 } |
| 55 | 64 |
| 56 Node build() { | 65 Node build() { |
| 57 if (focused && !_isAttachedToKeyboard) { | 66 if (focused && !_isAttachedToKeyboard) { |
| 58 keyboard.show(_editableValue.stub); | 67 keyboard.show(_editableValue.stub); |
| 59 _isAttachedToKeyboard = true; | 68 _isAttachedToKeyboard = true; |
| 60 } | 69 } |
| 61 | 70 |
| 71 List<Node> children = []; |
| 72 |
| 73 if (placeholder != null && value.isEmpty) { |
| 74 children.add(new Container( |
| 75 style: _placeholderStyle, |
| 76 children: [new Text(placeholder)] |
| 77 )); |
| 78 } |
| 79 |
| 80 children.add(new EditableText(value: _editableValue, focused: focused)); |
| 81 |
| 62 return new Container( | 82 return new Container( |
| 63 style: _style, | 83 style: _style, |
| 64 inlineStyle: focused ? _focusedInlineStyle : null, | 84 inlineStyle: focused ? _focusedInlineStyle : null, |
| 65 children: [ | 85 children: children |
| 66 new EditableText(value: _editableValue, focused: focused), | |
| 67 ] | |
| 68 ); | 86 ); |
| 69 } | 87 } |
| 70 } | 88 } |
| OLD | NEW |