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

Migrate components default props usage #3027

Merged
merged 15 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
trim_trailing_whitespace = true
24 changes: 12 additions & 12 deletions design-system/src/theme-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ const applyTheme = ({
};

type ThemeProviderProps = {
parentSelector: typeof defaultParentSelector;
theme: string;
parentSelector?: typeof defaultParentSelector;
theme?: string;
themeOverrides?: Record<string, string>;
};

const ThemeProvider = (props: ThemeProviderProps) => {
const parentSelectorRef = useRef(props.parentSelector);
const ThemeProvider = ({
parentSelector = defaultParentSelector,
theme = 'default',
...props
}: ThemeProviderProps) => {
const parentSelectorRef = useRef(parentSelector);
const themeNameRef = useRef<string>();
const themeOverridesRef = useRef<Record<string, string>>();

Expand All @@ -80,26 +84,22 @@ const ThemeProvider = (props: ThemeProviderProps) => {
// provided include a new object with the same theme overrides
// (eg: users providing an inline object as prop to the ThemeProvider)
if (
themeNameRef.current !== props.theme ||
themeNameRef.current !== theme ||
!isEqual(themeOverridesRef.current, props.themeOverrides)
) {
themeNameRef.current = props.theme;
themeNameRef.current = theme;
themeOverridesRef.current = props.themeOverrides;

applyTheme({
newTheme: props.theme,
newTheme: theme,
parentSelector: parentSelectorRef.current,
themeOverrides: props.themeOverrides,
});
}
}, [props.theme, props.themeOverrides]);
}, [theme, props.themeOverrides]);

return null;
};
ThemeProvider.defaultProps = {
parentSelector: defaultParentSelector,
theme: 'default',
};

