Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #16 from openizr/0.0.26
Browse files Browse the repository at this point in the history
0.0.26
  • Loading branch information
matthieujabbour committed Aug 27, 2021
2 parents ed914e3 + fb6565f commit f8ca865
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
20 changes: 10 additions & 10 deletions library/src/scripts/react/components/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import PropTypes, { InferProps } from 'prop-types';
import Message from 'scripts/react/components/Message';
import fieldPropType, { Field as FormField } from 'scripts/propTypes/field';

type OUA = (newValue: FormValue) => void;
type OUA = (type: 'click' | 'input', newValue: FormValue) => void;
type I18n = (label: string, values?: Record<string, string>) => string;
type Option = { value?: string; label?: string; type?: string; disabled?: boolean; };

Expand Down Expand Up @@ -59,7 +59,7 @@ const builtInComponents: Components = {
icon={field.options.icon}
type={field.options.type}
iconPosition={field.options.iconPosition}
onClick={(): void => onUserAction(true)}
onClick={(): void => onUserAction('input', true)}
modifiers={`${field.status} ${field.options.modifiers || ''}`}
/>
),
Expand All @@ -72,7 +72,6 @@ const builtInComponents: Components = {
helper={field.message}
min={field.options.min}
max={field.options.max}
onChange={onUserAction}
step={field.options.step}
icon={field.options.icon}
size={field.options.size}
Expand All @@ -87,6 +86,7 @@ const builtInComponents: Components = {
autocomplete={field.options.autocomplete}
iconPosition={field.options.iconPosition}
debounceTimeout={field.options.debounceTimeout || 100}
onChange={(value): void => onUserAction('input', value)}
readonly={field.options.readonly || field.active === false}
modifiers={`${field.status} ${field.options.modifiers || ''}`}
placeholder={(field.options.placeholder !== undefined && field.options.placeholder !== null)
Expand All @@ -101,7 +101,6 @@ const builtInComponents: Components = {
value={field.value}
label={field.label}
helper={field.message}
onChange={onUserAction}
cols={field.options.cols}
rows={field.options.rows}
onBlur={field.options.onBlur}
Expand All @@ -112,6 +111,7 @@ const builtInComponents: Components = {
transform={field.options.transform}
autocomplete={field.options.autocomplete}
debounceTimeout={field.options.debounceTimeout || 100}
onChange={(value): void => onUserAction('input', value)}
readonly={field.options.readonly || field.active === false}
modifiers={`${field.status} ${field.options.modifiers || ''}`}
placeholder={(field.options.placeholder !== undefined && field.options.placeholder !== null)
Expand All @@ -126,12 +126,12 @@ const builtInComponents: Components = {
label={field.label}
value={field.value}
helper={field.message}
onChange={onUserAction}
icon={field.options.icon}
accept={field.options.accept}
onFocus={field.options.onFocus}
multiple={field.options.multiple}
iconPosition={field.options.iconPosition}
onChange={(value): void => onUserAction('input', value)}
modifiers={`${field.status} ${field.options.modifiers || ''}`}
placeholder={(field.options.placeholder !== undefined && field.options.placeholder !== null)
? field.i18n(field.options.placeholder, field.options.formValues)
Expand All @@ -145,9 +145,9 @@ const builtInComponents: Components = {
label={field.label}
value={field.value}
helper={field.message}
onChange={onUserAction}
icon={field.options.icon}
onFocus={field.options.onFocus}
onChange={(value): void => onUserAction('input', value)}
options={field.options.options.map((option: Option) => ((option.label !== undefined)
? ({ ...option, label: field.i18n(option.label, field.options.formValues) })
: option))}
Expand All @@ -162,8 +162,8 @@ const builtInComponents: Components = {
label={field.label}
value={field.value}
helper={field.message}
onChange={onUserAction}
onFocus={field.options.onFocus}
onChange={(value): void => onUserAction('input', value)}
options={field.options.options.map((option: Option) => ((option.label !== undefined)
? ({ ...option, label: field.i18n(option.label, field.options.formValues) })
: option))}
Expand All @@ -177,8 +177,8 @@ const builtInComponents: Components = {
label={field.label}
value={field.value}
helper={field.message}
onChange={onUserAction}
onFocus={field.options.onFocus}
onChange={(value): void => onUserAction('input', value)}
options={field.options.options.map((option: Option) => ((option.label !== undefined)
? ({ ...option, label: field.i18n(option.label, field.options.formValues) })
: option))}
Expand Down Expand Up @@ -221,8 +221,8 @@ export default function Field(props: InferProps<typeof propTypes>): JSX.Element
}
};

const onUserAction = (newValue: FormValue): void => {
props.onUserAction({ type: 'input', value: newValue, fieldId: id });
const onUserAction: OUA = (actionType, newValue) => {
props.onUserAction({ type: actionType, value: newValue, fieldId: id });
};

// Unknown field type...
Expand Down
3 changes: 3 additions & 0 deletions library/src/scripts/react/components/__tests__/Field.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Any = any; // eslint-disable-line @typescript-eslint/no-explicit-any
jest.mock('sonar-ui/react', () => {
/* eslint-disable react/destructuring-assignment, jsx-a11y/no-static-element-interactions */
function Component(props: Any): JSX.Element {
if (props.onChange !== undefined) {
props.onChange();
}
return (
<div
id={props.id}
Expand Down
6 changes: 4 additions & 2 deletions library/src/scripts/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ declare module 'gincko' {
}

declare module 'gincko/react' {
type OUA = (newValue: FormValue) => void;
type OUA = (type: 'click' | 'input', newValue: FormValue) => void;

/** Custom React component. */
export type Component = (field: Field & { i18n: I18n; }, onUserAction: OUA) => JSX.Element;
Expand Down Expand Up @@ -402,6 +402,8 @@ declare module 'gincko/vue' {
import Vue from 'vue';
import { ExtendedVue } from 'vue/types/vue.d';

type OUA = (type: 'click' | 'input', newValue: FormValue) => void;

/**
* Dynamic form.
*/
Expand All @@ -417,7 +419,7 @@ declare module 'gincko/vue' {

/** List of form's custom components. */
customComponents: {
[type: string]: (field: Field, onUserAction: (newValue: FormValue) => void) => {
[type: string]: (field: Field, onUserAction: OUA) => {
component: Vue.Component;
props: any;
events: any;
Expand Down
24 changes: 12 additions & 12 deletions library/src/scripts/vue/components/Field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { FormValue } from 'scripts/core/Engine';
type Generic = Record<string, FormValue>;
type Components = { [type: string]: Component; };
type Component = (field: Field, onUserAction: (newValue: FormValue) => void) => FormValue;
type Component = (field: Field, onUserAction: (type: 'click' | 'input', newValue: FormValue) => void) => FormValue;
interface Props {
id: string;
Expand Down Expand Up @@ -127,7 +127,7 @@ const builtInComponents: Components = {
iconPosition: field.options.iconPosition,
},
events: {
click: (): void => onUserAction(true),
click: (): void => onUserAction('input', true),
},
}),
Textfield: (field, onUserAction) => ({
Expand Down Expand Up @@ -155,10 +155,10 @@ const builtInComponents: Components = {
modifiers: `${field.status} ${field.options.modifiers || ''} `,
},
events: {
change: onUserAction,
focus: field.options.onFocus,
blur: field.options.onBlur,
focus: field.options.onFocus,
iconClick: field.options.onIconClick,
change: (value: FormValue): void => onUserAction('input', value),
},
}),
Textarea: (field, onUserAction) => ({
Expand All @@ -182,9 +182,9 @@ const builtInComponents: Components = {
modifiers: `${field.status} ${field.options.modifiers || ''}`,
},
events: {
change: onUserAction,
focus: field.options.onFocus,
blur: field.options.onBlur,
focus: field.options.onFocus,
change: (value: FormValue): void => onUserAction('input', value),
},
}),
FileUploader: (field, onUserAction) => ({
Expand All @@ -203,8 +203,8 @@ const builtInComponents: Components = {
modifiers: `${field.status} ${field.options.modifiers || ''}`,
},
events: {
change: onUserAction,
focus: field.options.onFocus,
change: (value: FormValue): void => onUserAction('input', value),
},
}),
Dropdown: (field, onUserAction) => ({
Expand All @@ -223,8 +223,8 @@ const builtInComponents: Components = {
modifiers: `${field.status} ${field.options.modifiers || ''}`,
},
events: {
change: onUserAction,
focus: field.options.onFocus,
change: (value: FormValue): void => onUserAction('input', value),
},
}),
Checkbox: (field, onUserAction) => ({
Expand All @@ -241,8 +241,8 @@ const builtInComponents: Components = {
modifiers: `${field.status} ${field.options.modifiers || ''}`,
},
events: {
change: onUserAction,
focus: field.options.onFocus,
change: (value: FormValue): void => onUserAction('input', value),
},
}),
Radio: (field, onUserAction) => ({
Expand All @@ -259,8 +259,8 @@ const builtInComponents: Components = {
modifiers: `${field.status} ${field.options.modifiers || ''}`,
},
events: {
change: onUserAction,
focus: field.options.onFocus,
change: (value: FormValue): void => onUserAction('input', value),
},
}),
};
Expand Down Expand Up @@ -369,8 +369,8 @@ export default Vue.extend<Generic, Generic, Generic, Props>({
},
},
methods: {
onUserAction(newValue: FormValue): void {
this.$emit('userAction', { fieldId: this.id, type: 'input', value: newValue });
onUserAction(type: 'click' | 'input', newValue: FormValue): void {
this.$emit('userAction', { fieldId: this.id, type, value: newValue });
},
focusField(focusedValue: FormValue): void {
this.isActive = true;
Expand Down
17 changes: 13 additions & 4 deletions library/src/scripts/vue/components/__tests__/Field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jest.mock('sonar-ui/vue', () => {
click(): void {
self.$emit('click');
},
change(): void {
self.$emit('change');
},
keyDown(): void {
self.$emit('focus');
},
Expand Down Expand Up @@ -225,6 +228,7 @@ describe('vue/components/Field', () => {
userAction: onUserAction,
},
});
await wrapper.trigger('change');
await wrapper.trigger('keyDown');
await wrapper.vm.$nextTick();
expect(wrapper.html()).toMatchSnapshot();
Expand Down Expand Up @@ -290,6 +294,7 @@ describe('vue/components/Field', () => {
});
expect(wrapper.html()).toMatchSnapshot();
expect(onFocus).toHaveBeenCalledTimes(0);
await wrapper.trigger('change');
await wrapper.trigger('keyDown');
await wrapper.vm.$nextTick();
expect(wrapper.html()).toMatchSnapshot();
Expand Down Expand Up @@ -317,7 +322,7 @@ describe('vue/components/Field', () => {
expect(wrapper.html()).toMatchSnapshot();
});

test('FileUploader', () => {
test('FileUploader', async () => {
const wrapper = mount(Field, {
propsData: {
i18n,
Expand All @@ -333,10 +338,11 @@ describe('vue/components/Field', () => {
userAction: onUserAction,
},
});
await wrapper.trigger('change');
expect(wrapper.html()).toMatchSnapshot();
});

test('Dropdown', () => {
test('Dropdown', async () => {
const wrapper = mount(Field, {
propsData: {
i18n,
Expand All @@ -357,10 +363,11 @@ describe('vue/components/Field', () => {
userAction: onUserAction,
},
});
await wrapper.trigger('change');
expect(wrapper.html()).toMatchSnapshot();
});

test('Checkbox', () => {
test('Checkbox', async () => {
const wrapper = mount(Field, {
propsData: {
i18n,
Expand All @@ -381,10 +388,11 @@ describe('vue/components/Field', () => {
userAction: onUserAction,
},
});
await wrapper.trigger('change');
expect(wrapper.html()).toMatchSnapshot();
});

test('Radio', () => {
test('Radio', async () => {
const wrapper = mount(Field, {
propsData: {
i18n,
Expand All @@ -405,6 +413,7 @@ describe('vue/components/Field', () => {
userAction: onUserAction,
},
});
await wrapper.trigger('change');
expect(wrapper.html()).toMatchSnapshot();
});
});

0 comments on commit f8ca865

Please sign in to comment.