Skip to content

Commit

Permalink
Added placeholder and setPlaceholder methods to CPTextField, to facil…
Browse files Browse the repository at this point in the history
…itate displaying placeholder text for textfields before user has entered text. May want to consider changing text color while displaying placeholder text.
  • Loading branch information
lethain committed Sep 7, 2008
1 parent 2c3ac4c commit b1c99a5
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion AppKit/CPTextField.j
Expand Up @@ -66,6 +66,7 @@ var _CPTextFieldSquareBezelColor = nil;
BOOL _isSelectable;

id _value;
id _placeholderString;

CPLineBreakMode _lineBreakMode;
#if PLATFORM(DOM)
Expand Down Expand Up @@ -104,7 +105,8 @@ var _CPTextFieldSquareBezelColor = nil;
if (self)
{
_value = @"";

_placeholderValue = @"";

#if PLATFORM(DOM)
_DOMTextElement = document.createElement("div");
_DOMTextElement.style.position = "absolute";
Expand Down Expand Up @@ -245,6 +247,12 @@ var _CPTextFieldSquareBezelColor = nil;

[[CPDOMWindowBridge sharedDOMWindowBridge] _propagateCurrentDOMEvent:YES];

// If current value is the placeholder value, remove it to
// allow user to update.
if ([_value caseInsensitiveCompare:[self placeholderString]]==0) {
[self setStringValue:@""];
}

return YES;
}

Expand All @@ -254,6 +262,11 @@ var _CPTextFieldSquareBezelColor = nil;

_DOMElement.removeChild(element);
[self setStringValue:element.value];

// If textfield has no value, then display the placeholderValue
if ([_value caseInsensitiveCompare:@""]==0) {
[self setStringValue:[self placeholderString]];
}

return YES;
}
Expand Down Expand Up @@ -354,6 +367,24 @@ var _CPTextFieldSquareBezelColor = nil;
#endif
}

- (CPString)placeholderString
{
return [_placeholderString string];
}

-(void)setPlaceholderString:(CPString)aStringValue
{
_placeholderString = aStringValue;
#if PLATFORM(DOM)
var cssString = _value ? [_value cssString] : @"";

if (CPFeatureIsCompatible(CPJavascriptInnerTextFeature))
_DOMTextElement.innerText = cssString;
else if (CPFeatureIsCompatible(CPJavascriptTextContentFeature))
_DOMTextElement.textContent = cssString;
#endif
}

- (void)sizeToFit
{
#if PLATFORM(DOM)
Expand Down

0 comments on commit b1c99a5

Please sign in to comment.