-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from jbouder/public-sharing
Update UI for Public Sharing
- Loading branch information
Showing
18 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import '@testing-library/jest-dom'; | ||
import { act, fireEvent, render } from '@testing-library/react'; | ||
import { Toggle } from './toggle'; | ||
|
||
describe('Toggle', () => { | ||
test('renders a default toggle successfully', () => { | ||
const { baseElement } = render(<Toggle id="default-toggle" />); | ||
const input = baseElement.querySelector('input'); | ||
|
||
expect(input).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders a toggle with label', () => { | ||
const { baseElement } = render(<Toggle id="label-toggle" label="label" />); | ||
const label = baseElement.querySelector('label'); | ||
|
||
expect(label).toBeInTheDocument(); | ||
}); | ||
|
||
test('fires event callback when changed', async () => { | ||
const { baseElement } = render(<Toggle id="clickable-toggle" />); | ||
const input = baseElement.querySelector('input') as HTMLInputElement; | ||
const body = baseElement.querySelector('.toggle-body') as HTMLInputElement; | ||
expect(body).not.toHaveClass('btn-primary'); | ||
|
||
await act(async () => { | ||
fireEvent.click(input); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { ChangeEvent, ChangeEventHandler, useEffect, useState } from 'react'; | ||
|
||
export interface ToggleProps { | ||
/** | ||
* The unique identifier for this component | ||
*/ | ||
id: string; | ||
/** | ||
* The name of the text input | ||
*/ | ||
name?: string; | ||
/** | ||
* Whether the toggle is checked or not | ||
*/ | ||
checked?: boolean; | ||
/** | ||
* A label to display with the toggle | ||
*/ | ||
label?: string; | ||
/** | ||
* Custom callback for when input is changed | ||
*/ | ||
onChange?: ChangeEventHandler<HTMLInputElement>; | ||
} | ||
|
||
export const Toggle = ({ | ||
id, | ||
name, | ||
checked = false, | ||
label, | ||
onChange, | ||
}: ToggleProps) => { | ||
const [isChecked, setIsChecked] = useState(false); | ||
const toggleHandler = (event: ChangeEvent<HTMLInputElement>) => { | ||
setIsChecked(!isChecked); | ||
if (onChange) { | ||
onChange(event); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setIsChecked(checked); | ||
}, [checked]); | ||
|
||
return ( | ||
<div className="toggle flex items-center"> | ||
<label htmlFor={id} className="flex items-center cursor-pointer"> | ||
<div className="relative"> | ||
<input | ||
type="checkbox" | ||
id={id} | ||
name={name} | ||
className="sr-only" | ||
checked={isChecked} | ||
onChange={toggleHandler} | ||
/> | ||
<div | ||
className={`toggle-body w-12 h-6 rounded-full shadow-inner ${ | ||
isChecked ? 'toggle-body-on' : '' | ||
}`} | ||
></div> | ||
<div | ||
className={`toggle-dot absolute w-6 h-6 rounded-full shadow inset-y-0 left-0 ${ | ||
isChecked ? 'ml-6' : 'ml-0' | ||
}`} | ||
></div> | ||
</div> | ||
{label && <span className="toggle-label ml-3">{label}</span>} | ||
</label> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Toggle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters