Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 默认值没有添加上单位, 点击事件阻止冒泡 #9204

Merged
merged 1 commit into from
Dec 22, 2023
Merged
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
16 changes: 13 additions & 3 deletions packages/amis-ui/src/components/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,16 @@ export class NumberInput extends React.Component<NumberProps, NumberState> {
}

@autobind
handleEnhanceModeChange(action: 'add' | 'subtract'): void {
handleClick(e: React.SyntheticEvent<HTMLElement>) {
e.stopPropagation();
}

@autobind
handleEnhanceModeChange(
action: 'add' | 'subtract',
e: React.MouseEvent
): void {
e.stopPropagation();
const {value, step = 1, disabled, readOnly, precision} = this.props;
// value为undefined会导致溢出错误
let val = value || 0;
Expand Down Expand Up @@ -342,6 +351,7 @@ export class NumberInput extends React.Component<NumberProps, NumberState> {
disabled={disabled}
placeholder={placeholder}
onFocus={this.handleFocus}
onClick={this.handleClick}
onBlur={this.handleBlur}
stringMode={this.isBig ? true : false}
keyboard={keyboard}
Expand Down Expand Up @@ -386,7 +396,7 @@ export class NumberInput extends React.Component<NumberProps, NumberState> {
disabled ? 'Number--enhance-border-disabled' : '',
readOnly ? 'Number--enhance-border-readOnly' : ''
)}
onClick={() => this.handleEnhanceModeChange('subtract')}
onClick={e => this.handleEnhanceModeChange('subtract', e)}
>
<Icon
icon="minus"
Expand All @@ -403,7 +413,7 @@ export class NumberInput extends React.Component<NumberProps, NumberState> {
disabled ? 'Number--enhance-border-disabled' : '',
readOnly ? 'Number--enhance-border-readOnly' : ''
)}
onClick={() => this.handleEnhanceModeChange('add')}
onClick={e => this.handleEnhanceModeChange('add', e)}
>
<Icon
icon="plus"
Expand Down
56 changes: 55 additions & 1 deletion packages/amis/__tests__/renderers/Form/number.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const setup = async (
formOptions: any = {},
formItems: any[] = [{}]
) => {
const fetcher = jest.fn().mockImplementation(() =>
Promise.resolve({
data: {
status: 0,
msg: 'ok',
data: {
id: '12'
}
}
})
);
const utils = render(
amisRender(
{
Expand All @@ -39,7 +50,9 @@ const setup = async (
...formOptions
},
{},
makeEnv()
makeEnv({
fetcher
})
)
);

Expand Down Expand Up @@ -185,6 +198,47 @@ test('Renderer:number with unitOptions', async () => {
expect(container).toMatchSnapshot();
});

test('Renderer:number with unitOptions and default value', async () => {
const {container} = await setup(
{
unitOptions: ['px', '%', 'em'],
value: 12
},
{},
[
{
type: 'static',
name: 'number'
}
]
);

const staticDom = container.querySelector('.cxd-PlainField') as Element;
expect(staticDom.innerHTML).toBe('12px');
});

test('Renderer:number with unitOptions and initApi', async () => {
const {container} = await setup(
{
name: 'id',
unitOptions: ['px', '%', 'em']
},
{
initApi: '/amis/api/mock2/sample/12'
},
[
{
type: 'static',
name: 'id'
}
]
);
await wait(500); // 等待 initApi 加载完

const staticDom = container.querySelector('.cxd-PlainField') as Element;
expect(staticDom.innerHTML).toBe('12px');
});

test('Renderer:number with precision and default value', async () => {
const {input, wrap, container, getByText} = await setup({
precision: 2,
Expand Down
11 changes: 10 additions & 1 deletion packages/amis/src/renderers/Form/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,19 @@ export default class NumberControl extends React.Component<
}

componentDidUpdate(prevProps: NumberProps) {
const unit = this.getUnit();
const value = this.props.value;
if (
value != null &&
(typeof value === 'string' || typeof value === 'number') &&
unit &&
!String(value).endsWith(unit)
) {
this.props.setPrinstineValue(this.getValue(value));
Copy link
Member

Choose a reason for hiding this comment

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

初步感觉有两个问题

  1. didMount 没有相关逻辑
  2. 如果 外部 value 发生变化,而且初始化之后,不应该是 setPrinstineValue 而是 onChange出去

}
// 匹配 数字 + ?字符
const reg = /^([-+]?(([1-9]\d*\.?\d*)|(0\.\d*[1-9]))[^\d\.]*)$/;
if (reg.test(this.props.value) && this.props.value !== prevProps.value) {
const unit = this.getUnit();
this.setState({unit: unit});
}

Expand Down
Loading