Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ export const AxisAnimationDisable = () => {
return group;
};

AxisAnimationDisable.tags = ['坐标轴', '动画', '更新'];
AxisAnimationDisable.tags = ['Axis', 'Animation', 'Update'];
2 changes: 1 addition & 1 deletion __tests__/integration/components/axis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ export { AxisAnimationUpdate13 } from './axis-animation-update-13';
export { AxisAnimationDisable } from './axis-animation-disable';
export { AxisLinearLabelSpacing } from './axis-linear-label-spacing';
export { AxisLinearTime1 } from './axis-linear-time-1';
export { AxisLinearTime2 } from './axis-linear-time-2';
export { AxisLinearTime2 } from './axis-linear-time-2';
7 changes: 4 additions & 3 deletions src/ui/checkbox/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ export const CHECKBOX_RECT_STYLE = {

const CHECKED_SHAPE_PATH = [
['M', 3, 6],
['L', '5', '8.5'],
['L', '8.5', '4'],
] as any;
['L', 5, 8.5],
['L', 8.5, 4],
];

export const CHECKED_SHAPE_STYLE = {
d: CHECKED_SHAPE_PATH,
lineWidth: 1,
cursor: 'pointer',
fill: 'none',
};
50 changes: 27 additions & 23 deletions src/ui/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,13 @@ import type { CheckboxOptions, CheckboxStyleProps } from './types';

import { CHECKBOX_RECT_STYLE, CHECKED_SHAPE_STYLE, LABEL_TEXT_STYLE } from './constant';

export type { CheckboxStyleProps, CheckboxOptions };

function getLablePosition(shape: Rect, spacing?: number) {
const bounds = shape.getLocalBounds();

return {
x: bounds.halfExtents[0] ? bounds.max[0] + (spacing || 0) : (shape.style.x as number),
y: bounds.halfExtents[1] ? (bounds.min[1] + bounds.max[1]) / 2 : (shape.style.y as number),
};
}
export type { CheckboxOptions };

export class Checkbox extends Component<CheckboxStyleProps> {
/**
* 组件 checkbox
*/
public static tag = 'checkbox';

/** checkbox 的背景方框组件 */
private checkboxBoxShape!: Rect;

/** 值 */
private checked!: boolean;

constructor(options: CheckboxOptions) {
Expand All @@ -40,37 +26,55 @@ export class Checkbox extends Component<CheckboxStyleProps> {
public render(attributes: Required<CheckboxStyleProps>, container: Group) {
const { checked, spacing } = attributes;
this.checked = !!checked;

const group = maybeAppend(container, '.checkbox-content', 'g').attr('className', 'checkbox-content').node();

const boxStyle = subStyleProps(attributes, 'box');
const checkedStyle = subStyleProps(attributes, 'checked');
const labelStyle = subStyleProps(attributes, 'label');

const checkboxStyle = {
...(this.checked ? CHECKBOX_RECT_STYLE.selected : CHECKBOX_RECT_STYLE.default),
...boxStyle,
};

const checkboxBoxCheckedStyle = { ...CHECKED_SHAPE_STYLE, ...checkedStyle };

// Create the box first
this.checkboxBoxShape = maybeAppend(group, '.checkbox-box', 'rect')
.styles({
className: 'checkbox-box',
zIndex: (group.style.zIndex || 0) - 1,
...checkboxStyle,
})
.node();

maybeAppend(this.checkboxBoxShape, '.checkbox-checked', 'path').styles({
className: 'checkbox-box-checked',
stroke: '#fff',
...checkboxBoxCheckedStyle,
});

const { x, y } = getLablePosition(this.checkboxBoxShape, Number(spacing));
// Only draw the checkmark if checked
if (this.checked) {
maybeAppend(group, '.checkbox-box-checked', 'path').styles({
className: 'checkbox-box-checked',
stroke: '#fff',
...CHECKED_SHAPE_STYLE,
...checkboxBoxCheckedStyle,
zIndex: 9, // Ensure it's drawn above the box
});
}

const { x, y } = this.getLabelPosition(this.checkboxBoxShape, Number(spacing));
maybeAppend(group, '.checkbox-label', 'text').styles({
className: 'checkbox-label',
x,
y,
...labelStyle,
});
}

private getLabelPosition(shape: Rect, spacing?: number) {
// getLocalBounds might differ slightly between rendering backends.
// Make sure the shape is rendered before calling this if needed.
const bounds = shape.getLocalBounds();
return {
x: bounds.halfExtents[0] ? bounds.max[0] + (spacing || 0) : (shape.style.x as number),
y: bounds.halfExtents[1] ? (bounds.min[1] + bounds.max[1]) / 2 : (shape.style.y as number),
};
}
Copy link
Member

Choose a reason for hiding this comment

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

why move this method into class?

}
Loading