Skip to content

Commit

Permalink
feat(core): Improve type-safety of custom ui input config
Browse files Browse the repository at this point in the history
Relates to #414
  • Loading branch information
michaelbromley committed Jul 30, 2020
1 parent e72f0b3 commit d0cc096
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
28 changes: 28 additions & 0 deletions packages/common/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ export type CustomFieldType = 'string' | 'localeString' | 'int' | 'float' | 'boo
*/
export type ConfigArgType = 'string' | 'int' | 'float' | 'boolean' | 'datetime' | 'ID';

/**
* The ids of the default form input components that ship with the
* Admin UI.
*/
export type DefaultFormComponentId =
| 'boolean-form-input'
| 'currency-form-input'
| 'date-form-input'
| 'facet-value-form-input'
| 'number-form-input'
| 'select-form-input'
| 'text-form-input';

/**
* Used to defined the expected arguments for a given default form input component.
*/
type DefaultFormConfigHash = {
'date-form-input': { min?: string; max?: string; yearRange?: number };
'number-form-input': { min?: number; max?: number; step?: number; prefix?: string; suffix?: string };
'select-form-input': { options?: Array<{ value: string; label?: string }> };
'boolean-form-input': {};
'currency-form-input': {};
'facet-value-form-input': {};
'text-form-input': {};
};

export type DefaultFormComponentConfig<T extends DefaultFormComponentId> = DefaultFormConfigHash[T];

export type CustomFieldsObject = { [key: string]: any };

/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/e2e/shipping-method.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('ShippingMethod resolver', () => {
},
{
ui: {
component: 'number-form-input',
suffix: '%',
},
description: null,
Expand Down
22 changes: 16 additions & 6 deletions packages/core/src/common/configurable-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
Maybe,
StringFieldOption,
} from '@vendure/common/lib/generated-types';
import { ConfigArgType, ID } from '@vendure/common/lib/shared-types';
import { ConfigArgType, DefaultFormComponentConfig, ID } from '@vendure/common/lib/shared-types';
import { assertNever } from '@vendure/common/lib/shared-utils';
import { simpleDeepClone } from '@vendure/common/lib/simple-deep-clone';

import { RequestContext } from '../api/common/request-context';

Expand Down Expand Up @@ -39,15 +38,26 @@ import { InjectableStrategy } from './types/injectable-strategy';
*/
export type LocalizedStringArray = Array<Omit<LocalizedString, '__typename'>>;

export type UiComponentConfig =
| ({ component: 'number-form-input' } & DefaultFormComponentConfig<'number-form-input'>)
| ({ component: 'date-form-input' } & DefaultFormComponentConfig<'date-form-input'>)
| ({ component: 'select-form-input' } & DefaultFormComponentConfig<'select-form-input'>)
| ({ component: 'text-form-input' } & DefaultFormComponentConfig<'text-form-input'>)
| ({ component: 'boolean-form-input' } & DefaultFormComponentConfig<'boolean-form-input'>)
| ({ component: 'currency-form-input' } & DefaultFormComponentConfig<'currency-form-input'>)
| ({ component: 'facet-value-form-input' } & DefaultFormComponentConfig<'facet-value-form-input'>)
| { component: string; [prop: string]: any };

const d: UiComponentConfig = {
component: 'number-form-input',
};

export interface ConfigArgCommonDef<T extends ConfigArgType> {
type: T;
list?: boolean;
label?: LocalizedStringArray;
description?: LocalizedStringArray;
ui?: {
component?: string;
[prop: string]: any;
};
ui?: UiComponentConfig;
}

export type ConfigArgListDef<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const variantNameCollectionFilter = new CollectionFilter({
operator: {
type: 'string',
ui: {
component: 'select-form-input',
options: [
{ value: 'startsWith' },
{ value: 'endsWith' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const discountOnItemWithFacets = new PromotionItemAction({
discount: {
type: 'int',
ui: {
component: 'number-form-input',
suffix: '%',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const orderPercentageDiscount = new PromotionOrderAction({
discount: {
type: 'int',
ui: {
component: 'number-form-input',
suffix: '%',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const defaultShippingCalculator = new ShippingCalculator({
},
taxRate: {
type: 'int',
ui: { suffix: '%' },
ui: { component: 'number-form-input', suffix: '%' },
label: [{ languageCode: LanguageCode.en, value: 'Tax rate' }],
},
},
Expand Down

0 comments on commit d0cc096

Please sign in to comment.