-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add toggle buttons to pub field form elements (#665)
* add toggle buttons to pub field form elements * only render field toggles on pub create form * allow creation of empty pub * remove log level change
- Loading branch information
Showing
22 changed files
with
339 additions
and
65 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use client"; | ||
|
||
import { PropsWithChildren } from "react"; | ||
|
||
import { Toggle } from "ui/toggle"; | ||
|
||
import { useFormElementToggleContext } from "./FormElementToggleContext"; | ||
|
||
type Props = PropsWithChildren<ElementProps>; | ||
|
||
export const FormElementToggle = (props: Props) => { | ||
const formElementToggle = useFormElementToggleContext(); | ||
const isEnabled = formElementToggle.isEnabled(props.name); | ||
return ( | ||
<div className="flex gap-2"> | ||
<Toggle | ||
aria-label="Toggle field" | ||
className={ | ||
"z-50 h-full w-2 rounded-full p-0 data-[state=off]:bg-gray-200 data-[state=on]:bg-gray-400 data-[state=off]:opacity-50" | ||
} | ||
pressed={isEnabled} | ||
onClick={() => formElementToggle.toggle(props.name)} | ||
/> | ||
<div className="w-full">{props.children}</div> | ||
</div> | ||
); | ||
}; |
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,58 @@ | ||
"use client"; | ||
|
||
import { | ||
createContext, | ||
PropsWithChildren, | ||
useCallback, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from "react"; | ||
|
||
type FormElementToggleContext = { | ||
isEnabled: (field: string) => boolean; | ||
toggle: (field: string) => void; | ||
}; | ||
|
||
const FormElementToggleContext = createContext<FormElementToggleContext>({ | ||
isEnabled: () => true, | ||
toggle: () => {}, | ||
}); | ||
|
||
type Props = PropsWithChildren<{ | ||
fields: string[]; | ||
}>; | ||
|
||
export const FormElementToggleProvider = (props: Props) => { | ||
const [enabledFields, setEnabledFields] = useState(new Set(props.fields)); | ||
|
||
const isEnabled = useCallback( | ||
(field: string) => { | ||
return enabledFields.has(field); | ||
}, | ||
[enabledFields] | ||
); | ||
|
||
const toggle = useCallback( | ||
(field: string) => { | ||
const nextEnabledFields = new Set(enabledFields); | ||
if (nextEnabledFields.has(field)) { | ||
nextEnabledFields.delete(field); | ||
} else { | ||
nextEnabledFields.add(field); | ||
} | ||
setEnabledFields(nextEnabledFields); | ||
}, | ||
[enabledFields] | ||
); | ||
|
||
const value = useMemo(() => ({ isEnabled, toggle }), [isEnabled, toggle, enabledFields]); | ||
|
||
return ( | ||
<FormElementToggleContext.Provider value={value}> | ||
{props.children} | ||
</FormElementToggleContext.Provider> | ||
); | ||
}; | ||
|
||
export const useFormElementToggleContext = () => useContext(FormElementToggleContext); |
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
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
Oops, something went wrong.