Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite AdminPage.js into Typescript #2996

Merged
merged 21 commits into from
Aug 23, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions js/src/admin/components/AdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ export interface HTMLInputSettingsComponentOptions extends CommonSettingsItemOpt
type: HTMLInputTypes;
}

const BooleanSettingTypes = ['bool', 'checkbox', 'switch', 'boolean'] as const;
davwheat marked this conversation as resolved.
Show resolved Hide resolved
const SelectSettingTypes = ['select', 'dropdown', 'selectdropdown'] as const;

/**
* Valid options for the setting component builder to generate a Switch.
*/
export interface SwitchSettingComponentOptions extends CommonSettingsItemOptions {
type: 'bool' | 'checkbox' | 'switch' | 'boolean';
type: typeof BooleanSettingTypes[number];
}

/**
* Valid options for the setting component builder to generate a Select dropdown.
*/
export interface SelectSettingComponentOptions extends CommonSettingsItemOptions {
type: 'select' | 'dropdown' | 'selectdropdown';
type: typeof SelectSettingTypes[number];
/**
* Map of values to their labels
*/
Expand Down Expand Up @@ -178,7 +181,9 @@ export default class AdminPage<CustomAttrs extends IPageAttrs = IPageAttrs> exte

const [inputId, helpTextId] = [generateElementId(), generateElementId()];

if (['bool', 'checkbox', 'switch', 'boolean'].includes(type)) {
// Typescript being Typescript
// https://github.com/microsoft/TypeScript/issues/14520
if ((BooleanSettingTypes as readonly string[]).includes(type)) {
return (
// TODO: Add aria-describedby for switch help text.
//? Requires changes to Checkbox component to allow providing attrs directly for the element(s).
Expand All @@ -189,7 +194,7 @@ export default class AdminPage<CustomAttrs extends IPageAttrs = IPageAttrs> exte
<div className="helpText">{help}</div>
</div>
);
} else if (['select', 'dropdown', 'selectdropdown'].includes(type)) {
} else if ((SelectSettingTypes as readonly string[]).includes(type)) {
const { default: defaultValue, options, ...otherAttrs } = componentAttrs;

return (
Expand Down