diff --git a/__tests__/integration/components/axis/axis-animation-disable.ts b/__tests__/integration/components/axis/axis-animation-disable.ts index 468367e3..83d973fc 100644 --- a/__tests__/integration/components/axis/axis-animation-disable.ts +++ b/__tests__/integration/components/axis/axis-animation-disable.ts @@ -67,4 +67,4 @@ export const AxisAnimationDisable = () => { return group; }; -AxisAnimationDisable.tags = ['坐标轴', '动画', '更新']; +AxisAnimationDisable.tags = ['Axis', 'Animation', 'Update']; diff --git a/__tests__/integration/components/axis/index.ts b/__tests__/integration/components/axis/index.ts index 500899c6..a6c845b8 100644 --- a/__tests__/integration/components/axis/index.ts +++ b/__tests__/integration/components/axis/index.ts @@ -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'; \ No newline at end of file diff --git a/src/ui/checkbox/constant.ts b/src/ui/checkbox/constant.ts index 4a94aa63..c5519de0 100644 --- a/src/ui/checkbox/constant.ts +++ b/src/ui/checkbox/constant.ts @@ -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', }; diff --git a/src/ui/checkbox/index.ts b/src/ui/checkbox/index.ts index 2d0b9d29..990f6ad1 100644 --- a/src/ui/checkbox/index.ts +++ b/src/ui/checkbox/index.ts @@ -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 { - /** - * 组件 checkbox - */ public static tag = 'checkbox'; - /** checkbox 的背景方框组件 */ private checkboxBoxShape!: Rect; - /** 值 */ private checked!: boolean; constructor(options: CheckboxOptions) { @@ -40,32 +26,40 @@ export class Checkbox extends Component { public render(attributes: Required, 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, @@ -73,4 +67,14 @@ export class Checkbox extends Component { ...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), + }; + } }