Skip to content

Commit fc4d593

Browse files
authored
docs: document the empty state of the no-argument constructor
Close #3
1 parent e524ada commit fc4d593

4 files changed

Lines changed: 44 additions & 6 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ The relative string is computed and updated in the browser by the underlying web
111111

112112
For continuously-ticking elapsed displays use `Format.DURATION` or `Format.MICRO`. `Format.RELATIVE` (the default) collapses past times under a minute to "now". See [SPECIFICATIONS.md](SPECIFICATIONS.md) §2.8 for the full live-update behaviour matrix.
113113

114+
### Components without a value yet
115+
116+
`new RelativeTime()` writes no `datetime` attribute and renders nothing until `setDateTime` is called. No default is applied: the current instant would be a value the caller did not choose, and it would keep ticking away from the intended one until that value arrives.
117+
118+
Use the no-argument constructor when the value is not available at construction time. For an instance-reusing grid renderer:
119+
120+
```java
121+
grid.addColumn(new ComponentRenderer<>(RelativeTime::new,
122+
(rt, task) -> rt.setDateTime(task.getCreated())));
123+
```
124+
125+
For a display that starts empty and is filled in from a listener:
126+
127+
```java
128+
RelativeTime preview = new RelativeTime().setFormatStyle(FormatStyle.LONG);
129+
picker.addValueChangeListener(e -> preview.setDateTime(e.getValue()));
130+
add(preview);
131+
```
132+
133+
Call `clear()` to return the component to the empty state.
134+
114135
## Special configuration when using Spring
115136

116137
By default, Vaadin Flow only includes `com/vaadin/flow/component` to be always scanned for UI components and views. For this reason, the add-on might need to be allowed in order to display correctly.

SPECIFICATIONS.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ The cadence depends on the displayed unit:
119119
### 3.1 Construction
120120

121121
```java
122-
// Empty: datetime can be set later
122+
// No datetime: renders nothing until setDateTime is called. Intended for component
123+
// renderers, asynchronously loaded values, and displays that start empty.
123124
RelativeTime rt = new RelativeTime();
124125
add(rt);
125126

@@ -133,7 +134,7 @@ add(new RelativeTime(LocalDate.of(2025, 1, 1)));
133134
```java
134135
public class RelativeTime extends Component { // HasStyle inherited from Component
135136

136-
public RelativeTime(); // empty; datetime can be set later
137+
public RelativeTime(); // no datetime; renders nothing until set
137138
public RelativeTime(Instant datetime);
138139
public RelativeTime(OffsetDateTime datetime);
139140
public RelativeTime(ZonedDateTime datetime);
@@ -199,7 +200,7 @@ When no configuration is applied:
199200
- `time-zone` is unset, so absolute-date output uses the viewer's browser default zone.
200201
- The `title` attribute is set automatically to the absolute formatted date and is surfaced as a native tooltip.
201202
- The element auto-updates on its own timer; no polling code is needed on the Java side.
202-
- `RelativeTime` with no `datetime` set renders as an empty inline element.
203+
- `RelativeTime` with no `datetime` set renders as an empty inline element. No default datetime is applied; see §3.1.
203204

204205
## 5. Theming
205206

src/main/java/com/flowingcode/vaadin/addons/relativetime/RelativeTime.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
* the server's. There is no server-side API to read the displayed string; the string lives only
5050
* in the DOM.
5151
*
52+
* <p><b>Empty state.</b> A component with no target datetime renders nothing: the underlying
53+
* element has no text to show. This applies to the {@linkplain #RelativeTime() no-argument
54+
* constructor} and to {@link #clear()}. See the constructor for the cases it serves.
55+
*
5256
* <p><b>Attributes, not properties.</b> Setters write HTML attributes
5357
* ({@code setAttribute}), not DOM properties, because the upstream element is
5458
* attribute-driven and its kebab-case attribute names match the upstream docs
@@ -94,7 +98,17 @@ public class RelativeTime extends Component {
9498

9599
private Instant lastDateTime;
96100

97-
/** Creates an empty component. {@link #setDateTime} can be called later. */
101+
/**
102+
* Creates a relative time component without a target datetime. Renders nothing until
103+
* {@link #setDateTime} is called.
104+
*
105+
* <p>Use this constructor when the datetime is unavailable at creation, such as in component
106+
* renderers ({@code new ComponentRenderer<>(RelativeTime::new, ...)}), asynchronous data
107+
* loading, or displays reset via {@link #clear()}.
108+
*
109+
* <p>No default datetime is applied. The current instant would be a value the caller did not
110+
* choose, and it would keep ticking away from the intended one until that value arrives.
111+
*/
98112
public RelativeTime() {}
99113

100114
/** Creates a component bound to the given instant. */

src/test/java/com/flowingcode/vaadin/addons/relativetime/UseCasesDemo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public UseCasesDemo() {
9696
addUseCase(layout, "stopwatch",
9797
"Live stopwatch", buildStopwatch(),
9898
"A running counter using Format.DURATION (ticks every second from 0s with no \"now\""
99-
+ " plateau). The Start button pins the datetime to now; Stop clears it. Pattern:"
100-
+ " timers for in-progress work, build/deploy status, \"uptime since\" indicators.");
99+
+ " plateau). Built with the no-argument constructor, so it renders nothing until"
100+
+ " Start pins the datetime to now; Stop clears it and it goes back to empty."
101+
+ " Pattern: timers for in-progress work, build/deploy status, \"uptime since\""
102+
+ " indicators.");
101103

102104
addUseCase(layout, "session-expiry",
103105
"Session expiry warning", buildSessionWarning(),

0 commit comments

Comments
 (0)