2
$\begingroup$

I have a DateObject and InputField that I am using to collect an inputted date. Mouseover works well to switch between the two items. However, if the cursor is in the InputField and the mouse drifts out of it then it switches back to the DateObject.

reportDate = Today;
Mouseover[
 Dynamic[With[{date = Interpreter["Date"][reportDateInput]}, 
   If[DateObjectQ@date, reportDate = date, reportDate]], 
  TrackedSymbols :> {reportDateInput}]
 ,
 InputField[
  Dynamic[reportDateInput,
   {
    None,
    With[{date = Interpreter["Date"][#]}, 
      If[DateObjectQ@date, reportDateInput = #, 
       reportDateInput = "Try again. Date not recognised."]] &
    }],
  String,
  FieldHint -> "Enter the report date."
  ]
 ]

How do I keep the InputField visible as long as it has focus (e.g. text cursor in it)? Should I be using some other combination of controls? I'm only interested in the reportDate variable. The other is just a means to that end. Also, I would like not to have to use Interpreter twice.

This combination is to easily get a date from as there is no built in date picker at the moment. I know there are some custom date pickers out there (e.g. 16542) but I'd rather use this simpler method of free-text input at the moment. Also, I'd like to know how to do this for other UI elements.

$\endgroup$

1 Answer 1

1
+50
$\begingroup$

Use the function: "EventHandler", just like follows:

reportDate = Today;
a = Dynamic[
   With[{date = Interpreter["Date"][reportDateInput]}, 
    If[DateObjectQ@date, reportDate = date, reportDate]], 
   TrackedSymbols :> {reportDateInput}];
b = InputField[
   Dynamic[reportDateInput, {None, 
     With[{date = Interpreter["Date"][#]}, 
       If[DateObjectQ@date, reportDateInput = #, 
        reportDateInput = "Try again. Date not recognised."]] &}], 
   String, FieldHint -> "Enter the report date."];
Dynamic[EventHandler[a, {"MouseMoved" :> (a = b)}]]

How about this

reportDate = Today;
c = a = Dynamic[
    With[{date = Interpreter["Date"][reportDateInput]}, 
     If[DateObjectQ@date, reportDate = date, reportDate]], 
    TrackedSymbols :> {reportDateInput}];
b = InputField[
   Dynamic[reportDateInput, {None, 
     With[{date = Interpreter["Date"][#]}, 
       If[DateObjectQ@date, reportDateInput = #; c = a, 
        reportDateInput = "Try again. Date not recognised."]] &}], 
   String, FieldHint -> "Enter the report date."];
Dynamic[EventHandler[Dynamic[c], {"MouseMoved" :> (c = b)}]]
$\endgroup$
2
  • $\begingroup$ Thanks for trying but this does not appear to update reportDate and it does not return to the formatted output of reportDate after update. $\endgroup$
    – Edmund
    Commented Aug 25, 2015 at 12:20
  • $\begingroup$ The update works well. I did not consider EventHandler as I had only seen it in passing. $\endgroup$
    – Edmund
    Commented Aug 26, 2015 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.