Skip to content

fix(Field): show warning in case of properties misuse #407

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

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/cuddly-actors-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cube-dev/ui-kit': patch
---

Fix "for" attribute in field labels.
5 changes: 5 additions & 0 deletions .changeset/spicy-apricots-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cube-dev/ui-kit': patch
---

Show warning if a field is linked to a form but default value is provided. And in case when a field is unlinked but validation rules are provided.'
4 changes: 2 additions & 2 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ module.exports = [
}),
);
},
limit: '214kB',
limit: '215kB',
},
{
name: 'Tree shaking (just a Button)',
path: './dist/es/index.js',
webpack: true,
import: '{ Button }',
limit: '28 kB',
limit: '29 kB',
},
];
2 changes: 2 additions & 0 deletions src/components/forms/Form/use-field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ValidationState,
} from '../../../../shared';
import { CubeFormInstance } from '../use-form';
import { Props } from '../../../../tasty';

export interface CubeFieldProps<T extends FieldTypes> {
/** The initial value of the input. */
Expand All @@ -33,6 +34,7 @@ export interface CubeFieldProps<T extends FieldTypes> {
validateTrigger?: ValidateTrigger;
/** Message for the field. Some additional information or error notice */
message?: ReactNode;
labelProps?: Props;
}

export type FieldReturnValue<T extends FieldTypes> = {
Expand Down
44 changes: 42 additions & 2 deletions src/components/forms/Form/use-field/use-field-props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { useDebugValue } from 'react';
import { useChainedCallback, useEvent } from '../../../../_internal';
import { useInsideLegacyField } from '../Field';
import { mergeProps } from '../../../../utils/react';
import { warn } from '../../../../utils/warnings';

import { useField } from './use-field';

import type { ValidateTrigger } from '../../../../shared';
import type { CubeFieldProps } from './types';
import type { FieldTypes } from '../types';
import type { ValidateTrigger } from '../../../../shared';

export type UseFieldPropsParams = {
valuePropsMapper?: ({ value, onChange }) => any;
Expand All @@ -24,6 +25,19 @@ export type UseFieldPropsParams = {
unsafe__isDisabled?: boolean;
};

const VALUE_PROPERTIES = [
'value',
'defaultValue',
'isSelected',
'defaultSelected',
'isIndeterminate',
'defaultIndeterminate',
'selectedKey',
'defaultSelectedKey',
'selectedKeys',
'defaultSelectedKeys',
];

export function useFieldProps<
T extends FieldTypes,
Props extends CubeFieldProps<T>,
Expand Down Expand Up @@ -77,7 +91,23 @@ export function useFieldProps<
),
);

const result = isOutsideOfForm
if (props.rules && !props.name) {
warn(
`The "rules" prop is not suitable for fields that are not part of a form. Use "name" prop to link the field to a form.`,
);
}

if (isOutsideOfForm) {
for (const valuePropName of VALUE_PROPERTIES) {
if (valuePropName in props) {
warn(
`The "${valuePropName}" property is not suitable for fields that are part of a form. To set default values, please use the "defaultValues" property of the form component instead. To unlink the field from the form, remove the "name" property from the field.`,
);
}
}
}

const result: Props = isOutsideOfForm
? props
: mergeProps(
props,
Expand All @@ -89,6 +119,16 @@ export function useFieldProps<
},
);

if (result.id) {
if (!result.labelProps) {
result.labelProps = {};
}

if (result.labelProps) {
result.labelProps.for = result.id;
}
}

if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line react-hooks/rules-of-hooks
useDebugValue(result);
Expand Down
11 changes: 10 additions & 1 deletion src/components/forms/Switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,16 @@ function Switch(props: WithNullableSelected<CubeSwitchProps>, ref) {
let domRef = useFocusableRef(ref, inputRef);

// eslint-disable-next-line react-hooks/rules-of-hooks
let { inputProps } = useSwitch(props, useToggleState(props), inputRef);
let { inputProps } = useSwitch(
{
...props,
...(typeof label === 'string' && label.trim()
? { 'aria-label': label }
: {}),
},
useToggleState(props),
inputRef,
);

const mods = {
'inside-form': insideForm,
Expand Down