Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
feat: export common components (#1053)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneFreeman authored Jan 4, 2024
1 parent 99e6a3c commit cafb8c4
Show file tree
Hide file tree
Showing 63 changed files with 265 additions and 115 deletions.
2 changes: 2 additions & 0 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Field class names updated to FieldWrapper
- TextField class names updated to TextInput
2 changes: 1 addition & 1 deletion packages/core/src/components/common/alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const classes = generateClassNames('Alert', [
'confirm-button',
]);

interface AlertProps {
export interface AlertProps {
title: string | { key: string; options?: Record<string, unknown> };
body: string | { key: string; options?: Record<string, unknown> };
okay?: string | { key: string; options?: Record<string, unknown> };
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/components/common/alert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Alert, default as alert } from './Alert';
export type { AlertDialogProps, AlertProps } from './Alert';
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export const classes = generateClassNames('Autocomplete', [
'checkmark-icon',
]);

export interface Option {
export interface AutocompleteOption {
label: string;
value: string;
}

function getOptionLabelAndValue(option: string | Option): Option {
function getOptionLabelAndValue(option: string | AutocompleteOption): AutocompleteOption {
if (option && typeof option === 'object' && 'label' in option && 'value' in option) {
return option;
}
Expand All @@ -53,7 +53,7 @@ export type AutocompleteChangeEventHandler = (value: string | string[]) => void;
export interface AutocompleteProps {
label: ReactNode | ReactNode[];
value: string | string[] | null;
options: string[] | Option[];
options: string[] | AutocompleteOption[];
disabled?: boolean;
required?: boolean;
inputRef?: Ref<HTMLInputElement>;
Expand Down Expand Up @@ -87,7 +87,7 @@ const Autocomplete: FC<AutocompleteProps> = ({
);

const handleChange = useCallback(
(selectedValue: Option | readonly Option[] | null) => {
(selectedValue: AutocompleteOption | readonly AutocompleteOption[] | null) => {
if (selectedValue === null) {
if (Array.isArray(value)) {
onChange([]);
Expand Down Expand Up @@ -127,7 +127,7 @@ const Autocomplete: FC<AutocompleteProps> = ({

return acc;
},
{} as Record<string, Option>,
{} as Record<string, AutocompleteOption>,
),
[finalOptions],
);
Expand Down Expand Up @@ -238,7 +238,7 @@ const Autocomplete: FC<AutocompleteProps> = ({
{groupedOptions.length > 0 ? (
groupedOptions.map((option, index) => {
const { label: optionLabel, value: optionValue } = getOptionLabelAndValue(
option as Option,
option as AutocompleteOption,
);

const selected = Array.isArray(value)
Expand All @@ -248,7 +248,7 @@ const Autocomplete: FC<AutocompleteProps> = ({
return (
<li
key={index}
{...getOptionProps({ option: option as Option, index })}
{...getOptionProps({ option: option as AutocompleteOption, index })}
className={classNames(classes.option, selected && classes['option-selected'])}
data-testid={`autocomplete-option-${optionValue}`}
>
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/components/common/autocomplete/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { default as Autocomplete } from './Autocomplete';
export type {
AutocompleteChangeEventHandler,
AutocompleteProps,
AutocompleteOption,
} from './Autocomplete';
14 changes: 7 additions & 7 deletions packages/core/src/components/common/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { CSSProperties, FC, MouseEventHandler, ReactNode, Ref } from 'react

import './Button.css';

export interface BaseBaseProps {
export interface ButtonBaseProps {
variant?: 'contained' | 'outlined' | 'text';
color?: 'primary' | 'secondary' | 'success' | 'error' | 'warning' | 'info';
size?: 'medium' | 'small';
Expand All @@ -23,27 +23,27 @@ export interface BaseBaseProps {
onClick?: MouseEventHandler;
}

export interface ButtonProps extends BaseBaseProps {
export interface ButtonProps extends ButtonBaseProps {
disabled?: boolean;
buttonRef?: Ref<HTMLButtonElement>;
'aria-label'?: string;
}

export interface ButtonInternalLinkProps extends BaseBaseProps {
export interface ButtonInternalLinkProps extends ButtonBaseProps {
to: string;
linkRef?: Ref<HTMLAnchorElement>;
}

export interface ButtonExternalLinkProps extends BaseBaseProps {
export interface ButtonExternalLinkProps extends ButtonBaseProps {
href: string;
linkRef?: Ref<HTMLAnchorElement>;
}

export type LinkProps = ButtonInternalLinkProps | ButtonExternalLinkProps;
export type ButtonLinkProps = ButtonInternalLinkProps | ButtonExternalLinkProps;

export type ButtonLinkProps = ButtonProps | LinkProps;
export type ButtonOrButtonLinkProps = ButtonProps | ButtonLinkProps;

const Button: FC<ButtonLinkProps> = ({
const Button: FC<ButtonOrButtonLinkProps> = ({
variant = 'contained',
color = 'primary',
size = 'medium',
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/components/common/button/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export type IconButtonInternalLinkProps = Omit<ButtonInternalLinkProps, 'childre
export type IconButtonExternalLinkProps = Omit<ButtonExternalLinkProps, 'children' | 'className'> &
BaseIconButtonProps;

export type IconLinkProps = IconButtonInternalLinkProps | IconButtonExternalLinkProps;
export type IconButtonLinkProps = IconButtonInternalLinkProps | IconButtonExternalLinkProps;

export type IconButtonLinkProps = IconButtonProps | IconLinkProps;
export type IconButtonOrIconButtonLinkProps = IconButtonProps | IconButtonLinkProps;

const IconButton: FC<IconButtonLinkProps> = ({
const IconButton: FC<IconButtonOrIconButtonLinkProps> = ({
icon: Icon,
size = 'medium',
rootClassName,
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/components/common/button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export { default as Button } from './Button';
export type {
ButtonBaseProps,
ButtonProps,
ButtonInternalLinkProps,
ButtonExternalLinkProps,
ButtonLinkProps,
ButtonOrButtonLinkProps,
} from './Button';

export { default as IconButton } from './IconButton';
export type {
BaseIconButtonProps,
IconButtonProps,
IconButtonInternalLinkProps,
IconButtonExternalLinkProps,
IconButtonLinkProps,
IconButtonOrIconButtonLinkProps,
} from './IconButton';

export { default as useButtonClassNames, buttonClasses } from './useButtonClassNames';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from 'react';

import { generateClassNames } from '@staticcms/core/lib/util/theming.util';

import type { BaseBaseProps } from './Button';
import type { ButtonBaseProps } from './Button';

export const buttonClasses = generateClassNames('Button', [
'root-sm',
Expand Down Expand Up @@ -30,8 +30,8 @@ export const buttonClasses = generateClassNames('Button', [
]);

const classes: Record<
Required<BaseBaseProps>['variant'],
Record<Required<BaseBaseProps>['color'], string>
Required<ButtonBaseProps>['variant'],
Record<Required<ButtonBaseProps>['color'], string>
> = {
contained: {
primary: 'CMS_Button_contained-primary',
Expand Down Expand Up @@ -59,10 +59,10 @@ const classes: Record<
},
};

export default function useButtonClassNames(
variant: Required<BaseBaseProps>['variant'],
color: Required<BaseBaseProps>['color'],
size: Required<BaseBaseProps>['size'],
function useButtonClassNames(
variant: Required<ButtonBaseProps>['variant'],
color: Required<ButtonBaseProps>['color'],
size: Required<ButtonBaseProps>['size'],
rounded: boolean | 'no-padding',
) {
let mainClass = size === 'small' ? 'CMS_Button_root-sm' : 'CMS_Button_root';
Expand All @@ -74,3 +74,5 @@ export default function useButtonClassNames(

return useMemo(() => `${mainClass} ${classes[variant][color]}`, [color, mainClass, variant]);
}

export default useButtonClassNames;
2 changes: 1 addition & 1 deletion packages/core/src/components/common/card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { FC, ReactNode } from 'react';

import './Card.css';

interface CardProps {
export interface CardProps {
children: ReactNode | ReactNode[];
className?: string;
title?: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/components/common/card/CardActionArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import cardClasses from './Card.classes';

import type { FC, MouseEvent, ReactNode } from 'react';

interface BaseCardActionAreaProps {
export interface BaseCardActionAreaProps {
children: ReactNode;
className?: string;
}

interface CardActionAreaPropsLink extends BaseCardActionAreaProps {
export interface CardActionAreaPropsLink extends BaseCardActionAreaProps {
to: string;
}

interface CardActionAreaPropsButton extends BaseCardActionAreaProps {
export interface CardActionAreaPropsButton extends BaseCardActionAreaProps {
onClick: (event: MouseEvent) => void;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/common/card/CardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cardClasses from './Card.classes';

import type { FC, ReactNode } from 'react';

interface CardContentProps {
export interface CardContentProps {
children: ReactNode;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/common/card/CardHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cardClasses from './Card.classes';

import type { FC, ReactNode } from 'react';

interface CardHeaderProps {
export interface CardHeaderProps {
children: ReactNode;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/common/card/CardMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
UnknownField,
} from '@staticcms/core';

interface CardMediaProps<EF extends BaseField> {
export interface CardMediaProps<EF extends BaseField> {
image: string;
width?: string | number;
height?: string | number;
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/components/common/card/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export { default as Card } from './Card';
export type { CardProps } from './Card';

export { default as CardActionArea } from './CardActionArea';
export type {
BaseCardActionAreaProps,
CardActionAreaProps,
CardActionAreaPropsButton,
CardActionAreaPropsLink,
} from './CardActionArea';

export { default as CardContent } from './CardContent';
export type { CardContentProps } from './CardContent';

export { default as CardHeader } from './CardHeader';
export type { CardHeaderProps } from './CardHeader';

export { default as CardMedia } from './CardMedia';
export type { CardMediaProps } from './CardMedia';
2 changes: 2 additions & 0 deletions packages/core/src/components/common/checkbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as Checkbox } from './Checkbox';
export type { CheckboxProps } from './Checkbox';
2 changes: 1 addition & 1 deletion packages/core/src/components/common/confirm/Confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const classes = generateClassNames('Confirm', [
'cancel-button',
]);

interface ConfirmProps {
export interface ConfirmProps {
title: string | { key: string; options?: Record<string, unknown> };
body: string | { key: string; options?: Record<string, unknown> };
cancel?: string | { key: string; options?: Record<string, unknown> };
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/components/common/confirm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Confirm, default as confirm } from './Confirm';
export type { ConfirmDialogProps, ConfirmProps } from './Confirm';
Loading

0 comments on commit cafb8c4

Please sign in to comment.