-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ `useForwardedProxyRef` hook
- Loading branch information
Showing
12 changed files
with
282 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@ezez/hooks", | ||
"version": "2.1.1+", | ||
"version": "2.2.0", | ||
"repository": "[email protected]:dzek69/ezez-hooks.git", | ||
"author": "Jacek Nowacki", | ||
"license": "MIT", | ||
|
@@ -61,7 +61,7 @@ | |
"husky": "^8.0.3", | ||
"jest": "^29.6.1", | ||
"must": "^0.13.4", | ||
"next": "^14.0.4", | ||
"next": "^14.2.3", | ||
"nodemon": "^3.0.1", | ||
"prettier": "^2.8.8", | ||
"react": "^18.2.0", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,95 @@ | ||
import React, { forwardRef, useRef, useState } from "react"; | ||
|
||
import { useForwardedProxyRef } from "../../useForwardedProxyRef"; | ||
|
||
interface Props {} | ||
|
||
const noDucksProxy: ProxyHandler<HTMLInputElement | HTMLTextAreaElement> = { | ||
set(input, prop, value) { | ||
console.info("setting value on ducks proxy", value, input.value, value === input.value); | ||
|
||
if (value === "duck") { | ||
console.error("No ducks allowed!"); | ||
return true; | ||
} | ||
|
||
if (prop === "value" && typeof value === "string") { | ||
// eslint-disable-next-line no-param-reassign | ||
input[prop] = value; | ||
} | ||
return true; | ||
}, | ||
}; | ||
// @ts-expect-error Just a debug helper | ||
noDucksProxy.name = "No ducks"; | ||
|
||
const noChickenProxy: ProxyHandler<HTMLInputElement | HTMLTextAreaElement> = { | ||
set(input, prop, value) { | ||
console.info("setting value on chicken proxy", value, input.value, value === input.value); | ||
|
||
if (value === "chicken") { | ||
console.error("No chickens allowed!"); | ||
return true; | ||
} | ||
|
||
if (prop === "value" && typeof value === "string") { | ||
// eslint-disable-next-line no-param-reassign | ||
input[prop] = value; | ||
} | ||
return true; | ||
}, | ||
}; | ||
// @ts-expect-error Just a debug helper | ||
noChickenProxy.name = "No chicken"; | ||
|
||
const Input = forwardRef< | ||
HTMLInputElement | null, { proxy: ProxyHandler<HTMLInputElement | HTMLTextAreaElement> } | ||
>((props, ref) => { | ||
const theRef = useForwardedProxyRef(ref, props.proxy); | ||
|
||
// const standardRef = useRef<Date | number | null>(5); | ||
// const wrapped = useForwardedProxyRef(standardRef, {}); | ||
// console.info("Standard ref", { wrapped, standardRef }); | ||
|
||
return ( | ||
<input ref={theRef} /> | ||
); | ||
}); | ||
|
||
// eslint-disable-next-line react/no-multi-comp | ||
const UseForwardedProxyRef: React.FC<Props> = () => { | ||
const [number, setNumber] = useState(0); // just to trigger re-renders | ||
|
||
const [currentProxy, setCurrentProxy] = useState< | ||
ProxyHandler<HTMLInputElement | HTMLTextAreaElement> | ||
>(noDucksProxy); | ||
|
||
const ref = useRef<HTMLInputElement | null>(null); | ||
|
||
const setRefValue = (value: string) => { | ||
if (!ref.current) { | ||
console.error("No ref!"); | ||
return; | ||
} | ||
ref.current.value = value; | ||
}; | ||
|
||
return ( | ||
<div> | ||
Currently set proxy: {currentProxy === noDucksProxy ? "ducks preventing proxy" : "chicken preventing proxy"} | ||
<button onClick={() => { setCurrentProxy(noDucksProxy); }}>set proxy to ducks</button> | ||
<button onClick={() => { setCurrentProxy(noChickenProxy); }}>set proxy to chicken</button> | ||
<hr /> | ||
Input ref #{number}: <hr /> | ||
<Input key={number} ref={ref} proxy={currentProxy} /> | ||
<button onClick={() => { setNumber(p => p + 1); }}>Change ref</button> | ||
<hr /> | ||
Value manipulation: | ||
<button onClick={() => { setRefValue("lorem"); }}>call ref.current.value = "lorem";</button> | ||
<button onClick={() => { setRefValue("duck"); }}>call ref.current.value = "duck";</button> | ||
<button onClick={() => { setRefValue("chicken"); }}>call ref.current.value = "chicken";</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export { UseForwardedProxyRef }; |
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
Oops, something went wrong.