Skip to content

Commit

Permalink
apps/contrib/*: add HoverButton and helper for dates
Browse files Browse the repository at this point in the history
  • Loading branch information
philli-m committed Feb 27, 2023
1 parent 27b6aec commit 891e9be
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
37 changes: 37 additions & 0 deletions apps/contrib/assets/HoverButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { useState, useEffect } from 'react'

export const HoverButton = (props) => {
const { textMouseOn, textMouseOff, onClick } = props
const [buttonText, setButtonText] = useState(textMouseOff)
const [processing, setProcessing] = useState(false)

const handleClick = () => {
setProcessing(true)
onClick()
}

useEffect(() => {
setProcessing(false)
setButtonText(textMouseOff)
}, [textMouseOff])

return (
<button
id={props.id}
className={props.className}
type="button"
onClick={handleClick}
disabled={processing || props.disabled}
onMouseEnter={() => setButtonText(textMouseOn)}
onMouseLeave={() => setButtonText(textMouseOff)}
onFocus={() => setButtonText(textMouseOn)}
onBlur={() => setButtonText(textMouseOff)}
aria-label={buttonText}
>
{props.icon}
<span className="ms-1">
{buttonText}
</span>
</button>
)
}
53 changes: 53 additions & 0 deletions apps/contrib/assets/__tests__/HoverButton.jest.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom'
import { HoverButton } from '../HoverButton'

test('Show button with normal text', () => {
render(
<HoverButton
textMouseOff="activated"
/>
)
expect(screen.getByText('activated')).toBeInTheDocument()
})

test('Show button with hover text, via mouse', () => {
render(
<HoverButton
textMouseOn="deactivate"
textMouseOff="activated"
/>
)
fireEvent.mouseOver(screen.getByText('activated'))
expect(screen.getByText('deactivate')).toBeInTheDocument()
fireEvent.mouseOut(screen.getByText('deactivate'))
expect(screen.getByText('activated')).toBeInTheDocument()
})

test('Show button with hover text, via keyboard', () => {
render(
<HoverButton
textMouseOn="deactivate"
textMouseOff="activated"
/>
)
fireEvent.focus(screen.getByText('activated'))
expect(screen.getByText('deactivate')).toBeInTheDocument()
fireEvent.blur(screen.getByText('deactivate'))
expect(screen.getByText('activated')).toBeInTheDocument()
})

test('Show button with normal text and click on button', () => {
const onChangeFn = jest.fn()
render(
<HoverButton
textMouseOn="deactivate"
textMouseOff="activated"
onClick={onChangeFn}
/>
)
expect(screen.getByText('activated')).toBeInTheDocument()
fireEvent.click(screen.getByText('activated'))
expect(onChangeFn).toHaveBeenCalled()
})
8 changes: 8 additions & 0 deletions apps/contrib/dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.template import defaultfilters
from django.utils import timezone


def get_date_display(date):
local_date = timezone.localtime(date)
return '{}, {}'.format(defaultfilters.date(local_date),
defaultfilters.time(local_date))

0 comments on commit 891e9be

Please sign in to comment.