-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ee42ac
commit 08337a1
Showing
12 changed files
with
215 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { NumberControl } from './Number'; | ||
|
||
export default { | ||
component: NumberControl, | ||
tags: ['autodocs'], | ||
parameters: { withRawArg: 'value', controls: { include: ['value', 'min', 'max', 'step'] } }, | ||
args: { name: 'number' }, | ||
} as Meta<typeof NumberControl>; | ||
|
||
export const Undefined: StoryObj<typeof NumberControl> = { | ||
args: { value: undefined }, | ||
}; | ||
// for security reasons a file input field cannot have an initial value, so it doesn't make sense to have stories for it | ||
|
||
export const Ten: StoryObj<typeof NumberControl> = { | ||
args: { value: 10 }, | ||
}; | ||
export const Zero: StoryObj<typeof NumberControl> = { | ||
args: { value: 0 }, | ||
}; | ||
|
||
export const WithMin: StoryObj<typeof NumberControl> = { | ||
args: { min: 1, value: 3 }, | ||
}; | ||
export const WithMax: StoryObj<typeof NumberControl> = { | ||
args: { max: 7, value: 3 }, | ||
}; | ||
export const WithMinAndMax: StoryObj<typeof NumberControl> = { | ||
args: { min: -2, max: 5, value: 3 }, | ||
}; | ||
export const LessThanMin: StoryObj<typeof NumberControl> = { | ||
args: { min: 3, value: 1 }, | ||
}; | ||
export const MoreThanMax: StoryObj<typeof NumberControl> = { | ||
args: { max: 3, value: 6 }, | ||
}; | ||
|
||
export const WithStep: StoryObj<typeof NumberControl> = { | ||
args: { step: 5, value: 3 }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { FC, ChangeEvent } from 'react'; | ||
import React, { useState, useCallback, useEffect, useRef } from 'react'; | ||
import { styled } from '@storybook/theming'; | ||
import { Form } from '@storybook/components'; | ||
import { getControlId, getControlSetterButtonId } from './helpers'; | ||
|
||
import type { ControlProps, NumberValue, NumberConfig } from './types'; | ||
|
||
const Wrapper = styled.label({ | ||
display: 'flex', | ||
}); | ||
|
||
type NumberProps = ControlProps<NumberValue | null> & NumberConfig; | ||
|
||
export const parse = (value: string) => { | ||
const result = parseFloat(value); | ||
return Number.isNaN(result) ? undefined : result; | ||
}; | ||
|
||
export const format = (value: NumberValue) => (value != null ? String(value) : ''); | ||
|
||
export const NumberControl: FC<NumberProps> = ({ | ||
name, | ||
value, | ||
onChange, | ||
min, | ||
max, | ||
step, | ||
onBlur, | ||
onFocus, | ||
}) => { | ||
const [inputValue, setInputValue] = useState(typeof value === 'number' ? value : ''); | ||
const [forceVisible, setForceVisible] = useState(false); | ||
const [parseError, setParseError] = useState<Error>(null); | ||
|
||
const handleChange = useCallback( | ||
(event: ChangeEvent<HTMLInputElement>) => { | ||
setInputValue(event.target.value); | ||
|
||
const result = parseFloat(event.target.value); | ||
if (Number.isNaN(result)) { | ||
setParseError(new Error(`'${event.target.value}' is not a number`)); | ||
} else { | ||
onChange(result); | ||
setParseError(null); | ||
} | ||
}, | ||
[onChange, setParseError] | ||
); | ||
|
||
const onForceVisible = useCallback(() => { | ||
setInputValue('0'); | ||
onChange(0); | ||
setForceVisible(true); | ||
}, [setForceVisible]); | ||
|
||
const htmlElRef = useRef(null); | ||
useEffect(() => { | ||
if (forceVisible && htmlElRef.current) htmlElRef.current.select(); | ||
}, [forceVisible]); | ||
|
||
useEffect(() => { | ||
const newInputValue = typeof value === 'number' ? value : ''; | ||
if (inputValue !== newInputValue) { | ||
setInputValue(value); | ||
} | ||
}, [value]); | ||
|
||
if (!forceVisible && value === undefined) { | ||
return ( | ||
<Form.Button id={getControlSetterButtonId(name)} onClick={onForceVisible}> | ||
Set number | ||
</Form.Button> | ||
); | ||
} | ||
|
||
return ( | ||
<Wrapper> | ||
<Form.Input | ||
ref={htmlElRef} | ||
id={getControlId(name)} | ||
type="number" | ||
onChange={handleChange} | ||
size="flex" | ||
placeholder="Edit number..." | ||
value={inputValue} | ||
valid={parseError ? 'error' : null} | ||
autoFocus={forceVisible} | ||
{...{ name, min, max, step, onFocus, onBlur }} | ||
/> | ||
</Wrapper> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
code/ui/blocks/src/controls/react-editable-json-tree/utils/parse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters