feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import type { StoryObj, Meta } from '@storybook/react';
|
||||
import { useArgs } from '@storybook/preview-api';
|
||||
|
||||
import { Number } from './number';
|
||||
|
||||
const meta: Meta<typeof Number> = {
|
||||
title: 'workflow setters/Number',
|
||||
component: Number,
|
||||
parameters: {
|
||||
layout: 'centered',
|
||||
},
|
||||
args: {
|
||||
value: 10,
|
||||
},
|
||||
tags: ['autodocs'],
|
||||
render: args => {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks -- linter-disable-autofix
|
||||
const [, updateArgs] = useArgs();
|
||||
|
||||
return (
|
||||
<Number
|
||||
{...args}
|
||||
onChange={newValue => {
|
||||
updateArgs({ ...args, value: newValue });
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
};
|
||||
export default meta;
|
||||
|
||||
type Story = StoryObj<typeof Number>;
|
||||
|
||||
export const Base: Story = {};
|
||||
|
||||
export const Placeholder: Story = {
|
||||
args: {
|
||||
value: undefined,
|
||||
placeholder: '请输入数字',
|
||||
},
|
||||
};
|
||||
|
||||
export const Width: Story = {
|
||||
args: {
|
||||
width: 100,
|
||||
},
|
||||
};
|
||||
|
||||
export const MaxMinStep: Story = {
|
||||
args: {
|
||||
max: 100,
|
||||
min: 10,
|
||||
step: 10,
|
||||
},
|
||||
};
|
||||
|
||||
export const Readonly: Story = {
|
||||
args: {
|
||||
readonly: true,
|
||||
},
|
||||
};
|
||||
|
||||
export const Slider: Story = {
|
||||
args: {
|
||||
mode: 'slider',
|
||||
width: 200,
|
||||
min: 1,
|
||||
max: 10,
|
||||
step: 1,
|
||||
},
|
||||
};
|
||||
160
frontend/packages/workflow/setters/src/number/index.test.tsx
Normal file
160
frontend/packages/workflow/setters/src/number/index.test.tsx
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import '@testing-library/jest-dom';
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
|
||||
import { Number } from './number';
|
||||
|
||||
const mockProps = {
|
||||
value: 0,
|
||||
onChange: vi.fn(),
|
||||
};
|
||||
|
||||
async function clickNumberButtonDown(container: HTMLElement) {
|
||||
await clickNumberButton(container, 'down');
|
||||
}
|
||||
|
||||
async function clickNumberButtonUp(container: HTMLElement) {
|
||||
await clickNumberButton(container, 'up');
|
||||
}
|
||||
|
||||
async function clickNumberButton(container: HTMLElement, arrow: 'up' | 'down') {
|
||||
// 先触发 hover
|
||||
const numberContainer = container.firstChild as HTMLElement;
|
||||
fireEvent.mouseEnter(numberContainer);
|
||||
|
||||
// 等待下一个事件循环
|
||||
await Promise.resolve();
|
||||
|
||||
const upButton = container.querySelector(
|
||||
'.semi-input-number-button-up',
|
||||
) as HTMLElement;
|
||||
const downButton = container.querySelector(
|
||||
'.semi-input-number-button-down',
|
||||
) as HTMLElement;
|
||||
|
||||
if (arrow === 'up') {
|
||||
fireEvent.mouseDown(upButton);
|
||||
fireEvent.mouseUp(upButton);
|
||||
} else {
|
||||
fireEvent.mouseDown(downButton);
|
||||
fireEvent.mouseUp(downButton);
|
||||
}
|
||||
}
|
||||
|
||||
function inputValue(container: HTMLElement, value: number) {
|
||||
const inputElement = container.querySelector('input') as HTMLElement;
|
||||
fireEvent.input(inputElement, { target: { value } });
|
||||
}
|
||||
|
||||
describe('Number Setter', () => {
|
||||
it('renders correctly with default props', () => {
|
||||
const { container } = render(
|
||||
// @ts-expect-error -- mock
|
||||
<Number {...mockProps} value={0} onChange={vi.fn} />,
|
||||
);
|
||||
|
||||
expect(container.firstChild).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('displays the correct placeholder text', () => {
|
||||
const placeholderText = 'Enter a number';
|
||||
render(<Number {...mockProps} value={0} placeholder={placeholderText} />);
|
||||
const inputElement = screen.getByPlaceholderText(placeholderText);
|
||||
expect(inputElement).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onChange when value is changed', () => {
|
||||
const newValue = 5;
|
||||
const handleChange = vi.fn();
|
||||
|
||||
const { container } = render(
|
||||
<Number {...mockProps} value={0} onChange={handleChange} />,
|
||||
);
|
||||
|
||||
inputValue(container, newValue);
|
||||
|
||||
expect(handleChange).toHaveBeenCalledTimes(1);
|
||||
expect(handleChange).toHaveBeenCalledWith(newValue);
|
||||
});
|
||||
|
||||
it('applies custom width when provided', () => {
|
||||
const customWidth = '50%';
|
||||
const { container } = render(
|
||||
<Number {...mockProps} value={0} width={customWidth} />,
|
||||
);
|
||||
|
||||
expect(container.firstChild).toHaveStyle(`width: ${customWidth}`);
|
||||
});
|
||||
|
||||
it('is readonly when readonly prop is true', () => {
|
||||
const handleChange = vi.fn();
|
||||
const { container } = render(
|
||||
<Number {...mockProps} value={0} onChange={handleChange} readonly />,
|
||||
);
|
||||
|
||||
inputValue(container, 1);
|
||||
expect(handleChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not allow values less than min', async () => {
|
||||
const handleChange = vi.fn();
|
||||
const min = 0;
|
||||
const { container } = render(
|
||||
<Number {...mockProps} value={min} onChange={handleChange} min={min} />,
|
||||
);
|
||||
|
||||
await clickNumberButtonDown(container);
|
||||
expect(handleChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not allow values greater than max', async () => {
|
||||
const handleChange = vi.fn();
|
||||
const max = 10;
|
||||
const { container } = render(
|
||||
<Number {...mockProps} value={max} onChange={handleChange} max={max} />,
|
||||
);
|
||||
|
||||
await clickNumberButtonUp(container);
|
||||
expect(handleChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('increments value by step when using arrow up', async () => {
|
||||
const handleChange = vi.fn();
|
||||
const step = 2;
|
||||
const { container } = render(
|
||||
<Number {...mockProps} value={1} onChange={handleChange} step={step} />,
|
||||
);
|
||||
|
||||
await clickNumberButtonUp(container);
|
||||
expect(handleChange).toBeCalledTimes(1);
|
||||
expect(handleChange).toHaveBeenCalledWith(3);
|
||||
});
|
||||
|
||||
it('decrements value by step when using arrow down', async () => {
|
||||
const handleChange = vi.fn();
|
||||
const step = 2;
|
||||
const { container } = render(
|
||||
<Number {...mockProps} value={3} onChange={handleChange} step={step} />,
|
||||
);
|
||||
|
||||
await clickNumberButtonDown(container);
|
||||
expect(handleChange).toBeCalledTimes(1);
|
||||
expect(handleChange).toHaveBeenCalledWith(1);
|
||||
});
|
||||
});
|
||||
18
frontend/packages/workflow/setters/src/number/index.ts
Normal file
18
frontend/packages/workflow/setters/src/number/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export { Number } from './number';
|
||||
export type { NumberOptions } from './number';
|
||||
@@ -0,0 +1,20 @@
|
||||
.readonly {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.slider {
|
||||
position: relative;
|
||||
|
||||
:global {
|
||||
|
||||
// semi-slider放到flex布局会无法拖动 这里样式可以修复这个问题
|
||||
.semi-slider {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
|
||||
.semi-slider-wrapper {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
90
frontend/packages/workflow/setters/src/number/number.tsx
Normal file
90
frontend/packages/workflow/setters/src/number/number.tsx
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import cx from 'classnames';
|
||||
import { CozInputNumber, Slider } from '@coze-arch/coze-design';
|
||||
|
||||
import type { Setter } from '../types';
|
||||
|
||||
import styles from './number.module.less';
|
||||
|
||||
export interface NumberOptions {
|
||||
placeholder?: string;
|
||||
width?: number | string;
|
||||
step?: number;
|
||||
max?: number;
|
||||
min?: number;
|
||||
mode?: 'input' | 'slider';
|
||||
size?: 'small' | 'default';
|
||||
style?: React.CSSProperties;
|
||||
}
|
||||
|
||||
export const Number: Setter<number, NumberOptions> = ({
|
||||
value,
|
||||
onChange,
|
||||
width = '100%',
|
||||
readonly = false,
|
||||
mode = 'input',
|
||||
max,
|
||||
min,
|
||||
step,
|
||||
placeholder,
|
||||
size = 'default',
|
||||
style = {},
|
||||
}) => {
|
||||
const handleChange = (newValue: number | string) => {
|
||||
if (typeof newValue === 'number' && !readonly) {
|
||||
onChange?.(newValue);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSliderChange = (newValue?: number | number[]) => {
|
||||
if (typeof newValue === 'number' && !readonly) {
|
||||
onChange?.(newValue);
|
||||
}
|
||||
};
|
||||
|
||||
if (mode === 'slider') {
|
||||
return (
|
||||
<div className={styles.slider} style={{ width, ...style }}>
|
||||
<Slider
|
||||
className={cx({ [styles.readonly]: readonly })}
|
||||
value={value}
|
||||
min={min}
|
||||
max={max}
|
||||
step={step}
|
||||
onChange={handleSliderChange}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<CozInputNumber
|
||||
value={value}
|
||||
onChange={handleChange}
|
||||
className={cx({ [styles.readonly]: readonly })}
|
||||
style={{ width, ...style }}
|
||||
max={max}
|
||||
min={min}
|
||||
step={step}
|
||||
placeholder={placeholder}
|
||||
size={size}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user