-
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.
Customize styling of the porting embed (#5)
- Adds the ability to pass in custom class names based on a class name function - This supports all kinds of custom stylings: CSS Modules, css-in-js, Tailwind, plain css - Still ships with some unique class names (`GigsEmbeds-XYZ`) so we can ship a default CSS file - Adds an option to use a custom `formId`, which is used together with the submit button - Adds an `stepChange` event which is fired when the wizard step changes
- Loading branch information
Showing
24 changed files
with
1,631 additions
and
117 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,11 +1,27 @@ | ||
type Props = React.HTMLAttributes<HTMLInputElement> | ||
import { FieldStore } from '@modular-forms/preact' | ||
|
||
import { useEmbedOptions } from './Options' | ||
|
||
type Props = { | ||
of: FieldStore<any, string> // eslint-disable-line @typescript-eslint/no-explicit-any | ||
} & React.HTMLAttributes<HTMLInputElement> | ||
|
||
export function EmbedFieldInput({ of: field, ...rest }: Props) { | ||
const options = useEmbedOptions() | ||
const customClassName = | ||
options.className?.input?.({ | ||
name: field.name, | ||
touched: field.touched.value, | ||
dirty: field.dirty.value, | ||
valid: !field.error.value, | ||
}) || '' | ||
const id = `__ge_${field.name}` | ||
|
||
export function EmbedFieldInput(props: Props) { | ||
// TODO: customizable classNames | ||
return ( | ||
<input | ||
className="GigsEmbeds GigsPortingEmbed GigsEmbeds-fieldInput" | ||
{...props} | ||
className={`GigsEmbeds GigsPortingEmbed GigsEmbeds-input ${customClassName}`} | ||
id={id} | ||
{...rest} | ||
/> | ||
) | ||
} |
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,11 +1,27 @@ | ||
type Props = React.HTMLAttributes<HTMLLabelElement> | ||
import { FieldStore } from '@modular-forms/preact' | ||
|
||
import { useEmbedOptions } from './Options' | ||
|
||
type Props = { | ||
of: FieldStore<any, string> // eslint-disable-line @typescript-eslint/no-explicit-any | ||
} & React.HTMLAttributes<HTMLLabelElement> | ||
|
||
export function EmbedFieldLabel({ of: field, ...rest }: Props) { | ||
const options = useEmbedOptions() | ||
const customClassName = | ||
options.className?.label?.({ | ||
name: field.name, | ||
touched: field.touched.value, | ||
dirty: field.dirty.value, | ||
valid: !field.error.value, | ||
}) || '' | ||
const id = `__ge_${field.name}` | ||
|
||
export function EmbedFieldLabel(props: Props) { | ||
// TODO: customizable classNames | ||
return ( | ||
<label | ||
className="GigsEmbeds GigsPortingEmbed GigsEmbeds-fieldLabel" | ||
{...props} | ||
htmlFor={id} | ||
className={`GigsEmbeds GigsPortingEmbed GigsEmbeds-label ${customClassName}`} | ||
{...rest} | ||
/> | ||
) | ||
} |
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,36 @@ | ||
import { createContext } from 'preact' | ||
import { useContext } from 'preact/hooks' | ||
|
||
type FieldState = { | ||
name: string | ||
dirty: boolean | ||
touched: boolean | ||
valid: boolean | ||
} | ||
|
||
type FormState = { | ||
name: string | ||
dirty: boolean | ||
valid: boolean | ||
submitting: boolean | ||
touched: boolean | ||
} | ||
|
||
export const defaultFormId = 'gigsPortingEmbedForm' | ||
|
||
export type EmbedOptions = { | ||
formId?: string | ||
className?: { | ||
form?: (state: FormState) => string | ||
field?: (state: FieldState) => string | ||
input?: (state: FieldState) => string | ||
label?: (state: FieldState) => string | ||
error?: (state: Omit<FieldState, 'valid'>) => string | ||
} | ||
} | ||
|
||
export const OptionsContext = createContext<EmbedOptions>({}) | ||
|
||
export function useEmbedOptions() { | ||
return useContext(OptionsContext) | ||
} |
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.