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

feat(InputPercent): add InputPercent component #851

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './fullscreen/index.js';
export * from './header/index.js';
export * from './hooks/index.js';
export * from './info-panel/index.js';
export * from './input-percent/index.js';
export * from './logger/index.js';
export * from './root-layout/index.js';
export * from './split-pane/index.js';
Expand Down
41 changes: 41 additions & 0 deletions src/components/input-percent/InputPercent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { InputGroup } from '@blueprintjs/core';
import type { ChangeEvent } from 'react';
import { useCallback } from 'react';

export interface InputPercentProps {
/**
* Between 0 and 1
*/
value: number;
onChange: (value: number) => void;
digits?: number;
}

export function InputPercent(props: InputPercentProps) {
const { value, onChange: inputPercentChange, digits } = props;

const onChange = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const newValue = event.currentTarget.value;

if (Number(newValue) < 0 || Number(newValue) > 100) {
return;
}

inputPercentChange(Number(newValue) / 100);
},
[inputPercentChange],
);

return (
<InputGroup
value={String((value * 100).toFixed(digits))}
onChange={onChange}
leftIcon="percentage"
type="number"
min={0}
max={100}
step={1}
/>
);
}
1 change: 1 addition & 0 deletions src/components/input-percent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './InputPercent.js';
59 changes: 59 additions & 0 deletions stories/components/input-percent.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { Meta, StoryObj } from '@storybook/react';
import { useState } from 'react';

import type { InputPercentProps } from '../../src/components/index.js';
import { InputPercent } from '../../src/components/index.js';

export default {
title: 'Forms / InputPercent',
component: InputPercent,
decorators: (Story) => {
return (
<div style={{ width: 200, padding: 5 }}>
<Story />
</div>
);
},
} as Meta;

type Story = StoryObj<InputPercentProps>;

export const Control = {
argTypes: {
onChange: {
table: {
disable: true,
},
},
},
args: {
value: 0.2,
onChange: () => {},
digits: undefined,
},
} satisfies Story;

export const Functional = {
argTypes: {
onChange: {
table: {
disable: true,
},
},
value: {
table: {
disable: true,
},
},
},
render: (props) => {
const [state, setState] = useState(0);

return (
<div style={{ display: 'flex', flexDirection: 'column', gap: 5 }}>
<InputPercent {...props} value={state} onChange={setState} />
<p>State: {state}</p>
</div>
);
},
} satisfies Story;
4 changes: 3 additions & 1 deletion stories/components/input.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { FormGroupProps, InputGroupProps } from '@blueprintjs/core';
import { FormGroup, InputGroup } from '@blueprintjs/core';
import styled from '@emotion/styled';
import type { Meta } from '@storybook/react';
import type { ChangeEvent } from 'react';
import { useState } from 'react';

export default {
title: 'Forms / Input',
};
component: InputGroup,
} as Meta;

const ExampleGroup = styled.div`
display: flex;
Expand Down
Loading