restore the original placeholder when unbinding

This commit is contained in:
Klaas Pieter Annema
2011-01-07 14:14:19 +01:00
parent 8254543da7
commit eee1dcef13
2 changed files with 31 additions and 1 deletions
+17 -1
View File
@@ -258,6 +258,8 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
[self setStringValue:@""];
[self setPlaceholderString:@""];
_originalPlaceholderString = undefined;
_sendActionOn = CPKeyUpMask | CPKeyDownMask;
[self setValue:CPLeftTextAlignment forThemeAttribute:@"alignment"];
@@ -733,6 +735,7 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
/*
@ignore
Sets the internal string value without updating the value in the input element
*/
- (void)_setStringValue:(id)aValue
{
@@ -806,16 +809,27 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder");
[self setPlaceholderString:[self stringValue]];
[self setStringValue:@""];
}
else if (_originalPlaceholderString)
else if (_originalPlaceholderString !== undefined)
{
// Restore the original placeholder, the actual textfield value is already correct
// because it was set using setValue:forKey:
[self setPlaceholderString:_originalPlaceholderString];
_originalPlaceholderString = undefined;
}
_currentValueIsPlaceholder = isPlaceholder;
}
- (void)unbind:(CPString)theBinding
{
if (theBinding === CPValueBinding)
{
[self _setCurrentValueIsPlaceholder:NO];
}
[super unbind:theBinding];
}
/*!
For non-bezeled text fields (typically a label), sizeToFit has two behaviors, depending
on the line break mode of the receiver.
@@ -1270,6 +1284,8 @@ var CPTextFieldIsEditableKey = "CPTextFieldIsEditableKey",
[self setAlignment:[aCoder decodeIntForKey:CPTextFieldAlignmentKey]];
[self setPlaceholderString:[aCoder decodeObjectForKey:CPTextFieldPlaceholderStringKey]];
_originalPlaceholderString = undefined;
}
+14
View File
@@ -222,9 +222,23 @@
[self assert:@"" equals:[textField stringValue] message:@"text field string value should be cleared"];
[self assert:@"Multiple Values" equals:[textField placeholderString] message:@"text field placeholder should be 'Multiple Values'"];
[textField unbind:@"value"];
[self assert:@"cheese" equals:[textField placeholderString] message:@"text field placeholder should be reset"];
[textField bind:@"value" toObject:arrayController withKeyPath:@"selection.cheese" options:options];
[arrayController setSelectionIndex:0];
[self assert:@"yellow" equals:[textField stringValue] message:"text field string value should be 'yellow'"];
[self assert:@"cheese" equals:[textField placeholderString] message:"text field placeholder should be restored"];
textField = [[CPTextField alloc] init];
[textField bind:@"value" toObject:arrayController withKeyPath:@"selection.cheese" options:options];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)]];
[self assert:@"Multiple Values" equals:[textField placeholderString] message:@"text field placeholder should 'Multiple Values'"];
[arrayController setSelectionIndex:0];
[self assert:@"" equals:[textField placeholderString] message:@"empty text field placeholder should be restored"];
}
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)changes context:(id)aContext