Skip to content

feat(tag): simulate onChange in Tag #899

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 packages/radio-group/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const RadioGroup = forwardRef<HTMLDivElement, RadioGroupProps>(
};

const clone = cloneElement(child, {
onClick: handleChange,
onChange: handleChange,
disabled,
...child.props,
checked,
Expand Down
29 changes: 28 additions & 1 deletion packages/tag/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ export type TagProps = Omit<NativeProps, 'onClick'> & {
},
) => void;

/**
* Обработчик переключения
*/
onChange?: (
event?: Event,
payload?: {
checked: boolean;
name?: string;
},
) => void;

/**
* Набор цветов для компонента
*/
Expand All @@ -69,6 +80,8 @@ export const Tag = forwardRef<HTMLButtonElement, TagProps>(
dataTestId,
name,
colors = 'default',
value,
onChange,
onClick,
...restProps
},
Expand Down Expand Up @@ -97,8 +110,22 @@ export const Tag = forwardRef<HTMLButtonElement, TagProps>(
};

const handleClick = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
const payload = { name, checked: !checked };

if (onClick) {
onClick(event, { name, checked: !checked });
onClick(event, payload);
}

if (onChange) {
const input = document.createElement('input');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А если так

 const input = document.createElement('input');
 input.type = "checkbox";
 input.name = name;
 if (typeof checked !== 'undefined') input.checked = payload.checked;
 if (typeof value !== 'undefined') input.value = value.toString();

  input.onchange = e => onChange(e, payload);

  input.click();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тогда ведь click-event будет, а не change

if (typeof checked !== 'undefined') input.checked = payload.checked;
if (typeof value !== 'undefined') input.value = value.toString();

input.onchange = e => onChange(e, payload);

const changeEvent = new Event('change');
input.dispatchEvent(changeEvent);
}
};

Expand Down