implement _setCurrentValueIsPlaceholder in CPTextField

CPKeyValueBinding will sent _setCurrentValueIsPlaceholder: to indicate to an object that current value is a placeholder and should be displayed as such. This commit implements the method for CPTextField and adds a unit test to test the behavior.
This commit is contained in:
Klaas Pieter Annema
2010-09-23 10:35:41 +02:00
parent 2fba03dc75
commit 8fe67b73da
2 changed files with 56 additions and 0 deletions
+26
View File
@@ -86,6 +86,8 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
CPColor _textFieldBackgroundColor;
id _placeholderString;
id _originalPlaceholderString;
BOOL _currentValueIsPlaceholder;
id _delegate;
@@ -780,6 +782,30 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
return _placeholderString;
}
- (void)_setCurrentValueIsPlaceholder:(BOOL)isPlaceholder
{
if (isPlaceholder)
{
// Save the original placeholder value so we can restore it later
// Only do this if the placeholder is not already overridden because the bindings logic might call this method
// several times and we don't want the bindings placeholder to ever become the original placeholder
if (!_currentValueIsPlaceholder)
_originalPlaceholderString = [self placeholderString];
// Set the current string value as the current placeholder and clear the string value
[self setPlaceholderString:[self stringValue]];
[self setStringValue:@""];
}
else
{
// Restore the original placeholder, the actual textfield value is already correct
// because it was set using setValue:forKey:
[self setPlaceholderString:_originalPlaceholderString];
}
_currentValueIsPlaceholder = isPlaceholder;
}
/*!
Size to fit has two behavior, depending on if the receiver is an editable text field or not.
+30
View File
@@ -190,6 +190,29 @@
[self assert:'value' equals:testView.lastKey];
}
- (void)testTextField
{
var textField = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[textField setPlaceholderString:@"cheese"];
content = [
[BindingTester testerWithCheese:@"yellow"],
[BindingTester testerWithCheese:@"green"]
];
arrayController = [[CPArrayController alloc] initWithContent:content];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)]];
var options = [CPDictionary dictionaryWithJSObject:{CPMultipleValuesPlaceholderBindingOption:@"Multiple Values"}];
[textField bind:@"value" toObject:arrayController withKeyPath:@"selection.cheese" options:options];
[self assert:@"Multiple Values" equals:[textField placeholderString]];
[arrayController setSelectionIndex:0];
[self assert:@"cheese" equals:[textField placeholderString]];
}
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)changes context:(id)aContext
{
CPLog(@"here: "+aKeyPath+" value: "+[anObject valueForKey:aKeyPath]);
@@ -202,6 +225,13 @@
id cheese;
}
+ (id)testerWithCheese:(id)aCheese
{
var tester = [[self alloc] init];
[tester setCheese:aCheese];
return tester;
}
- (void)setCheese:(id)aCheese
{
cheese = aCheese;