Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECHOES-531 Implement FormFieldLabel #243

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions src/components/form/FormFieldLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,43 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import styled from '@emotion/styled';
import { type ReactNode, forwardRef } from 'react';
import { forwardRef } from 'react';
import { Label } from '../typography';

import { useFormFieldContext } from './FormFieldContext';
interface Props {
children: JSX.Element | string;
gregaubert marked this conversation as resolved.
Show resolved Hide resolved
isDisabled?: boolean;
isRequired?: boolean;
htmlFor: string;
}

export type FormFieldLabelProps = {
children: ReactNode;
htmlFor?: string;
};

export const FormFieldLabel = forwardRef<HTMLLabelElement, FormFieldLabelProps>((props, ref) => {
const context = useFormFieldContext();
const { htmlFor = context.id, ...rest } = props;
return <StyledLabel htmlFor={htmlFor} ref={ref} {...rest} />;
export const FormFieldLabel = forwardRef<HTMLLabelElement, Props>((props, ref) => {
const { children, htmlFor, isDisabled = false, isRequired = false, ...rest } = props;
gregaubert marked this conversation as resolved.
Show resolved Hide resolved
return (
<StyledLabel htmlFor={htmlFor} isSubdued={isDisabled} ref={ref} {...rest}>
{children}
{isRequired && (
<FormFieldLabelRequired {...(isDisabled && { 'data-disabled': true })}>
*
</FormFieldLabelRequired>
)}
</StyledLabel>
);
});

FormFieldLabel.displayName = 'FormFieldLabel';

const StyledLabel = styled.label`
const StyledLabel = styled(Label)`
display: block;
`;

const FormFieldLabelRequired = styled.span`
color: var(--echoes-color-text-danger);
font: var(--echoes-typography-others-label-medium);
margin-left: var(--echoes-dimension-space-25);

&[data-disabled] {
color: var(--echoes-color-text-subdued);
}
`;
FormFieldLabelRequired.displayName = 'FormFieldLabelRequired';
4 changes: 2 additions & 2 deletions src/components/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
export { FormField, type FormFieldProps, WithProps, type WithPropsProps } from './FormField';
export { FormField, WithProps, type FormFieldProps, type WithPropsProps } from './FormField';
export { FormFieldState } from './FormFieldContext';
export { FormFieldLabel, type FormFieldLabelProps } from './FormFieldLabel';
export { FormFieldLabel } from './FormFieldLabel';
gregaubert marked this conversation as resolved.
Show resolved Hide resolved
export { FormFieldMessage, type FormFieldMessageProps } from './FormFieldMessage';
8 changes: 5 additions & 3 deletions stories/Form-stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export const Default: Story = {
args: {},
render: (args) => (
<FormField {...args}>
<FormFieldLabel>This is a label</FormFieldLabel>
<FormFieldLabel htmlFor="" isRequired>
This is a label
</FormFieldLabel>
<TextInput />
<FormFieldMessage>This is a message</FormFieldMessage>
</FormField>
Expand All @@ -56,7 +58,7 @@ export const ErrorState: Story = {
args: {},
render: (args) => (
<FormField state="error" {...args}>
<FormFieldLabel>This is a label</FormFieldLabel>
<FormFieldLabel htmlFor="">This is a label</FormFieldLabel>
<TextInput />
<FormFieldMessage>This is an error message</FormFieldMessage>
</FormField>
Expand All @@ -68,7 +70,7 @@ export const WithProps: Story = {
args: {},
render: (args) => (
<FormFieldWithProps
label={<FormFieldLabel>This is a label</FormFieldLabel>}
label={<FormFieldLabel htmlFor="">This is a label</FormFieldLabel>}
message={<FormFieldMessage>This is a message</FormFieldMessage>}
{...args}>
<TextInput key={1} />
Expand Down
Loading