Skip to content

Commit

Permalink
feat: add API for setting assertive to Notification (#6862)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Nov 25, 2024
1 parent 5109bed commit b2fe10c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ public Notification(String text, int duration, Position position) {
setPosition(position);
}

/**
* Creates a Notification with given text String, duration, position and
* assertive state.
* <P>
* Set to {@code 0} or a negative number to disable the notification
* auto-closing.
*
* @param text
* the text of the notification
* @param duration
* the duration in milliseconds to show the notification
* @param position
* the position of the notification. Valid enumerate values are
* TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE,
* BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH
* @param assertive
* whether the notification should have {@code aria-live}
* attribute set to {@code assertive} or {@code polite}
*/
public Notification(String text, int duration, Position position,
boolean assertive) {
this(text, duration, position);
setAssertive(assertive);
}

/**
* Creates a notification with given components inside.
* <p>
Expand Down Expand Up @@ -232,6 +257,31 @@ private void removeAutoAdded() {
}
}

/**
* Shows a notification in the current page with given text, duration,
* position and assertive state.
*
* @param text
* the text of the Notification
* @param duration
* the duration in milliseconds to show the notification
* @param position
* the position of the notification. Valid enumerate values are
* TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE,
* BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH
* @param assertive
* whether the notification should have {@code aria-live}
* attribute set to {@code assertive} or {@code polite}
* @return the notification
*/
public static Notification show(String text, int duration,
Position position, boolean assertive) {
Notification notification = new Notification(text, duration, position,
assertive);
notification.open();
return notification;
}

/**
* Shows a notification in the current page with given text, duration and
* position.
Expand All @@ -248,9 +298,7 @@ private void removeAutoAdded() {
*/
public static Notification show(String text, int duration,
Position position) {
Notification notification = new Notification(text, duration, position);
notification.open();
return notification;
return show(text, duration, position, false);
}

/**
Expand Down Expand Up @@ -501,6 +549,29 @@ public int getDuration() {
return getElement().getProperty("duration", 0);
}

/**
* When true, the notification card has {@code aria-live} attribute set to
* {@code assertive} instead of {@code polite}. This makes screen readers
* announce the notification content immediately when it appears.
*
* @param assertive
* the value to set
*/
public void setAssertive(boolean assertive) {
getElement().setProperty("assertive", assertive);
}

/**
* When true, the notification card has {@code aria-live} attribute set to
* {@code assertive} instead of {@code polite}. This makes screen readers
* announce the notification content immediately when it appears.
*
* @return the {@code assertive} property from the webcomponent
*/
public boolean isAssertive() {
return getElement().getProperty("assertive", false);
}

/**
* {@inheritDoc}
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,18 @@ public void setText_notificationHasUnmodifiedText() {
Assert.assertEquals("foo > bar",
notification.getElement().getProperty("text"));
}

@Test
public void setAssertive_isAssertive() {
Notification notification = new Notification();
notification.setAssertive(true);
Assert.assertEquals(notification.isAssertive(), true);
Assert.assertTrue(
notification.getElement().getProperty("assertive", false));

notification.setAssertive(false);
Assert.assertEquals(notification.isAssertive(), false);
Assert.assertFalse(
notification.getElement().getProperty("assertive", false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public void stringDurAndPositionCtor() {
notification.getPosition().getClientName());
}

@Test
public void stringDurationPositionAndAssertiveCtor() {
notification = new Notification("fooo", 10000, Position.TOP_END, true);
Assert.assertEquals(10000, notification.getDuration(), 0);
Assert.assertEquals("top-end",
notification.getPosition().getClientName());
Assert.assertTrue(notification.isAssertive());
}

@Test
public void componentCtor() {
notification = new Notification(new Label(), new NativeButton());
Expand All @@ -70,10 +79,11 @@ public void componentCtor() {

@Test
public void staticCtor() {
notification = Notification.show("fooooo", 4000,
Position.BOTTOM_CENTER);
notification = Notification.show("fooooo", 4000, Position.BOTTOM_CENTER,
true);
Assert.assertEquals("bottom-center",
notification.getPosition().getClientName());
Assert.assertTrue(notification.isAssertive());
}

@Test
Expand Down

0 comments on commit b2fe10c

Please sign in to comment.