feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 { String } from './string';
|
||||
|
||||
const meta: Meta<typeof String> = {
|
||||
title: 'workflow setters/String',
|
||||
component: String,
|
||||
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 (
|
||||
<String
|
||||
{...args}
|
||||
onChange={newValue => {
|
||||
updateArgs({ ...args, value: newValue });
|
||||
}}
|
||||
/>
|
||||
);
|
||||
},
|
||||
};
|
||||
export default meta;
|
||||
|
||||
type Story = StoryObj<typeof String>;
|
||||
|
||||
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: 20,
|
||||
},
|
||||
};
|
||||
|
||||
export const Readonly: Story = {
|
||||
args: {
|
||||
readonly: true,
|
||||
},
|
||||
};
|
||||
|
||||
export const TextMode: Story = {
|
||||
args: {
|
||||
textMode: true,
|
||||
},
|
||||
};
|
||||
75
frontend/packages/workflow/setters/src/string/index.test.tsx
Normal file
75
frontend/packages/workflow/setters/src/string/index.test.tsx
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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 { String } from './string';
|
||||
|
||||
const mockProps = {
|
||||
value: '',
|
||||
onChange: vi.fn(),
|
||||
};
|
||||
|
||||
function inputValue(container: HTMLElement, value: string) {
|
||||
const inputElement = container.querySelector('input') as HTMLElement;
|
||||
fireEvent.input(inputElement, { target: { value } });
|
||||
}
|
||||
|
||||
describe('String Setter', () => {
|
||||
it('renders correctly with default props', () => {
|
||||
const { container } = render(<String {...mockProps} />);
|
||||
expect(container.firstChild).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('displays the correct placeholder text', () => {
|
||||
const placeholderText = 'Enter some text';
|
||||
render(<String {...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(
|
||||
<String {...mockProps} onChange={handleChange} />,
|
||||
);
|
||||
|
||||
const newValue = 'new text';
|
||||
inputValue(container, newValue);
|
||||
|
||||
screen.logTestingPlaygroundURL();
|
||||
|
||||
expect(handleChange).toHaveBeenCalledTimes(1);
|
||||
expect(handleChange).toHaveBeenCalledWith(newValue);
|
||||
});
|
||||
|
||||
it('applies custom width when provided', () => {
|
||||
const customWidth = '50%';
|
||||
const { container } = render(<String {...mockProps} width={customWidth} />);
|
||||
expect(container.firstChild).toHaveStyle(`width: ${customWidth}`);
|
||||
});
|
||||
|
||||
it('does not allow input when readonly is true', () => {
|
||||
const handleChange = vi.fn();
|
||||
const { container } = render(<String {...mockProps} readonly />);
|
||||
const newValue = 'new text';
|
||||
|
||||
inputValue(container, newValue);
|
||||
expect(handleChange).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
18
frontend/packages/workflow/setters/src/string/index.ts
Normal file
18
frontend/packages/workflow/setters/src/string/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 { String } from './string';
|
||||
export type { StringOptions } from './string';
|
||||
@@ -0,0 +1,21 @@
|
||||
.suffix {
|
||||
overflow: hidden;
|
||||
|
||||
padding-right: 12px;
|
||||
padding-left: 8px;
|
||||
|
||||
font-size: 12px;
|
||||
font-weight: 400;
|
||||
font-style: normal;
|
||||
line-height: 16px;
|
||||
color: var(--light-usage-text-color-text-3, rgba(28, 31, 35, 35%));
|
||||
}
|
||||
|
||||
.readonly {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.text-mode {
|
||||
font-size: 12px;
|
||||
color: var(--coz-fg-primary);
|
||||
}
|
||||
81
frontend/packages/workflow/setters/src/string/string.tsx
Normal file
81
frontend/packages/workflow/setters/src/string/string.tsx
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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 { Input } from '@coze-arch/coze-design';
|
||||
|
||||
import { type Setter } from '../types';
|
||||
|
||||
import styles from './string.module.less';
|
||||
|
||||
export interface StringOptions {
|
||||
placeholder?: string;
|
||||
width?: number | string;
|
||||
maxCount?: number;
|
||||
// 新增这个配置的原因是readonly样式带有输入框 有些场景需要只展示文本
|
||||
textMode?: boolean;
|
||||
testId?: string;
|
||||
}
|
||||
|
||||
export const String: Setter<string, StringOptions> = ({
|
||||
value,
|
||||
onChange,
|
||||
readonly = false,
|
||||
width = 'auto',
|
||||
placeholder,
|
||||
maxCount,
|
||||
textMode = false,
|
||||
testId,
|
||||
}) => {
|
||||
const handleChange = (newValue: string) => {
|
||||
onChange?.(newValue);
|
||||
};
|
||||
|
||||
if (textMode) {
|
||||
return (
|
||||
<div style={{ width }} className={styles['text-mode']}>
|
||||
{value}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Input
|
||||
size="small"
|
||||
data-testid={testId}
|
||||
className={cx({
|
||||
[styles.readonly]: readonly,
|
||||
})}
|
||||
style={{
|
||||
width,
|
||||
}}
|
||||
value={value}
|
||||
onChange={handleChange}
|
||||
readonly={readonly}
|
||||
placeholder={placeholder}
|
||||
maxLength={maxCount}
|
||||
suffix={
|
||||
maxCount === undefined ? null : (
|
||||
<span className={styles.suffix}>
|
||||
{`${value?.length || 0}/${maxCount}`}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user