Skip to content

Commit

Permalink
fix: 修复有单位时默认值不对,点击事件阻止冒泡
Browse files Browse the repository at this point in the history
  • Loading branch information
liujintao03 committed Dec 21, 2023
1 parent 6d31c36 commit 726dd8f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
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
19 changes: 19 additions & 0 deletions packages/amis/__tests__/renderers/Form/number.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ 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 precision and default value', async () => {
const {input, wrap, container, getByText} = await setup({
precision: 2,
Expand Down
4 changes: 4 additions & 0 deletions packages/amis/src/renderers/Form/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export default class NumberControl extends React.Component<
}

this.state = {unit, unitOptions};

if (unit && value != null) {
setPrinstineValue(this.getValue(value));
}
}

/**
Expand Down

0 comments on commit 726dd8f

Please sign in to comment.