/* eslint-disable import/no-extraneous-dependencies */
import { storiesOf, html } from '@open-wc/demoing-storybook';
import { LionInput } from '@lion/input';
import '@lion/input/lion-input.js';
import '@lion/input-amount/lion-input-amount.js';
import '@lion/input-date/lion-input-date.js';
import '@lion/input-email/lion-input-email.js';
import {
Required,
EqualsLength,
MinLength,
MaxLength,
MinMaxLength,
IsNumber,
MinNumber,
MaxNumber,
MinMaxNumber,
IsDate,
MinDate,
MaxDate,
MinMaxDate,
IsEmail,
Validator,
loadDefaultFeedbackMessages,
DefaultSuccess,
} from '../index.js';
loadDefaultFeedbackMessages();
storiesOf('Forms|Validation', module)
.add(
'Required Validator',
() => html`
`,
)
.add(
'String Validators',
() => html`
`,
)
.add(
'Number Validators',
() => html`
`,
)
.add('Date Validators', () => {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
const day = today.getDate();
const yesterday = new Date(year, month, day - 1);
const tomorrow = new Date(year, month, day + 1);
return html`
`;
})
.add(
'Email Validator',
() => html`
`,
)
.add('Validation Types', () => {
try {
class MyTypesInput extends LionInput {
static get validationTypes() {
return ['error', 'warning', 'info', 'success'];
}
}
customElements.define('my-types-input', MyTypesInput);
} catch (err) {
// expected as it is a demo
}
return html`
`;
})
.add('Custom Validator', () => {
class MyValidator extends Validator {
constructor(...args) {
super(...args);
this.name = 'myValidator';
}
execute(modelValue, param) {
return modelValue !== param;
}
static getMessage({ fieldName, modelValue, params: param }) {
if (modelValue.length >= param.length - 1 && param.startsWith(modelValue)) {
return 'Almost there...';
}
return `No "${param}" found in ${fieldName}`;
}
}
return html`
`;
})
.add(
'Override default messages',
() => html`
`,
)
.add(
'Override fieldName',
() => html`
`,
)
.add('Asynchronous validation', () => {
function pause(ms = 0) {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, ms);
});
}
class AsyncValidator extends Validator {
constructor(...args) {
super(...args);
this.name = 'asyncValidator';
this.async = true;
}
async execute() {
console.log('async pending...');
await pause(2000);
console.log('async done...');
return true;
}
static getMessage({ modelValue }) {
return `validated for modelValue: ${modelValue}...`;
}
}
return html`
`;
})
.add('Dynamic parameter changes', () => {
const beginDate = new Date('09/09/1990');
const minDateValidatorRef = new MinDate(beginDate, {
message: 'Fill in a date after your birth date',
});
return html`
{
if (!errorState) {
// Since graduation date is usually not before birth date
minDateValidatorRef.param = modelValue;
}
}}"
>
`;
});