type TUseThemeResult = {
theme: ThemeName;
Expand Down
36 changes: 25 additions & 11 deletions packages/calendar-utils/src/calendar-body/calendar-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ export type TCalendarBody = {
theme?: Theme;
};

const defaultProps: Pick<TCalendarBody, 'isClearable'> = {
isClearable: true,
};

export const CalendarBody = (props: TCalendarBody) => {
export const CalendarBody = ({
isClearable = true,
...props
}: TCalendarBody) => {
const [isFocused, toggleIsFocused] = useToggleState(false);

const onInputFocus = props.inputProps?.onFocus;
Expand Down Expand Up @@ -130,18 +129,29 @@ export const CalendarBody = (props: TCalendarBody) => {

return (
<Inline alignItems="center">
<div css={getInputContainerStyles(props, { isFocused })}>
<div
css={getInputContainerStyles(
{
isClearable,
...props,
},
{ isFocused }
)}
>
<input
ref={props.inputRef}
{...props.inputProps}
disabled={props.isDisabled}
readOnly={props.isReadOnly}
css={getDateTimeInputStyles(props)}
css={getDateTimeInputStyles({
isClearable,
...props,
})}
onFocus={handleInputFocus}
onBlur={handleInputBlur}
aria-readonly={props.isReadOnly}
/>
{!disabledOrReadOnly && props.hasSelection && props.isClearable && (
{!disabledOrReadOnly && props.hasSelection && isClearable && (
<ClearSection
isCondensed={props.isCondensed}
hasError={props.hasError}
Expand All @@ -153,7 +163,13 @@ export const CalendarBody = (props: TCalendarBody) => {
)}
<button
type="button"
css={getCalendarIconContainerStyles(props, { isFocused })}
css={getCalendarIconContainerStyles(
{
isClearable,
...props,
},
{ isFocused }
)}
{...props.toggleButtonProps}
onFocus={handleToggleFocus}
onBlur={handleToggleBlur}
Expand All @@ -178,6 +194,4 @@ export const CalendarBody = (props: TCalendarBody) => {

CalendarBody.displayName = 'CalendarBody';

CalendarBody.defaultProps = defaultProps;

export default CalendarBody;
54 changes: 31 additions & 23 deletions packages/components/avatar/src/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ export type TInitialsProps = Pick<
'firstName' | 'lastName' | 'size'
>;

const defaultProps: Pick<
TAvatarProps,
'firstName' | 'lastName' | 'isHighlighted' | 'size' | 'color'
> = {
firstName: '',
lastName: '',
isHighlighted: false,
size: 's',
color: 'accent',
};

const getFirstChar = (str: string) =>
typeof str === 'string' ? str.trim().slice(0, 1).toUpperCase() : '';

Expand Down Expand Up @@ -127,11 +116,35 @@ const Initials = (props: TInitialsProps) => {
};
Initials.displayName = 'Initials';

const Avatar = (props: TAvatarProps) => {
const avatarSize = getWidthSize(props.size);
const foregroundColor = getForegroundColor(props.color);
const Avatar = ({
firstName = '',
lastName = '',
isHighlighted = false,
size = 's',
color = 'accent',
...props
}: TAvatarProps) => {
const avatarSize = getWidthSize(size);
const foregroundColor = getForegroundColor(color);
return (
<div css={getAvatarStyles(props)} {...filterDataAttributes(props)}>
<div
css={getAvatarStyles({
firstName,
lastName,
isHighlighted,
size,
color,
...props,
})}
{...filterDataAttributes({
firstName,
lastName,
isHighlighted,
size,
color,
...props,
})}
>
{props?.icon ? (
<div
css={css`
Expand All @@ -149,20 +162,15 @@ const Avatar = (props: TAvatarProps) => {
<>
<GravatarImg
gravatarHash={props.gravatarHash}
size={props.size}
isHighlighted={props.isHighlighted}
/>
<Initials
size={props.size}
firstName={props.firstName}
lastName={props.lastName}
size={size}
isHighlighted={isHighlighted}
/>
<Initials size={size} firstName={firstName} lastName={lastName} />
</>
)}
</div>
);
};
Avatar.displayName = 'Avatar';
Avatar.defaultProps = defaultProps;

export default Avatar;
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,29 @@ export type TAccessibleButtonProps = {
buttonAttributes?: Record<string, unknown>;
};

const defaultProps: Pick<
TAccessibleButtonProps,
'type' | 'buttonAttributes' | 'isToggleButton' | 'isToggled'
> = {
type: 'button',
buttonAttributes: {},
isToggleButton: false,
isToggled: false,
};

const AccessibleButton = forwardRef<HTMLButtonElement, TAccessibleButtonProps>(
(props: TAccessibleButtonProps, ref) => {
(
{
type = 'button',
buttonAttributes = {},
isToggleButton = false,
isToggled = false,
...props
}: TAccessibleButtonProps,
ref
) => {
warning(
props.as ? isValidElementType(props.as) : true,
`ui-kit/AccessibleButton: "as" must be a valid element type.`
);

warning(
!(props.as && props.type !== 'button'),
!(props.as && type !== 'button'),
`ui-kit/AccessibleButton: "type" does not have any effect when "as" is set.`
);

warning(
!(props.isToggleButton && props.isToggled === undefined),
!(isToggleButton && isToggled === undefined),
`ui-kit/AccessibleButton: "isToggled" is required if "isToggleButton" is "true"`
);

Expand Down Expand Up @@ -144,7 +143,7 @@ const AccessibleButton = forwardRef<HTMLButtonElement, TAccessibleButtonProps>(
let buttonProps = {};
if (isButton) {
buttonProps = {
type: props.type,
type: type,
};
} else {
buttonProps = {
Expand Down Expand Up @@ -177,8 +176,8 @@ const AccessibleButton = forwardRef<HTMLButtonElement, TAccessibleButtonProps>(
className={props.className}
disabled={props.isDisabled}
aria-disabled={props.isDisabled}
{...(props.isToggleButton ? { 'aria-pressed': props.isToggled } : {})}
{...omit(props.buttonAttributes, propsToOmit)}
{...(isToggleButton ? { 'aria-pressed': isToggled } : {})}
{...omit(buttonAttributes, propsToOmit)}
{...buttonProps}
{...filterAriaAttributes(props)}
{...filterDataAttributes(props)}
Expand All @@ -189,6 +188,5 @@ const AccessibleButton = forwardRef<HTMLButtonElement, TAccessibleButtonProps>(
}
);
AccessibleButton.displayName = 'AccessibleButton';
AccessibleButton.defaultProps = defaultProps;

export default AccessibleButton;
Loading
Loading