Skip to content

Commit

Permalink
Feat(core): Allow useState/useRef to initialize empty
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Apr 25, 2024
1 parent 6a18ae8 commit aedab20
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/checkbox/src/index.mts
Expand Up @@ -126,7 +126,7 @@ export default createPrompt(

const [active, setActive] = useState(bounds.first);
const [showHelpTip, setShowHelpTip] = useState(true);
const [errorMsg, setError] = useState<string | undefined>(undefined);
const [errorMsg, setError] = useState<string>();

useKeypress(async (key) => {

Check warning on line 131 in packages/checkbox/src/index.mts

View workflow job for this annotation

GitHub Actions / Linting

Async arrow function has a complexity of 21. Maximum allowed is 20

Check warning on line 131 in packages/checkbox/src/index.mts

View workflow job for this annotation

GitHub Actions / Linting

Async arrow function has a complexity of 21. Maximum allowed is 20
if (isEnterKey(key)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/lib/use-ref.mts
@@ -1,5 +1,7 @@
import { useState } from './use-state.mjs';

export function useRef<Value>(val: Value): { current: Value } {
export function useRef<Value>(val: Value): { current: Value };
export function useRef<Value>(val?: Value): { current: Value | undefined };

Check failure on line 4 in packages/core/src/lib/use-ref.mts

View workflow job for this annotation

GitHub Actions / Linting

'useRef' is already defined

Check failure on line 4 in packages/core/src/lib/use-ref.mts

View workflow job for this annotation

GitHub Actions / Linting

'useRef' is already defined
export function useRef<Value>(val: Value) {

Check failure on line 5 in packages/core/src/lib/use-ref.mts

View workflow job for this annotation

GitHub Actions / Linting

'useRef' is already defined

Check failure on line 5 in packages/core/src/lib/use-ref.mts

View workflow job for this annotation

GitHub Actions / Linting

'useRef' is already defined
return useState({ current: val })[0];
}
6 changes: 5 additions & 1 deletion packages/core/src/lib/use-state.mts
Expand Up @@ -4,7 +4,11 @@ type NotFunction<T> = T extends Function ? never : T;

export function useState<Value>(
defaultValue: NotFunction<Value> | (() => Value),
): [Value, (newValue: Value) => void] {
): [Value, (newValue: Value) => void];
export function useState<Value>(

Check failure on line 8 in packages/core/src/lib/use-state.mts

View workflow job for this annotation

GitHub Actions / Linting

'useState' is already defined

Check failure on line 8 in packages/core/src/lib/use-state.mts

View workflow job for this annotation

GitHub Actions / Linting

'useState' is already defined
defaultValue?: NotFunction<Value> | (() => Value),
): [Value | undefined, (newValue?: Value | undefined) => void];
export function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)) {

Check failure on line 11 in packages/core/src/lib/use-state.mts

View workflow job for this annotation

GitHub Actions / Linting

'useState' is already defined

Check failure on line 11 in packages/core/src/lib/use-state.mts

View workflow job for this annotation

GitHub Actions / Linting

'useState' is already defined
return withPointer<Value, [Value, (newValue: Value) => void]>((pointer) => {
const setFn = (newValue: Value) => {
// Noop if the value is still the same.
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/index.mts
Expand Up @@ -28,7 +28,7 @@ export default createPrompt<string, EditorConfig>((config, done) => {

const [status, setStatus] = useState<string>('pending');
const [value, setValue] = useState<string>(config.default || '');
const [errorMsg, setError] = useState<string | undefined>(undefined);
const [errorMsg, setError] = useState<string>();

const isLoading = status === 'loading';
const prefix = usePrefix({ isLoading, theme });
Expand Down
2 changes: 1 addition & 1 deletion packages/expand/src/index.mts
Expand Up @@ -48,7 +48,7 @@ export default createPrompt<string, ExpandConfig>((config, done) => {
const [status, setStatus] = useState<string>('pending');
const [value, setValue] = useState<string>('');
const [expanded, setExpanded] = useState<boolean>(defaultExpandState);
const [errorMsg, setError] = useState<string | undefined>(undefined);
const [errorMsg, setError] = useState<string>();
const theme = makeTheme(config.theme);
const prefix = usePrefix({ theme });

Expand Down
6 changes: 2 additions & 4 deletions packages/input/src/index.mts
Expand Up @@ -22,10 +22,8 @@ export default createPrompt<string, InputConfig>((config, done) => {
const { validate = () => true } = config;
const theme = makeTheme(config.theme);
const [status, setStatus] = useState<string>('pending');
const [defaultValue = '', setDefaultValue] = useState<string | undefined>(
config.default,
);
const [errorMsg, setError] = useState<string | undefined>(undefined);
const [defaultValue = '', setDefaultValue] = useState<string>(config.default);
const [errorMsg, setError] = useState<string>();
const [value, setValue] = useState<string>('');

const isLoading = status === 'loading';
Expand Down
2 changes: 1 addition & 1 deletion packages/password/src/index.mts
Expand Up @@ -22,7 +22,7 @@ export default createPrompt<string, PasswordConfig>((config, done) => {
const theme = makeTheme(config.theme);

const [status, setStatus] = useState<string>('pending');
const [errorMsg, setError] = useState<string | undefined>(undefined);
const [errorMsg, setError] = useState<string>();
const [value, setValue] = useState<string>('');

const isLoading = status === 'loading';
Expand Down
2 changes: 1 addition & 1 deletion packages/rawlist/src/index.mts
Expand Up @@ -36,7 +36,7 @@ export default createPrompt(
const { choices } = config;
const [status, setStatus] = useState<string>('pending');
const [value, setValue] = useState<string>('');
const [errorMsg, setError] = useState<string | undefined>(undefined);
const [errorMsg, setError] = useState<string>();
const theme = makeTheme(config.theme);
const prefix = usePrefix({ theme });

Expand Down
2 changes: 1 addition & 1 deletion packages/select/src/index.mts
Expand Up @@ -63,7 +63,7 @@ export default createPrompt(
const theme = makeTheme<SelectTheme>(selectTheme, config.theme);
const prefix = usePrefix({ theme });
const [status, setStatus] = useState('pending');
const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
const searchTimeoutRef = useRef<ReturnType<typeof setTimeout>>();

const bounds = useMemo(() => {
const first = items.findIndex(isSelectable);
Expand Down

0 comments on commit aedab20

Please sign in to comment.