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 38812b4
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
],
"no-eq-null": "off",
"no-redeclare": "off",
"no-restricted-imports": [
"error",
{
Expand Down
2 changes: 1 addition & 1 deletion packages/checkbox/src/index.mts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 };
export function useRef<Value>(val: Value) {
return useState({ current: val })[0];
}
6 changes: 5 additions & 1 deletion packages/core/src/lib/use-state.mts
Original file line number Diff line number Diff line change
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>(
defaultValue?: NotFunction<Value> | (() => Value),
): [Value | undefined, (newValue?: Value | undefined) => void];
export function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)) {
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 38812b4

Please sign in to comment.