feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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 { Text } from './text';
|
||||
|
||||
const meta: Meta<typeof Text> = {
|
||||
title: 'workflow setters/Text',
|
||||
component: Text,
|
||||
args: {
|
||||
value: '长文本',
|
||||
},
|
||||
tags: ['autodocs'],
|
||||
parameters: {
|
||||
layout: 'centered',
|
||||
},
|
||||
render: args => {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks -- linter-disable-autofix
|
||||
const [, updateArgs] = useArgs();
|
||||
|
||||
return (
|
||||
<Text
|
||||
{...args}
|
||||
onChange={newValue => {
|
||||
updateArgs({ ...args, value: newValue });
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
};
|
||||
export default meta;
|
||||
|
||||
type Story = StoryObj<typeof Text>;
|
||||
|
||||
export const Base: Story = {};
|
||||
|
||||
export const Placeholder: Story = {
|
||||
args: {
|
||||
value: '',
|
||||
placeholder: '请输入文字',
|
||||
},
|
||||
};
|
||||
|
||||
export const Width: Story = {
|
||||
args: {
|
||||
value: '长文本',
|
||||
placeholder: '请输入文字',
|
||||
width: 100,
|
||||
},
|
||||
};
|
||||
|
||||
export const MaxCount: Story = {
|
||||
args: {
|
||||
value: '长文本',
|
||||
maxCount: 100,
|
||||
},
|
||||
};
|
||||
|
||||
export const Readonly: Story = {
|
||||
args: {
|
||||
readonly: true,
|
||||
},
|
||||
};
|
||||
69
frontend/packages/workflow/setters/src/text/index.test.tsx
Normal file
69
frontend/packages/workflow/setters/src/text/index.test.tsx
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 { Text } from './text';
|
||||
|
||||
const mockProps = {
|
||||
value: '',
|
||||
onChange: vi.fn(),
|
||||
};
|
||||
|
||||
function inputValue(container: HTMLElement, value: string) {
|
||||
const inputElement = container.querySelector('textarea') as HTMLElement;
|
||||
fireEvent.input(inputElement, { target: { value } });
|
||||
}
|
||||
|
||||
describe('Text Setter', () => {
|
||||
it('renders correctly with default props', () => {
|
||||
const { container } = render(<Text {...mockProps} />);
|
||||
expect(container.firstChild).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('displays the correct placeholder text', () => {
|
||||
const placeholderText = 'Enter some text';
|
||||
render(<Text {...mockProps} placeholder={placeholderText} />);
|
||||
const inputElement = screen.getByPlaceholderText(placeholderText);
|
||||
expect(inputElement).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('calls onChange when text is entered', () => {
|
||||
const handleChange = vi.fn();
|
||||
const { container } = render(
|
||||
<Text {...mockProps} onChange={handleChange} />,
|
||||
);
|
||||
|
||||
const newValue = 'new text';
|
||||
inputValue(container, newValue);
|
||||
|
||||
screen.logTestingPlaygroundURL();
|
||||
|
||||
expect(handleChange).toHaveBeenCalledTimes(1);
|
||||
expect(handleChange).toHaveBeenCalledWith(newValue);
|
||||
});
|
||||
|
||||
it('does not allow input when readonly is true', () => {
|
||||
const handleChange = vi.fn();
|
||||
const { container } = render(<Text {...mockProps} readonly />);
|
||||
const newValue = 'new text';
|
||||
|
||||
inputValue(container, newValue);
|
||||
expect(handleChange).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
18
frontend/packages/workflow/setters/src/text/index.ts
Normal file
18
frontend/packages/workflow/setters/src/text/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 { Text } from './text';
|
||||
export type { TextOptions } from './text';
|
||||
@@ -0,0 +1,3 @@
|
||||
.readonly {
|
||||
pointer-events: none;
|
||||
}
|
||||
57
frontend/packages/workflow/setters/src/text/text.tsx
Normal file
57
frontend/packages/workflow/setters/src/text/text.tsx
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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 { TextArea } from '@coze-arch/coze-design';
|
||||
|
||||
import { type Setter } from '../types';
|
||||
|
||||
import styles from './text.module.less';
|
||||
|
||||
export interface TextOptions {
|
||||
placeholder?: string;
|
||||
width?: number | string;
|
||||
maxCount?: number;
|
||||
}
|
||||
|
||||
export const Text: Setter<string, TextOptions> = ({
|
||||
value,
|
||||
onChange,
|
||||
readonly = false,
|
||||
width = '100%',
|
||||
placeholder,
|
||||
maxCount,
|
||||
}) => {
|
||||
const handleChange = (newValue: string) => {
|
||||
onChange?.(newValue);
|
||||
};
|
||||
|
||||
return (
|
||||
<TextArea
|
||||
className={cx({ [styles.readonly]: readonly })}
|
||||
style={{
|
||||
width,
|
||||
}}
|
||||
value={value}
|
||||
onChange={handleChange}
|
||||
placeholder={placeholder}
|
||||
maxLength={maxCount}
|
||||
maxCount={maxCount}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user