-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2820f29
commit 029e1fd
Showing
16 changed files
with
347 additions
and
116 deletions.
There are no files selected for viewing
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,74 @@ | ||
<h2>Input Time</h2> | ||
<p>This is just a simple wrapper around the native input time control.</p> | ||
|
||
|
||
<h3>Normal</h3> | ||
<InputTime bind:value="{val}" /> | ||
<p>Input value: {val}</p> | ||
|
||
<h3>Disabled</h3> | ||
<InputTime disabled value="00:00"/> | ||
|
||
|
||
<h3>With validation</h3> | ||
<InputTime | ||
label="Select Midnight" | ||
error="{error1}" | ||
value="00:01" | ||
on:change="{onchange}" /> | ||
|
||
|
||
<h3>Label on the left</h3> | ||
<InputTime label="Label is on the left" labelOnTheLeft="true"/> | ||
|
||
|
||
|
||
<CodeExample html="{exampleHtml}" /> | ||
|
||
<API props="{apiProps}"/> | ||
|
||
|
||
<script> | ||
import { InputTime } from '../../../../src'; | ||
import { CodeExample } from '../../../code-example'; | ||
import { API } from '../../../api-table'; | ||
const apiProps = [ | ||
{ name: 'class', type: 'string', description: 'Additional css class name to be added to the component.' }, | ||
{ name: 'disabled', description: 'Make the input disabled.' }, | ||
{ name: 'id', type: 'string', description: 'Assign ID to the underlying input.' }, | ||
{ name: 'info', type: 'string', description: 'Show info message above the input.' }, | ||
{ name: 'error', type: 'string', description: 'Error message to show above the input.' }, | ||
{ name: 'name', type: 'string', description: 'Assign title to the underlying input.' }, | ||
{ name: 'label', type: 'string', description: 'Label for the input.' }, | ||
{ name: 'labelOnTheLeft', type: ['true', 'false'], default: 'false', description: 'Put label to the left of the input (instead of at the top). Usually in longer forms, to align labels and inputs, hence input also gets <em>width: 100%</em>, as it will be constraint by the form container.' }, | ||
{ name: 'placeholder', type: 'string', description: 'Assign placeholder to the underlying input.' }, | ||
{ name: 'required', description: 'Mark the input as <i>aria-required</i>. The actual validation must be done in the consumer.' }, | ||
{ name: 'title', type: 'string', description: 'Assign title to the underlying input.' }, | ||
{ name: 'value', type: ['string', 'number'], description: 'Initial value of the input.' }, | ||
{ name: 'bind:element', type: 'element', description: 'Exposes the HTML element of the component.' }, | ||
{ name: 'bind:inputElement', type: 'element', description: 'Exposes the HTML element of the underlying input.' }, | ||
{ name: 'on:change', type: 'function', description: 'Triggered after the value changes and the focus leaves the input.' }, | ||
{ name: 'on:input', type: 'function', description: 'Triggered as soon as the input value changes.' }, | ||
]; | ||
const exampleHtml = ` | ||
<InputTime label="Email" error="Invalid email" value="00:00" on:change="{onChange}" /> | ||
<script> | ||
function onChange (e) { | ||
console.log('value', e.target.value); | ||
} | ||
</script> | ||
`; | ||
let val = '00:00'; | ||
let error1 = 'Select midnight please.'; | ||
function onchange (e) { | ||
error1 = (e.target.value === '00:00' ? '' : 'Select midnight please.'); | ||
console.log(e.target.value); | ||
} | ||
</script> |
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 @@ | ||
export { default as InputTime } from './InputTime.svelte'; |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.input-time input { | ||
padding-left: calc(2rem + 6px); | ||
} |
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,64 @@ | ||
<div | ||
class="input input-time {className}" | ||
class:has-error="{error}" | ||
class:has-value="{value !== ''}" | ||
class:label-on-the-left="{labelOnTheLeft === true || labelOnTheLeft === 'true'}" | ||
bind:this="{element}"> | ||
|
||
<Label {label} {disabled} for="{_id}"/> | ||
<Info msg="{info}" /> | ||
|
||
<div class="input-inner" class:disabled> | ||
<InputError id="{errorMessageId}" msg="{error}" /> | ||
|
||
<div class="input-row"> | ||
<Icon name="clock"/> | ||
|
||
<input | ||
autocomplete="off" | ||
type="time" | ||
{...props} | ||
{disabled} | ||
id="{_id}" | ||
aria-invalid="{error}" | ||
aria-errormessage="{error ? errorMessageId : undefined}" | ||
aria-required="{required}" | ||
bind:this="{inputElement}" | ||
bind:value="{value}" | ||
on:input | ||
on:change | ||
on:focus | ||
on:blur> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
import { pluck, guid } from '../../utils'; | ||
import { Icon } from '../../icon'; | ||
import { Info } from '../../info-bar'; | ||
import { InputError } from '../input-error'; | ||
import { Label } from '../label'; | ||
$:props = pluck($$props, ['title', 'name', 'placeholder']); | ||
let className = ''; | ||
export { className as class }; | ||
export let id = ''; | ||
export let required = undefined; | ||
export let disabled = false; | ||
export let value = ''; | ||
export let label = ''; | ||
export let error = undefined; | ||
export let info = undefined; | ||
export let labelOnTheLeft = false; | ||
export let element = undefined; | ||
export let inputElement = undefined; | ||
$:_id = id || name || guid(); | ||
const errorMessageId = guid(); | ||
</script> |
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 @@ | ||
export { default as InputTime } from './InputTime.svelte'; |
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,70 @@ | ||
import { render } from '@testing-library/svelte'; | ||
import jest from 'jest-mock'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
|
||
import { InputTime } from '../../src/input'; | ||
import { waitForTimeout } from '../helpers/utils'; | ||
|
||
|
||
const props = { | ||
id: 'Component1', | ||
name: 'Component1', | ||
title: 'Component1', | ||
placeholder: 'Component1', | ||
class: 'test-class', | ||
required: true, | ||
error: 'error', | ||
label: 'Component1', | ||
value: '12:00', | ||
}; | ||
|
||
|
||
test('InputTime', async () => { | ||
const { container, component, getByTitle } = render(InputTime, props); | ||
const mock = jest.fn(); | ||
component.$on('change', mock); | ||
|
||
const cmp = container.querySelector('.test-class'); | ||
expect(cmp).toBeInTheDocument(); | ||
expect(cmp).toHaveClass('test-class'); | ||
|
||
// verify props | ||
const input = getByTitle(props.title); | ||
expect(input).toHaveAttribute('id', props.id); | ||
expect(input).toHaveAttribute('title', props.title); | ||
expect(input).toHaveAttribute('name', props.name); | ||
expect(input).toHaveAttribute('placeholder', props.placeholder); | ||
expect(input).toHaveAttribute('aria-required'); | ||
expect(input).toHaveValue(props.value); | ||
|
||
await userEvent.clear(input); | ||
await userEvent.type(input, '14:00'); | ||
await userEvent.keyboard('[Enter]'); | ||
expect(input).toHaveValue('14:00'); | ||
|
||
|
||
let err = cmp.querySelector('.info-bar-error'); | ||
expect(err).toBeInTheDocument(); | ||
expect(err).toHaveTextContent(props.error); | ||
|
||
await component.$set({ error: '' }); | ||
await waitForTimeout(); | ||
err = cmp.querySelector('.info-bar-error'); | ||
expect(err).not.toBeInTheDocument(); | ||
|
||
await component.$set({ info: 'info' }); | ||
let info = cmp.querySelector('.info-bar-info'); | ||
expect(info).toBeInTheDocument(); | ||
expect(info).toHaveTextContent('info'); | ||
|
||
await component.$set({ info: '' }); | ||
await waitForTimeout(); | ||
info = cmp.querySelector('.info-bar-info'); | ||
expect(info).not.toBeInTheDocument(); | ||
|
||
const lbl = cmp.querySelector('label'); | ||
expect(lbl).toBeInTheDocument(); | ||
expect(lbl).toHaveAttribute('for', props.id); | ||
expect(lbl).toHaveTextContent(props.label); | ||
}); |